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

export interface LoaAcknowledgementAttributes {
    id: string;
    loaDocId: string;
    applicantId: string;
    acknowledgedAt: Date;
    remarks: string | null;
}

export interface LoaAcknowledgementInstance extends Model<LoaAcknowledgementAttributes>, LoaAcknowledgementAttributes { }

export default (sequelize: Sequelize) => {
    const LoaAcknowledgement = sequelize.define<LoaAcknowledgementInstance>('LoaAcknowledgement', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        loaDocId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loa_documents_generated',
                key: 'id'
            }
        },
        applicantId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        acknowledgedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        remarks: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'loa_acknowledgements',
        timestamps: true,
        updatedAt: false
    });

    (LoaAcknowledgement as any).associate = (models: any) => {
        LoaAcknowledgement.belongsTo(models.LoaDocumentGenerated, { foreignKey: 'loaDocId', as: 'loaDocument' });
        LoaAcknowledgement.belongsTo(models.User, { foreignKey: 'applicantId', as: 'applicant' });
    };

    return LoaAcknowledgement;
};
