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

export interface QuestionnaireScoreAttributes {
    id: string;
    applicationId: string;
    questionnaireId?: string;
    sectionName?: string;
    score: number;
    maxScore?: number;
    status?: string;
    weightage?: number;
    weightedScore?: number;
}

export interface QuestionnaireScoreInstance extends Model<QuestionnaireScoreAttributes>, QuestionnaireScoreAttributes { }

export default (sequelize: Sequelize) => {
    const QuestionnaireScore = sequelize.define<QuestionnaireScoreInstance>('QuestionnaireScore', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        applicationId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'applications',
                key: 'id'
            }
        },
        questionnaireId: {
            type: DataTypes.UUID,
            allowNull: true
        },
        sectionName: {
            type: DataTypes.STRING,
            allowNull: true
        },
        score: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: false
        },
        maxScore: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true
        },
        status: {
            type: DataTypes.STRING,
            allowNull: true
        },
        weightage: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true
        },
        weightedScore: {
            type: DataTypes.DECIMAL(10, 2),
            allowNull: true
        }
    }, {
        tableName: 'questionnaire_scores',
        timestamps: true,
        updatedAt: true
    });

    (QuestionnaireScore as any).associate = (models: any) => {
        QuestionnaireScore.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
    };

    return QuestionnaireScore;
};
