import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import Club from './Club.js';
import User from './User.js';

const FriendlyMatchInvitation = sequelize.define('FriendlyMatchInvitation', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
  },
  fromClubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Club,
      key: 'id',
    },
    onDelete: 'CASCADE',
    field: 'from_club_id',
  },
  toClubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Club,
      key: 'id',
    },
    onDelete: 'CASCADE',
    field: 'to_club_id',
  },
  proposedDate: {
    type: DataTypes.DATEONLY,
    allowNull: false,
    field: 'proposed_date',
  },
  proposedStartTime: {
    type: DataTypes.TIME,
    allowNull: true,
    field: 'proposed_start_time',
  },
  proposedMatchName: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'proposed_match_name',
  },
  message: {
    type: DataTypes.TEXT,
    allowNull: true,
  },
  status: {
    type: DataTypes.STRING(32),
    allowNull: false,
    defaultValue: 'pending',
  },
  createdByUserId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: User,
      key: 'id',
    },
    onDelete: 'SET NULL',
    field: 'created_by_user_id',
  },
  acceptedByUserId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: User,
      key: 'id',
    },
    onDelete: 'SET NULL',
    field: 'accepted_by_user_id',
  },
  acceptedAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'accepted_at',
  },
}, {
  tableName: 'friendly_match_invitation',
  underscored: true,
  timestamps: true,
  indexes: [
    {
      fields: ['to_club_id', 'status', 'proposed_date'],
    },
    {
      fields: ['from_club_id', 'status', 'proposed_date'],
    },
  ],
});

export default FriendlyMatchInvitation;
