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

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

      providerId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: "provider_id",
      },

      pair: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
      },

      marketType: {
        type: DataTypes.ENUM("spot", "forex", "stock", "terminal"),
        allowNull: false,
        defaultValue: "forex",
        field: "market_type",
      },

      type: {
        type: DataTypes.ENUM("buy", "sell", "long", "short", "close"),
        allowNull: false,
      },

      marketEntry: {
        type: DataTypes.ENUM("market", "limit", "stop"),
        allowNull: false,
        defaultValue: "market",
        field: "market_entry",
      },

      status: {
        type: DataTypes.ENUM("active", "closed", "cancelled", "expired"),
        defaultValue: "active",
      },

      entryPrice: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
        field: "entry_price",
      },

      currentPrice: {
        type: DataTypes.DECIMAL(20, 8),
        field: "current_price",
      },

      exitPrice: {
        type: DataTypes.DECIMAL(20, 8),
        field: "exit_price",
      },

      stopLoss: {
        type: DataTypes.DECIMAL(20, 8),
        field: "stop_loss",
      },

      takeProfit1: {
        type: DataTypes.DECIMAL(20, 8),
        field: "take_profit_1",
      },

      takeProfit2: {
        type: DataTypes.DECIMAL(20, 8),
        field: "take_profit_2",
      },

      takeProfit3: {
        type: DataTypes.DECIMAL(20, 8),
        field: "take_profit_3",
      },

      leverage: {
        type: DataTypes.INTEGER.UNSIGNED,
        defaultValue: 1,
      },

      timeframe: {
        type: DataTypes.STRING(20),
      },

      profitLoss: {
        type: DataTypes.DECIMAL(20, 8),
        defaultValue: 0,
        field: "profit_loss",
      },

      profitLossPercent: {
        type: DataTypes.DECIMAL(10, 2),
        defaultValue: 0,
        field: "profit_loss_percent",
      },

      riskRewardRatio: {
        type: DataTypes.DECIMAL(10, 2),
        field: "risk_reward_ratio",
      },

      analysis: {
        type: DataTypes.TEXT,
      },

      imageUrl: {
        type: DataTypes.STRING(500),
        field: "image_url",
      },

      tags: {
        type: DataTypes.JSON,
      },

      confidence: {
        type: DataTypes.ENUM("low", "medium", "high"),
        defaultValue: "medium",
      },

      closedAt: {
        type: DataTypes.DATE,
        field: "closed_at",
      },

      expiresAt: {
        type: DataTypes.DATE,
        field: "expires_at",
      },

      subscribersTaken: {
        type: DataTypes.INTEGER.UNSIGNED,
        defaultValue: 0,
        field: "subscribers_taken",
      },

      successfulTrades: {
        type: DataTypes.INTEGER.UNSIGNED,
        defaultValue: 0,
        field: "successful_trades",
      },
    },
    {
      tableName: "signals",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["provider_id"] },
        { fields: ["pair"] },
        { fields: ["market_type"] },
        { fields: ["status"] },
        { fields: ["type"] },
        { fields: ["created_at"] },
      ],
    }
  );


  return Signal;
};