const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {


  // Referral Earnings
  const ReferralEarning = sequelize.define('ReferralEarning', {
    id: {
       type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    referralId: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      field: 'referral_id',
      references: {
        model: 'referrals',
        key: 'id',
      },
    },
    referrerId: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      field: 'referrer_id',
    },
    referredId: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      field: 'referred_id',
    },
    type: {
      type: DataTypes.ENUM('trading_fee', 'bonus', 'tier_bonus'),
      allowNull: false,
    },
    currency: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    amount: {
      type: DataTypes.DECIMAL(20, 8),
      allowNull: false,
    },
    tradingVolume: {
      type: DataTypes.DECIMAL(30, 8),
      field: 'trading_volume',
    },
    commissionRate: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'commission_rate',
    },
    tradeId: {
      type: DataTypes.UUID,
      field: 'trade_id',
    },
    status: {
      type: DataTypes.ENUM('pending', 'credited', 'cancelled'),
      defaultValue: 'pending',
    },
    creditedAt: {
      type: DataTypes.DATE,
      field: 'credited_at',
    },
    metadata: {
      type: DataTypes.JSON,
      defaultValue: {},
    },
  }, {
    tableName: 'referral_earnings',
    timestamps: true,
    underscored: true,
    indexes: [
      { fields: ['referral_id'] },
      { fields: ['referrer_id'] },
      { fields: ['referred_id'] },
      { fields: ['status'] },
      { fields: ['type'] },
    ],
  });

  return ReferralEarning ;
};
