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

export interface InterviewConfigItemAttributes {
    id: string;
    configId: string;
    itemKey: string;
    label: string;
    type: 'select' | 'text' | 'textarea' | 'number';
    order: number;
    isRequired: boolean;
    weight: number | null;
    maxScore: number | null;
}

export interface InterviewConfigItemInstance extends Model<InterviewConfigItemAttributes>, InterviewConfigItemAttributes { }

export default (sequelize: Sequelize) => {
    const InterviewConfigItem = sequelize.define<InterviewConfigItemInstance>('InterviewConfigItem', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        configId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'interview_configs',
                key: 'id'
            }
        },
        itemKey: {
            type: DataTypes.STRING,
            allowNull: false
        },
        label: {
            type: DataTypes.STRING,
            allowNull: false
        },
        type: {
            type: DataTypes.ENUM('select', 'text', 'textarea', 'number'),
            allowNull: false,
            defaultValue: 'text'
        },
        order: {
            type: DataTypes.INTEGER,
            allowNull: false,
            defaultValue: 1
        },
        isRequired: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        },
        weight: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true
        },
        maxScore: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true
        }
    }, {
        tableName: 'interview_config_items',
        timestamps: true
    });

    (InterviewConfigItem as any).associate = (models: any) => {
        InterviewConfigItem.belongsTo(models.InterviewConfig, { foreignKey: 'configId', as: 'config' });
        InterviewConfigItem.hasMany(models.InterviewConfigItemOption, { foreignKey: 'itemId', as: 'options' });
    };

    return InterviewConfigItem;
};
