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

module.exports = (sequelize) => {
  // Trader Profile (for traders who can be copied)
  const TraderProfile = sequelize.define('TraderProfile', {
    id: {
      type: DataTypes.BIGINT.UNSIGNED,
      allowNull: false,
      autoIncrement: true,
      primaryKey: true,
    },
    reference:{
      type: DataTypes.STRING(16),
      allowNull:false,
      defaultValue: () => generateRandomString(16)
    },
    displayName: {
      type: DataTypes.STRING(50),
      allowNull: false,
      field: 'display_name',
    },
    avatar: {
      type: DataTypes.STRING(500),
    },
    bio: {
      type: DataTypes.TEXT,
    },
    tradingStyle: {
      type: DataTypes.STRING(100),
      field: 'trading_style',
    },
    markets: {
      type: DataTypes.JSON,
      defaultValue: [],
    },
    isVerified: {
      type: DataTypes.BOOLEAN,
      defaultValue: false,
      field: 'is_verified',
    },
    badge: {
      type: DataTypes.ENUM('none', 'rising_star', 'consistent', 'pro', 'expert', 'elite'),
      defaultValue: 'none',
    },
    riskLevel: {
      type: DataTypes.ENUM('low', 'medium', 'high'),
      defaultValue: 'medium',
      field: 'risk_level',
    },
    
    // Stats
    totalFollowers: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'total_followers',
    },
    totalCopiers: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'total_copiers',
    },
    totalTrades: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'total_trades',
    },
    successfulTrades: {
      type: DataTypes.INTEGER,
      defaultValue: 0,
      field: 'successful_trades',
    },
    winRate: {
      type: DataTypes.DECIMAL(10, 4),
      defaultValue: 0,
      field: 'win_rate',
    },
    profit7d: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'profit_7d',
    },
    profit30d: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'profit_30d',
    },
    profit90d: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'profit_90d',
    },
    profitAll: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'profit_all',
    },
    maxDrawdown: {
      type: DataTypes.DECIMAL(10, 4),
      defaultValue: 0,
      field: 'max_drawdown',
    },
    avgTradeSize: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 0,
      field: 'avg_trade_size',
    },

    // Settings
    minCopyAmount: {
      type: DataTypes.DECIMAL(20, 8),
      defaultValue: 100,
      field: 'min_copy_amount',
    },
    performanceFee: {
      type: DataTypes.DECIMAL(10, 4),
      defaultValue: 0.2,
      field: 'performance_fee',
    },
    acceptingCopiers: {
      type: DataTypes.BOOLEAN,
      defaultValue: true,
      field: 'accepting_copiers',
    },

    status: {
      type: DataTypes.ENUM('active', 'inactive', 'suspended'),
      defaultValue: 'active',
    },
    metadata: {
      type: DataTypes.JSON,
      defaultValue: {},
    },
  }, {
    tableName: 'trader_profiles',
    timestamps: true,
    underscored: true,
    indexes: [
      { fields: ['status'] },
      { fields: ['badge'] },
      { fields: ['risk_level'] },
      { fields: ['profit_30d'] },
      { fields: ['total_copiers'] },
    ],
  });
  return TraderProfile;
};
