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

/**
 * SystemAuditLog
 * ----------------
 * Dedicated, segregated audit trail for *system-level / configuration* changes
 * (questionnaire versions, interview configs, master data — zones / regions /
 * districts, SLA configs, system configs, role assignments, hierarchy syncs).
 *
 * Application-lifecycle audit (per-application stage transitions, documents,
 * interviews) continues to live in `audit_logs`. Module-specific tables
 * (`resignation_audit_logs`, `termination_audit_logs`, `fnf_audit_logs`,
 * `relocation_audit_logs`, `constitutional_audit_logs`) remain unchanged.
 */
export interface SystemAuditLogAttributes {
    id: string;
    userId: string | null;
    actorName: string | null;
    actorRole: string | null;
    module: string;
    entityType: string;
    entityId: string | null;
    entityLabel: string | null;
    action: string;
    description: string | null;
    oldData: any | null;
    newData: any | null;
    metadata: any | null;
    ipAddress: string | null;
    userAgent: string | null;
}

export interface SystemAuditLogInstance
    extends Model<SystemAuditLogAttributes>,
    SystemAuditLogAttributes { }

export default (sequelize: Sequelize) => {
    const SystemAuditLog = sequelize.define<SystemAuditLogInstance>(
        'SystemAuditLog',
        {
            id: {
                type: DataTypes.UUID,
                defaultValue: DataTypes.UUIDV4,
                primaryKey: true
            },
            userId: {
                type: DataTypes.UUID,
                allowNull: true,
                references: { model: 'users', key: 'id' },
                onDelete: 'SET NULL'
            },
            actorName: {
                type: DataTypes.STRING(150),
                allowNull: true
            },
            actorRole: {
                type: DataTypes.STRING(80),
                allowNull: true
            },
            module: {
                type: DataTypes.STRING(60),
                allowNull: false
            },
            entityType: {
                type: DataTypes.STRING(80),
                allowNull: false
            },
            entityId: {
                type: DataTypes.STRING(80),
                allowNull: true
            },
            entityLabel: {
                type: DataTypes.STRING(255),
                allowNull: true
            },
            action: {
                type: DataTypes.STRING(60),
                allowNull: false
            },
            description: {
                type: DataTypes.TEXT,
                allowNull: true
            },
            oldData: {
                type: DataTypes.JSON,
                allowNull: true
            },
            newData: {
                type: DataTypes.JSON,
                allowNull: true
            },
            metadata: {
                type: DataTypes.JSON,
                allowNull: true
            },
            ipAddress: {
                type: DataTypes.STRING(64),
                allowNull: true
            },
            userAgent: {
                type: DataTypes.STRING(500),
                allowNull: true
            }
        },
        {
            tableName: 'system_audit_logs',
            timestamps: true,
            indexes: [
                { fields: ['module'] },
                { fields: ['entityType'] },
                { fields: ['entityId'] },
                { fields: ['action'] },
                { fields: ['userId'] },
                { fields: ['createdAt'] }
            ]
        }
    );

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

    return SystemAuditLog;
};
