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

export interface ResignationAuditAttributes {
    id: string;
    userId: string | null;
    resignationId: string;
    action: string;
    details: any | null;
    remarks: string | null;
}

export interface ResignationAuditInstance extends Model<ResignationAuditAttributes>, ResignationAuditAttributes { }

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

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

    return ResignationAudit;
};
