import { Model, DataTypes, Sequelize } from 'sequelize';

export interface InterviewAttributes {
    id: string;
    applicationId: string;
    level: number;
    scheduleDate: Date | null;
    interviewType: string;
    linkOrLocation: string | null;
    status: string;
    scheduledBy: string | null;
}

export interface InterviewInstance extends Model<InterviewAttributes>, InterviewAttributes { }

export default (sequelize: Sequelize) => {
    const Interview = sequelize.define<InterviewInstance>('Interview', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        level: {
            type: DataTypes.INTEGER,
            allowNull: false
        },
        scheduleDate: {
            type: DataTypes.DATE,
            allowNull: true
        },
        interviewType: {
            type: DataTypes.STRING,
            allowNull: false
        },
        linkOrLocation: {
            type: DataTypes.STRING,
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            defaultValue: 'scheduled'
        },
        scheduledBy: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        }
    }, {
        tableName: 'interviews',
        timestamps: true
    });

    (Interview as any).associate = (models: any) => {
        Interview.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
        Interview.belongsTo(models.User, { foreignKey: 'scheduledBy', as: 'scheduler' });
        Interview.hasMany(models.InterviewParticipant, { foreignKey: 'interviewId', as: 'participants' });
        Interview.hasMany(models.InterviewEvaluation, { foreignKey: 'interviewId', as: 'evaluations' });
    };

    return Interview;
};
