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

/**
 * Withdrawal Model
 * Represents user withdrawal requests
 */
const Withdrawal = sequelize.define('Withdrawal', {
  id: {
    type: DataTypes.UUID,
    defaultValue: DataTypes.UUIDV4,
    primaryKey: true,
  },
  userId: {
    type: DataTypes.UUID,
    allowNull: false,
    field: 'user_id',
    references: {
      model: 'user',
      key: 'id',
    },
  },
  withdrawalNo: {
    type: DataTypes.STRING(50),
    allowNull: false,
    unique: true,
    field: 'withdrawal_no',
  },
  amount: {
    type: DataTypes.DECIMAL(10, 2),
    allowNull: false,
  },
  paymentMethod: {
    type: DataTypes.ENUM('wechat', 'alipay', 'bank'),
    allowNull: false,
    field: 'payment_method',
  },
  paymentDetails: {
    type: DataTypes.JSON,
    allowNull: false,
    field: 'payment_details',
  },
  status: {
    type: DataTypes.ENUM('waiting', 'processing', 'completed', 'rejected'),
    defaultValue: 'waiting',
    allowNull: false,
  },
  reviewedBy: {
    type: DataTypes.UUID,
    allowNull: true,
    field: 'reviewed_by',
    references: {
      model: 'admin',
      key: 'id',
    },
  },
  reviewedAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'reviewed_at',
  },
  rejectionReason: {
    type: DataTypes.TEXT,
    allowNull: true,
    field: 'rejection_reason',
  },
  completedAt: {
    type: DataTypes.DATE,
    allowNull: true,
    field: 'completed_at',
  },
}, {
  tableName: 'withdrawal',
  timestamps: true,
  underscored: true,
  indexes: [
    { fields: ['user_id'] },
    { fields: ['withdrawal_no'] },
    { fields: ['status'] },
    { fields: ['reviewed_by'] },
  ],
});

module.exports = Withdrawal;
