import { DataTypes } from 'sequelize';
import sequelize from '../database.js';
import Club from './Club.js';
import League from './League.js';
import Team from './Team.js';
import Location from './Location.js';

const Match = sequelize.define('Match', {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
        allowNull: false,
    },
    date: {
        type: DataTypes.DATE,
        allowNull: false,
    },
    time: {
        type: DataTypes.TIME,
        allowNull: true,
    },
    locationId: {
        type: DataTypes.INTEGER,
        references: {
            model: Location,
            key: 'id',
        },
        allowNull: true,
    },
    homeTeamId: {
        type: DataTypes.INTEGER,
        references: {
            model: Team,
            key: 'id',
        },
        allowNull: false,
    },
    guestTeamId: {
        type: DataTypes.INTEGER,
        references: {
            model: Team,
            key: 'id',
        },
        allowNull: false,
    },
    leagueId: {
        type: DataTypes.INTEGER,
        references: {
            model: League,
            key: 'id',
        },
        allowNull: false,
    },
    clubId: {
        type: DataTypes.INTEGER,
        references: {
            model: Club,
            key: 'id',
        },
        allowNull: false,
    },
    code: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: 'Spiel-Code aus PDF-Parsing'
    },
    homePin: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: 'Pin-Code für Heimteam aus PDF-Parsing'
    },
    guestPin: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: 'Pin-Code für Gastteam aus PDF-Parsing'
    },
    myTischtennisMeetingId: {
        type: DataTypes.STRING,
        allowNull: true,
        unique: true,
        comment: 'Meeting ID from myTischtennis (e.g. 15440488)',
        field: 'my_tischtennis_meeting_id'
    },
    homeMatchPoints: {
        type: DataTypes.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Match points won by home team',
        field: 'home_match_points'
    },
    guestMatchPoints: {
        type: DataTypes.INTEGER,
        allowNull: true,
        defaultValue: 0,
        comment: 'Match points won by guest team',
        field: 'guest_match_points'
    },
    isCompleted: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: 'Whether the match is completed',
        field: 'is_completed'
    },
    pdfUrl: {
        type: DataTypes.STRING,
        allowNull: true,
        comment: 'PDF URL from myTischtennis',
        field: 'pdf_url'
    },
    playersReady: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: 'Array of member IDs who are ready to play',
        field: 'players_ready'
    },
    playersPlanned: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: 'Array of member IDs who are planned to play',
        field: 'players_planned'
    },
    playersPlayed: {
        type: DataTypes.JSON,
        allowNull: true,
        comment: 'Array of member IDs who actually played',
        field: 'players_played'
    },
}, {
    underscored: true,
    tableName: 'match',
    timestamps: true,
});

export default Match;
