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

export interface RelocationAuditAttributes {
    id: string;
    userId: string | null;
    relocationRequestId: string;
    action: string;
    details: any | null;
    remarks: string | null;
}

export interface RelocationAuditInstance extends Model<RelocationAuditAttributes>, RelocationAuditAttributes { }

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

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

    return RelocationAudit;
};
