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

export interface QuestionnaireAttributes {
    id: string;
    version: string;
    isActive: boolean;
}

export interface QuestionnaireInstance extends Model<QuestionnaireAttributes>, QuestionnaireAttributes { }

export default (sequelize: Sequelize) => {
    const Questionnaire = sequelize.define<QuestionnaireInstance>('Questionnaire', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        version: {
            type: DataTypes.STRING,
            allowNull: false
        },
        isActive: {
            type: DataTypes.BOOLEAN,
            defaultValue: true
        }
    }, {
        tableName: 'questionnaires',
        timestamps: true
    });

    (Questionnaire as any).associate = (models: any) => {
        Questionnaire.hasMany(models.QuestionnaireQuestion, { foreignKey: 'questionnaireId', as: 'questions' });
        Questionnaire.hasMany(models.QuestionnaireResponse, { foreignKey: 'questionnaireId', as: 'responses' });
    };

    return Questionnaire;
};
