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

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

export interface LoiDocumentGeneratedInstance extends Model<LoiDocumentGeneratedAttributes>, LoiDocumentGeneratedAttributes { }

export default (sequelize: Sequelize) => {
    const LoiDocumentGenerated = sequelize.define<LoiDocumentGeneratedInstance>('LoiDocumentGenerated', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        requestId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'loi_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: 'loi_documents_generated',
        timestamps: true,
        updatedAt: false
    });

    (LoiDocumentGenerated as any).associate = (models: any) => {
        LoiDocumentGenerated.belongsTo(models.LoiRequest, { foreignKey: 'requestId', as: 'request' });
        LoiDocumentGenerated.belongsTo(models.OnboardingDocument, { foreignKey: 'documentId', as: 'document' });
        LoiDocumentGenerated.hasMany(models.LoiAcknowledgement, { foreignKey: 'loiDocId', as: 'acknowledgements' });
    };

    return LoiDocumentGenerated;
};
