import {
  doublePrecision,
  index,
  integer,
  jsonb,
  pgTable,
  text,
  timestamp,
  uuid,
} from "drizzle-orm/pg-core";
import { createdAt, jobStatusEnum, jobTypeEnum, scanTypeEnum, updatedAt } from "./_shared.js";
import { households } from "./households.js";
import { users } from "./users.js";

/**
 * Asynkrona skannings-/AI-jobb (spec §50, §54):
 * App → signed S3 upload → API job → worker → AAMOS → result → app.
 */
export const scanJobs = pgTable(
  "scan_jobs",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    userId: uuid("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    householdId: uuid("household_id").references(() => households.id, { onDelete: "set null" }),
    scanType: scanTypeEnum("scan_type").notNull(),
    jobType: jobTypeEnum("job_type").notNull(),
    status: jobStatusEnum("status").notNull().default("queued"),
    s3Keys: text("s3_keys").array().notNull().default([]),
    context: jsonb("context"),
    /** Strukturerat AI-resultat validerat mot ai-contracts innan lagring. */
    result: jsonb("result"),
    error: text("error"),
    modelVersion: text("model_version"),
    promptVersion: text("prompt_version"),
    latencyMs: integer("latency_ms"),
    costUsd: doublePrecision("cost_usd"),
    attempts: integer("attempts").notNull().default(0),
    completedAt: timestamp("completed_at", { withTimezone: true }),
    createdAt: createdAt(),
    updatedAt: updatedAt(),
  },
  (t) => [
    index("scan_jobs_user_idx").on(t.userId, t.createdAt),
    index("scan_jobs_status_idx").on(t.status),
  ],
);

/**
 * Verifierade korrigeringar (spec §33): AI sa X, användaren sa Y.
 * Grund för träningsdata – används ENDAST enligt samtycke.
 */
export const aiCorrections = pgTable(
  "ai_corrections",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    scanJobId: uuid("scan_job_id").references(() => scanJobs.id, { onDelete: "set null" }),
    userId: uuid("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    taskType: text("task_type").notNull(),
    aiOutput: jsonb("ai_output").notNull(),
    userCorrection: jsonb("user_correction").notNull(),
    modelVersion: text("model_version"),
    promptVersion: text("prompt_version"),
    /** Snapshot av samtyckesläget när korrigeringen skapades. */
    consentSnapshot: jsonb("consent_snapshot").notNull(),
    exportedToTraining: timestamp("exported_to_training", { withTimezone: true }),
    trainingBatchId: text("training_batch_id"),
    createdAt: createdAt(),
  },
  (t) => [index("ai_corrections_task_idx").on(t.taskType, t.createdAt)],
);
