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

const FriendlyMatchShared = sequelize.define('FriendlyMatchShared', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
  },
  homeClubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Club,
      key: 'id',
    },
    onDelete: 'CASCADE',
    field: 'home_club_id',
  },
  guestClubId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Club,
      key: 'id',
    },
    onDelete: 'CASCADE',
    field: 'guest_club_id',
  },
  date: {
    type: DataTypes.DATEONLY,
    allowNull: false,
  },
  startTime: {
    type: DataTypes.TIME,
    allowNull: true,
    field: 'start_time',
  },
  matchName: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'match_name',
  },
  homeTeamName: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'home_team_name',
  },
  guestTeamName: {
    type: DataTypes.STRING(255),
    allowNull: false,
    field: 'guest_team_name',
  },
  locationName: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'location_name',
  },
  locationAddress: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'location_address',
  },
  locationCity: {
    type: DataTypes.STRING(255),
    allowNull: true,
    field: 'location_city',
  },
  locationZip: {
    type: DataTypes.STRING(32),
    allowNull: true,
    field: 'location_zip',
  },
  matchSystem: {
    type: DataTypes.STRING(120),
    allowNull: false,
    defaultValue: 'Braunschweiger System',
    field: 'match_system',
  },
  singlesCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 12,
    field: 'singles_count',
  },
  doublesCount: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 4,
    field: 'doubles_count',
  },
  winningSets: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 3,
    field: 'winning_sets',
  },
  homeMatchPoints: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'home_match_points',
  },
  guestMatchPoints: {
    type: DataTypes.INTEGER,
    allowNull: false,
    defaultValue: 0,
    field: 'guest_match_points',
  },
  isCompleted: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
    field: 'is_completed',
  },
  homeParticipants: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'home_participants',
  },
  guestParticipants: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'guest_participants',
  },
  resultDetails: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'result_details',
  },
  playersReady: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'players_ready',
  },
  playersPlanned: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'players_planned',
  },
  playersPlayed: {
    type: DataTypes.JSON,
    allowNull: true,
    field: 'players_played',
  },
  status: {
    type: DataTypes.STRING(32),
    allowNull: false,
    defaultValue: 'active',
  },
  createdByUserId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: User,
      key: 'id',
    },
    onDelete: 'SET NULL',
    field: 'created_by_user_id',
  },
  createdFromInvitationId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'created_from_invitation_id',
  },
}, {
  tableName: 'friendly_match_shared',
  underscored: true,
  timestamps: true,
  indexes: [
    {
      fields: ['home_club_id', 'date', 'start_time'],
    },
    {
      fields: ['guest_club_id', 'date', 'start_time'],
    },
    {
      fields: ['status'],
    },
  ],
});

export default FriendlyMatchShared;
