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

export interface QuestionnaireResponseAttributes {
    id: string;
    applicationId: string;
    questionnaireId: string;
    questionId: string;
    responseValue: string | null;
    attachmentUrl: string | null;
}

export interface QuestionnaireResponseInstance extends Model<QuestionnaireResponseAttributes>, QuestionnaireResponseAttributes { }

export default (sequelize: Sequelize) => {
    const QuestionnaireResponse = sequelize.define<QuestionnaireResponseInstance>('QuestionnaireResponse', {
        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: false,
            references: {
                model: 'questionnaires',
                key: 'id'
            }
        },
        questionId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'questionnaire_questions',
                key: 'id'
            }
        },
        responseValue: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        attachmentUrl: {
            type: DataTypes.STRING,
            allowNull: true
        }
    }, {
        tableName: 'questionnaire_responses',
        timestamps: true,
        updatedAt: false
    });

    (QuestionnaireResponse as any).associate = (models: any) => {
        QuestionnaireResponse.belongsTo(models.Application, { foreignKey: 'applicationId', as: 'application' });
        QuestionnaireResponse.belongsTo(models.Questionnaire, { foreignKey: 'questionnaireId', as: 'questionnaire' });
        QuestionnaireResponse.belongsTo(models.QuestionnaireQuestion, { foreignKey: 'questionId', as: 'question' });
    };

    return QuestionnaireResponse;
};
