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

module.exports = (sequelize) => {
  const BotEvent = sequelize.define(
    "BotEvent",
    {
      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" },

      type: {
        type: DataTypes.ENUM("tick", "candle", "order_update", "command", "risk", "error", "log"),
        allowNull: false,
      },

      payload: { type: DataTypes.JSON, allowNull: false },
    },
    {
      tableName: "bot_events",
      underscored: true,
      timestamps: true,
      indexes: [
        { fields: ["bot_instance_id"] },
        { fields: ["type"] },
        { fields: ["created_at"] },
      ],
    }
  );

  return BotEvent;
};
