/**
 * Transaction-anomaly detection (#50 P2) — Drizzle declarations.
 *
 * These two tables are RUNTIME-CREATED in rls-init `applyBootSchemaMigrations`
 * (M66) — that is the authoritative create (prod does not run drizzle-kit push).
 * They are ALSO declared here so schema-conformance (which introspects the
 * Drizzle schema) can verify the tenant_id column for the standard TENANT_TABLES
 * RLS onboarding. This dual runtime-create + Drizzle-declaration mirrors the
 * fincrime_cases pattern (schema-persistence.ts).
 *
 * Column types here must match the M66 runtime DDL. Only tenant_id is
 * conformance-checked (Category 1: TENANT_TABLES × tenant_id).
 */

import { pgTable, varchar, text, integer, jsonb, timestamp } from "drizzle-orm/pg-core";

export const anomalyDetections = pgTable("anomaly_detections", {
  id: varchar("id").primaryKey(),
  tenantId: text("tenant_id").notNull(),
  userId: text("user_id").notNull(),
  kytResultId: varchar("kyt_result_id").notNull(),
  score: integer("score").notNull(),
  contributions: jsonb("contributions").notNull(),
  baselineSummary: jsonb("baseline_summary").notNull(),
  detectedAt: timestamp("detected_at", { withTimezone: true }).notNull().defaultNow(),
}).enableRLS();

export const anomalySweepState = pgTable("anomaly_sweep_state", {
  tenantId: text("tenant_id").primaryKey(),
  lastSweptAt: timestamp("last_swept_at", { withTimezone: true }).notNull().defaultNow(),
  txnsScanned: integer("txns_scanned").notNull().default(0),
  usersScanned: integer("users_scanned").notNull().default(0),
  usersInsufficient: integer("users_insufficient").notNull().default(0),
  detectionsFound: integer("detections_found").notNull().default(0),
}).enableRLS();
