const {DataTypes} = require("sequelize")

module.exports = (sequelize) => {
  return sequelize.define(
    "DerivativeAccount",
    {
      id: { type: DataTypes.BIGINT.UNSIGNED, primaryKey: true, autoIncrement: true },
      user_id: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false },

      kind: { type: DataTypes.ENUM("forex_cfd", "perp"), allowNull: false },
      margin_mode: { type: DataTypes.ENUM("cross", "isolated"), allowNull: false, defaultValue: "cross" },

      equity: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },
      balance: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },
      margin_used: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },
      margin_free: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },
      margin_level: { type: DataTypes.DECIMAL(30, 12), allowNull: false, defaultValue: 0 },

      status: { type: DataTypes.ENUM("active", "restricted", "liquidating"), allowNull: false, defaultValue: "active" },
    },
    {
      tableName: "derivative_accounts",
      underscored: true,
      timestamps: true,
      indexes: [{ fields: ["user_id", "kind"] }],
    }
  );
};