// models/TournamentMatch.js
import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import Tournament from './Tournament.js';
import TournamentGroup from './TournamentGroup.js';

const TournamentMatch = sequelize.define('TournamentMatch', {
  stageId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  tournamentId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Tournament,
      key: 'id'
    },
    onDelete: 'CASCADE',
    onUpdate: 'CASCADE'
  },
  groupId: {
    type: DataTypes.INTEGER,
    allowNull: true,
    references: {
      model: TournamentGroup,
      key: 'id'
    },
    onDelete: 'SET NULL',
    onUpdate: 'CASCADE'
  },
  classId: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  groupRound: {
    type: DataTypes.INTEGER,
    allowNull: true,
  },
  round: {
    type: DataTypes.STRING,
    allowNull: false,
  },
  player1Id: {
    type: DataTypes.INTEGER,
  allowNull: true,
  },
  player2Id: {
    type: DataTypes.INTEGER,
  allowNull: true,
  },
  isFinished: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: false,
  },
  tableNumber: {
    type: DataTypes.INTEGER,
    allowNull: true,
    field: 'table_number',
  },
  result: {
    type: DataTypes.STRING,
    allowNull: true,
  },
}, {
  underscored: true,
  tableName: 'tournament_match',
  timestamps: true,
});

export default TournamentMatch;
