import { pgTable, text, timestamp, index, uuid, jsonb } from "drizzle-orm/pg-core";
import { users } from "./auth";
import { reportType, reportStatus } from "./enums";

export const securityReports = pgTable("security_reports", {
  id: uuid("id").defaultRandom().primaryKey(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  subscriptionId: uuid("subscription_id").notNull(),
  reportType: reportType("report_type").notNull(),
  status: reportStatus("status").default("PENDING").notNull(),
  periodStart: timestamp("period_start", { withTimezone: true, mode: "date" }).notNull(),
  periodEnd: timestamp("period_end", { withTimezone: true, mode: "date" }).notNull(),
  title: text("title").notNull(),
  summary: text("summary"),
  htmlContent: text("html_content"),
  pdfUrl: text("pdf_url"),
  dataPayload: jsonb("data_payload"),
  error: text("error"),
  scheduledFor: timestamp("scheduled_for", { withTimezone: true, mode: "date" }),
  deliveredAt: timestamp("delivered_at", { withTimezone: true, mode: "date" }),
  createdAt: timestamp("created_at", { withTimezone: true, mode: "date" }).defaultNow().notNull(),
  updatedAt: timestamp("updated_at", { withTimezone: true, mode: "date" }).defaultNow().notNull().$onUpdate(() => new Date()),
}, (table) => ({
  userIdIdx: index("security_reports_user_id_idx").on(table.userId),
  subscriptionIdIdx: index("security_reports_subscription_id_idx").on(table.subscriptionId),
  reportTypeIdx: index("security_reports_report_type_idx").on(table.reportType),
  statusIdx: index("security_reports_status_idx").on(table.status),
  periodIdx: index("security_reports_period_idx").on(table.periodStart, table.periodEnd),
  createdAtIdx: index("security_reports_created_at_idx").on(table.createdAt),
}));
