const {DataTypes} = require("sequelize")

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

      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 },

      contracts: { type: DataTypes.INTEGER, allowNull: false },
      entryPremium: { type: DataTypes.DECIMAL(18, 4), allowNull: false },
      markPremium: { type: DataTypes.DECIMAL(18, 4), allowNull: false },

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

      realizedPnl: { type: DataTypes.DECIMAL(18, 4), allowNull: false, defaultValue: 0 },

      status: {
        type: DataTypes.ENUM("open", "closed"),
        defaultValue: "open",
      },

      openedAt: { type: DataTypes.DATE, allowNull: true },
      closedAt: { type: DataTypes.DATE, allowNull: true },
    },
    {
      tableName: "option_positions",
      timestamps: true,
      underscored: true,
    }
  );

  return OptionPosition;
};