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

export interface FnFAuditAttributes {
    id: string;
    userId: string | null;
    fnfId: string;
    action: string;
    details: any | null;
    remarks: string | null;
}

export interface FnFAuditInstance extends Model<FnFAuditAttributes>, FnFAuditAttributes { }

export default (sequelize: Sequelize) => {
    const FnFAudit = sequelize.define<FnFAuditInstance>('FnFAudit', {
        id: {
            type: DataTypes.UUID,
            defaultValue: DataTypes.UUIDV4,
            primaryKey: true
        },
        userId: {
            type: DataTypes.UUID,
            allowNull: true,
            references: {
                model: 'users',
                key: 'id'
            }
        },
        fnfId: {
            type: DataTypes.UUID,
            allowNull: false,
            references: {
                model: 'fnf_settlements',
                key: 'id'
            }
        },
        action: {
            type: DataTypes.STRING,
            allowNull: false
        },
        details: {
            type: DataTypes.JSON,
            allowNull: true
        },
        remarks: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        tableName: 'fnf_audit_logs',
        timestamps: true,
        indexes: [
            { fields: ['fnfId'] },
            { fields: ['userId'] },
            { fields: ['action'] }
        ]
    });

    (FnFAudit as any).associate = (models: any) => {
        FnFAudit.belongsTo(models.User, { foreignKey: 'userId', as: 'user' });
        FnFAudit.belongsTo(models.FnF, { foreignKey: 'fnfId', as: 'fnf' });
    };

    return FnFAudit;
};
