import { pgTable, text, timestamp, index, uuid, boolean } from "drizzle-orm/pg-core";
import { users } from "./auth";
import { subscriptions } from "./subscription";
import { exposures } from "./darkwatch";
import { alertType, alertSeverity, alertChannel } from "./enums";

export const alerts = pgTable("alerts", {
  id: uuid("id").defaultRandom().primaryKey(),
  subscriptionId: uuid("subscription_id").notNull().references(() => subscriptions.id, { onDelete: "cascade" }),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  exposureId: uuid("exposure_id").references(() => exposures.id),
  type: alertType("type").notNull(),
  title: text("title").notNull(),
  message: text("message").notNull(),
  severity: alertSeverity("severity").default("info").notNull(),
  isRead: boolean("is_read").default(false).notNull(),
  readAt: timestamp("read_at", { withTimezone: true, mode: "date" }),
  channel: alertChannel("channel").array().notNull(),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  subscriptionIdIdx: index("alerts_subscription_id_idx").on(table.subscriptionId),
  userIdIdx: index("alerts_user_id_idx").on(table.userId),
  isReadIdx: index("alerts_is_read_idx").on(table.isRead),
  createdAtIdx: index("alerts_created_at_idx").on(table.createdAt),
}));
