const { DataTypes } = require('sequelize');

module.exports = (sequelize) => {
  const AssetLot = sequelize.define(
    'AssetLot',
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
      },
      userId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: 'user_id'
      },
      assetId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: 'asset_id'
      },
      quantity: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
      },
      buyPrice: {
        type: DataTypes.DECIMAL(20, 8),
        allowNull: false,
      },

      side: {
       type: DataTypes.ENUM(['buy' , 'sell']),
       allowNull: false,
      },
      type: {
        type: DataTypes.ENUM(['market' , 'limit', 'stop']),
        defaultValue: 'market',
       allowNull: false,
      },
      marketLimit:{
        type: DataTypes.DECIMAL(30,8),

      },
      traderId: {
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'trader_id'
      },
      botId: {
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'bot_id'
      },
      signalId: {
        type: DataTypes.BIGINT.UNSIGNED,
        field: 'signal_id'
      },
      tradeType: {
        type: DataTypes.ENUM('manual','copy','signal','bot','auto'),
        defaultValue: 'manual',
        field: 'trade_type'
      },
      status: {
        type: DataTypes.ENUM('open', 'closed'),
        allowNull: false,
        defaultValue: 'open',
      },
      timestamp:{
        type: DataTypes.DATE
      }
    },
    {
      tableName: 'asset_lots',
      underscored:true,
      timestamps: true,
      indexes: [
        { fields: ['user_id', 'asset_id'] },
        { fields: ['status'] },
      ],
    }
  );

  return AssetLot;
};
