const {DataTypes} = require("sequelize")

module.exports = (sequelize) => {
  const OptionOrder = sequelize.define(
    "OptionOrder",
    {
      userId: { type: DataTypes.INTEGER, allowNull: false },
      assetId: { type: DataTypes.INTEGER, allowNull: false },
      parentPositionId: { type: DataTypes.INTEGER, allowNull: true },

      symbol: { type: DataTypes.STRING, allowNull: false },
      expiry: { type: DataTypes.STRING, allowNull: false },
      strike: { type: DataTypes.DECIMAL(18, 4), allowNull: false },

      optionType: { type: DataTypes.ENUM("call", "put"), allowNull: false },
      side: { type: DataTypes.ENUM("buy", "sell"), allowNull: false },
      orderType: { type: DataTypes.ENUM("market", "limit"), allowNull: false },

      contracts: { type: DataTypes.INTEGER, allowNull: false },
      premium: { type: DataTypes.DECIMAL(18, 4), allowNull: false },
      totalCost: { type: DataTypes.DECIMAL(18, 4), allowNull: false, defaultValue: 0 },
      reservedAmount: { type: DataTypes.DECIMAL(18, 4), allowNull: false, defaultValue: 0 },

      stopLoss: { type: DataTypes.DECIMAL(18, 4), allowNull: true, defaultValue: 0 },
      takeProfit: { type: DataTypes.DECIMAL(18, 4), allowNull: true, defaultValue: 0 },

      status: {
        type: DataTypes.ENUM("pending", "filled", "cancelled", "rejected"),
        defaultValue: "pending",
      },

      filledAt: { type: DataTypes.DATE, allowNull: true },
    },
    {
        underscored: true,
      tableName: "option_orders",
      timestamps: true,
    }
  );


  return OptionOrder;
};