import { sqliteTable, text, integer, real } from "drizzle-orm/sqlite-core";
import { alertRules } from "./alert_rules";
import { users } from "./users";

export const alerts = sqliteTable("alerts", {
  id: integer("id").primaryKey({ autoIncrement: true }),
  ruleId: integer("rule_id").notNull().references(() => alertRules.id),
  kpiKey: text("kpi_key").notNull(),
  kpiValue: real("kpi_value").notNull(),
  threshold: real("threshold").notNull(),
  severity: text("severity", { enum: ["low", "medium", "high", "critical"] }).notNull(),
  message: text("message").notNull(),
  wasSent: integer("was_sent", { mode: "boolean" }).notNull().default(false),
  sentAt: integer("sent_at", { mode: "timestamp" }),
  acknowledgedBy: integer("acknowledged_by").references(() => users.id),
  acknowledgedAt: integer("acknowledged_at", { mode: "timestamp" }),
  createdAt: integer("created_at", { mode: "timestamp" }).notNull().default(new Date()),
});

export type Alert = typeof alerts.$inferSelect;
export type NewAlert = typeof alerts.$inferInsert;
