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

export interface LoaDocumentGeneratedAttributes {
    id: string;
    requestId: string;
    documentId: string;
    version: string;
    generatedAt: Date;
}

export interface LoaDocumentGeneratedInstance extends Model<LoaDocumentGeneratedAttributes>, LoaDocumentGeneratedAttributes { }

export default (sequelize: Sequelize) => {
    const LoaDocumentGenerated = sequelize.define<LoaDocumentGeneratedInstance>('LoaDocumentGenerated', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loa_requests',
                key: 'id'
            }
        },
        documentId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'onboarding_documents',
                key: 'id'
            }
        },
        version: {
            type: DataTypes.STRING,
            allowNull: false
        },
        generatedAt: {
            type: DataTypes.DATE,
            defaultValue: DataTypes.NOW
        }
    }, {
        tableName: 'loa_documents_generated',
        timestamps: true,
        updatedAt: false
    });

    (LoaDocumentGenerated as any).associate = (models: any) => {
        LoaDocumentGenerated.belongsTo(models.LoaRequest, { foreignKey: 'requestId', as: 'request' });
        LoaDocumentGenerated.belongsTo(models.OnboardingDocument, { foreignKey: 'documentId', as: 'document' });
        LoaDocumentGenerated.hasMany(models.LoaAcknowledgement, { foreignKey: 'loaDocId', as: 'acknowledgements' });
    };

    return LoaDocumentGenerated;
};
