const { DataTypes } = require('sequelize');
const { sequelize } = require('../config/database');

const PaymentAuditEvent = sequelize.define('PaymentAuditEvent', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true
  },
  payment_id: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {
      model: 'payments',
      key: 'id'
    }
  },
  actor_user_id: {
    type: DataTypes.UUID,
    allowNull: true,
    references: {
      model: 'users',
      key: 'id'
    }
  },
  action: {
    type: DataTypes.ENUM(
      'create',
      'update_status',
      'update_details',
      'void',
      'gateway_update',
      'webhook_update'
    ),
    allowNull: false
  },
  reason: {
    type: DataTypes.TEXT,
    allowNull: true
  },
  before: {
    type: DataTypes.JSON,
    allowNull: true
  },
  after: {
    type: DataTypes.JSON,
    allowNull: true
  }
}, {
  tableName: 'payment_audit_events',
  timestamps: true,
  createdAt: 'created_at',
  updatedAt: 'updated_at',
  indexes: [
    { fields: ['payment_id', 'created_at'] },
    { fields: ['actor_user_id', 'created_at'] }
  ]
});

module.exports = PaymentAuditEvent;

