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

export interface TerminationAuditAttributes {
    id: string;
    userId: string | null;
    terminationRequestId: string;
    action: string;
    details: any | null;
    remarks: string | null;
}

export interface TerminationAuditInstance extends Model<TerminationAuditAttributes>, TerminationAuditAttributes { }

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

    (TerminationAudit as any).associate = (models: any) => {
        TerminationAudit.belongsTo(models.User, { foreignKey: 'userId', as: 'user' });
        TerminationAudit.belongsTo(models.TerminationRequest, { foreignKey: 'terminationRequestId', as: 'termination' });
    };

    return TerminationAudit;
};
