// models/WatchlistAlert.js
const { DataTypes } = require("sequelize");

module.exports = (sequelize) => {
  const WatchlistAlert = sequelize.define(
    "WatchlistAlert",
    {
      id: {
        type: DataTypes.BIGINT.UNSIGNED,
        primaryKey: true,
        autoIncrement: true,
      },
      userId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: "user_id",
      },
      watchlistId: {
        type: DataTypes.BIGINT.UNSIGNED,
        allowNull: false,
        field: "watchlist_id",
      },
      symbol: {
        type: DataTypes.STRING(32),
        allowNull: false,
      },
      // "above" | "below"
      type: {
        type: DataTypes.ENUM("above", "below"),
        allowNull: false,
        defaultValue: "above",
      },
      targetPrice: {
        type: DataTypes.DECIMAL(24, 8),
        allowNull: false,
        field: "target_price",
      },
      active: {
        type: DataTypes.BOOLEAN,
        allowNull: false,
        defaultValue: true,
      },
      triggeredAt: {
        type: DataTypes.DATE,
        allowNull: true,
        field: "triggered_at",
      },
    },
    {
      tableName: "watchlist_alerts",
      underscored: true,
      indexes: [
        { fields: ["user_id"] },
        { fields: ["watchlist_id"] },
        { fields: ["symbol"] },
        { fields: ["active"] },
      ],
    }
  );


  return WatchlistAlert;
};