const {DataTypes} = require("sequelize")
module.exports = (sequelize) => {
  return sequelize.define(
    "SpotOrder",
    {
      id: { type: DataTypes.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true },
      user_id: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false },
      market_id: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false },
      side: { type: DataTypes.ENUM("buy", "sell"), allowNull: false },
      type: { type: DataTypes.ENUM("limit", "market", "stop_limit"), allowNull: false },
      price: { type: DataTypes.DECIMAL(30, 12), allowNull: true },
      stop_price: { type: DataTypes.DECIMAL(30, 12), allowNull: true },
      qty: { type: DataTypes.DECIMAL(30, 12), allowNull: false },
      qty_filled: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },
      tradedBy: {
        type: DataTypes.ENUM('copy','manual','auto','bot', 'signal'),
        allowNull:false,
        defaultValue: 'manual',
        field: 'traded_by'
      },
      signalId:{
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'signal_id'
      },
      traderId:{
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'trader_id'
      },
      botId:{
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'bot_id'
      },
      status: {
        type: DataTypes.ENUM("new", "partially_filled", "filled", "canceled", "rejected"),
        allowNull: false,
        defaultValue: "new",
      },
      client_order_id: { type: DataTypes.STRING(64), allowNull: true },
    },
    {
      tableName: "spot_orders",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["user_id", "status"] },
        { fields: ["market_id", "status"] },
        { fields: ["market_id", "side", "price", "created_at"] },
        { unique: true, fields: ["user_id", "client_order_id"] },
      ],
    }
  );
};