"use strict";
const { DataTypes } = require("sequelize");
const { generateRandomString } = require("../utils/helpers");

module.exports = (sequelize) => {
    const BotPosition = sequelize.define(
        "BotPosition",
        {
            id: { type: DataTypes.BIGINT.UNSIGNED, autoIncrement: true, primaryKey: true },
            reference: { type: DataTypes.STRING(20), allowNull: false, defaultValue: () => generateRandomString(16) },
            botInstanceId: { type: DataTypes.BIGINT.UNSIGNED, allowNull: false, field: "bot_instance_id" },
            symbol: { type: DataTypes.STRING(30), allowNull: false },

            netQty: { type: DataTypes.DECIMAL(30, 18), allowNull: false, defaultValue: 0, field: "net_qty" },
            avgEntry: { type: DataTypes.DECIMAL(30, 18), allowNull: true, field: "avg_entry" },

            realizedPnl: { type: DataTypes.DECIMAL(30, 18), allowNull: false, defaultValue: 0, field: "realized_pnl" },
            unrealizedPnl: { type: DataTypes.DECIMAL(30, 18), allowNull: false, defaultValue: 0, field: "unrealized_pnl" },
        },
        {
            tableName: "bot_positions",
            underscored: true,
            timestamps: true,
            indexes: [{ unique: true, fields: ["bot_instance_id", "symbol"] }],
        }
    );

    return BotPosition;
};
