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

export interface LoiAcknowledgementAttributes {
    id: string;
    loiDocId: string;
    applicantId: string;
    acknowledgedAt: Date;
    remarks: string | null;
}

export interface LoiAcknowledgementInstance extends Model<LoiAcknowledgementAttributes>, LoiAcknowledgementAttributes { }

export default (sequelize: Sequelize) => {
    const LoiAcknowledgement = sequelize.define<LoiAcknowledgementInstance>('LoiAcknowledgement', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        loiDocId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loi_documents_generated',
                key: 'id'
            }
        },
        applicantId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'users', // Applicant is a user
                key: 'id'
            }
        },
        acknowledgedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        },
        remarks: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'loi_acknowledgements',
        timestamps: true,
        updatedAt: false
    });

    (LoiAcknowledgement as any).associate = (models: any) => {
        LoiAcknowledgement.belongsTo(models.LoiDocumentGenerated, { foreignKey: 'loiDocId', as: 'loiDocument' });
        LoiAcknowledgement.belongsTo(models.User, { foreignKey: 'applicantId', as: 'applicant' });
    };

    return LoiAcknowledgement;
};
