const { DataTypes } = require('sequelize');
const { generateRandomString } = require('../utils/helpers');

module.exports = (sequelize) => {
  // Trader Profile (for traders who can be copied)

  // Copy Trading Relationship
  const CopyRelation = sequelize.define('CopyRelation', {
    id: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    reference:{
      type: DataTypes.STRING(16),
      allowNull:false,
      defaultValue: () => generateRandomString(16)
    },
    copierId: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      field: 'copier_id',
      references: {
        model: 'users',
        key: 'id',
      },
    },
    traderId: {
      type: DataTypes.BIGINT.UNSIGNED,
      field: 'trader_id',
      
    },
    traderProfileId: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      field: 'trader_profile_id',
      references: {
        model: 'trader_profiles',
        key: 'id',
      },
    },
    type: {
      type: DataTypes.ENUM('follow', 'copy'),
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM('active', 'paused', 'stopped'),
      defaultValue: 'active',
    },

    // Copy Settings
    copyAmount: {
      type: DataTypes.DECIMAL(20, 8),
      field: 'copy_amount',
    },
    copyMode: {
      type: DataTypes.ENUM('proportional', 'fixed'),
      defaultValue: 'proportional',
      field: 'copy_mode',
    },
    maxPerTrade: {
      type: DataTypes.DECIMAL(20, 8),
      field: 'max_per_trade',
    },
    stopLossPercent: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'stop_loss_percent',
    },
    takeProfitPercent: {
      type: DataTypes.DECIMAL(10, 4),
      field: 'take_profit_percent',
    },
    notifications: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
    },

    // Stats
    invested: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
    },
    currentValue: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'current_value',
    },
    totalProfit: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'total_profit',
    },
    profitPercent: {
      type: DataTypes.DECIMAL(10, 4),
      defaultValue: 0,
      field: 'profit_percent',
    },
    copiedTrades: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'copied_trades',
    },
    winningTrades: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'winning_trades',
    },
    losingTrades: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'losing_trades',
    },

    startedAt: {
      type: DataTypes.DATE,
      defaultValue: DataTypes.NOW,
      field: 'started_at',
    },
    pausedAt: {
      type: DataTypes.DATE,
      field: 'paused_at',
    },
    stoppedAt: {
      type: DataTypes.DATE,
      field: 'stopped_at',
    },
    metadata: {
      type: DataTypes.JSON,
      defaultValue: {},
    },
  }, {
    tableName: 'copy_relations',
    timestamps: true,
    underscored: true,
    indexes: [
      { fields: ['copier_id'] },
      { fields: ['trader_profile_id'] },
      { fields: ['status'] },
      { fields: ['type'] },
      { unique: true, fields: ['copier_id', 'trader_profile_id', 'type'] },
    ],
  });

  return CopyRelation;
};
