"use strict";
const { DataTypes } = require("sequelize");
const { generateTransactionId } = require("../utils/helpers");

module.exports = (sequelize) => {
  const TradeHistory = sequelize.define(
    "TradeHistory",
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      orderId: {
        type: DataTypes.STRING(20),
        allowNull: false,
        defaultValue: () => generateTransactionId('OD'),
        field: 'order_id'
      },
      userId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: "user_id",
      },
      tradedBy: {
        type: DataTypes.ENUM('copy','manual','auto','bot', 'signal'),
        allowNull:false,
        defaultValue: 'manual'
      },
      signalId:{
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'signal_id'
      },
      traderId:{
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'trader_id'
      },
     
      pairId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        references: {
          model: "market_pairs",
          key: "id"
        }
      },
      side: {
        type: DataTypes.ENUM("buy", "sell"),
        allowNull: false,
      },
      orderType: {
        type: DataTypes.ENUM("market", "limit", "stop-limit"),
        allowNull: false,
        field: "order_type",
      },
      volume: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: false,
      },
      entryPrice: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: true,
        field: "entry_price",
      },
      requestedPrice: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: true,
        field: "requested_price",
      },
      leverage: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      duration: {
        type: DataTypes.INTEGER,
        allowNull: true,
      },
      stopLoss: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: true,
        field: "stop_loss",
      },
      takeProfit: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: true,
        field: "take_profit",
      },
      status: {
        type: DataTypes.ENUM("open", "filled", "cancelled", "rejected", "closed"),
        allowNull: false,
        defaultValue: "open",
      },
      result: {
        type: DataTypes.ENUM('won','none', 'loss', 'refund'),
        defaultValue: 'none'
      },
      profitAmount: {
        type: DataTypes.DECIMAL(30, 10),
        allowNull: true,
        defaultValue: 0,
        field: "profit_amount",
      },
      closedAt: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "closed_at",
      },
    },
    {
      tableName: "trade_history",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["user_id", "created_at"] },
        { fields: ["user_id", "pair_id"] },
        { fields: ["user_id", "status"] },
      ],
    }
  );


  return TradeHistory;
};
