"use strict";
const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const SignalProvider = sequelize.define(
    "SignalProvider",
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        autoIncrement: true,
        primaryKey: true,
        allowNull: false,
      },

     

      provider: { type: DataTypes.STRING(100), allowNull: false },
      name: { type: DataTypes.STRING(120), allowNull: false },
      description: { type: DataTypes.TEXT },

      avatar: { type: DataTypes.STRING(500) },

      verified: { type: DataTypes.BOOLEAN, defaultValue: false },

      rating: { type: DataTypes.DECIMAL(3, 2), defaultValue: 0 },
      totalSignals: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0, field: "total_signals" },
      successfulSignals: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0, field: "successful_signals" },
      winRate: { type: DataTypes.DECIMAL(5, 2), defaultValue: 0, field: "win_rate" },

      avgProfitPercent: { type: DataTypes.DECIMAL(10, 2), defaultValue: 0, field: "avg_profit_percent" },
      totalProfit: { type: DataTypes.DECIMAL(20, 8), defaultValue: 0, field: "total_profit" },

      subscribers: { type: DataTypes.INTEGER.UNSIGNED, defaultValue: 0 },

      price: { type: DataTypes.DECIMAL(10, 2), allowNull: false, defaultValue: 0 },
      pricePeriod: {
        type: DataTypes.ENUM("month", "year", "lifetime"),
        defaultValue: "month",
        field: "price_period",
      },

      tier: {
        type: DataTypes.ENUM("basic", "premium", "elite"),
        defaultValue: "basic",
      },

      specialties: { type: DataTypes.JSON },
      supportedPairs: { type: DataTypes.JSON, field: "supported_pairs" },

      tradingStyle: { type: DataTypes.STRING(60), field: "trading_style" },
      experience: { type: DataTypes.STRING(40) },

      status: {
        type: DataTypes.ENUM("active", "inactive", "suspended"),
        defaultValue: "active",
      },

      isPublic: { type: DataTypes.BOOLEAN, defaultValue: true, field: "is_public" },
      isFeatured: { type: DataTypes.BOOLEAN, defaultValue: false, field: "is_featured" },

      telegramChannel: { type: DataTypes.STRING(255), field: "telegram_channel" },
      discordServer: { type: DataTypes.STRING(255), field: "discord_server" },
      website: { type: DataTypes.STRING(255) },

      performanceData: { type: DataTypes.JSON, field: "performance_data" },
    },
    {
      tableName: "signal_providers",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["user_id"] },
        { fields: ["verified"] },
        { fields: ["status"] },
        { fields: ["tier"] },
        { fields: ["win_rate"] },
        { fields: ["rating"] },
      ],
    }
  );

  SignalProvider.associate = (models) => {
    SignalProvider.belongsTo(models.User, {
      foreignKey: "userId",
      as: "user",
    });

    SignalProvider.hasMany(models.Signal, {
      foreignKey: "providerId",
      as: "signals",
    });

    SignalProvider.hasMany(models.SignalSubscription, {
      foreignKey: "providerId",
      as: "subscriptions",
    });
  };

  return SignalProvider;
};