import {
  boolean,
  doublePrecision,
  index,
  jsonb,
  pgTable,
  text,
  timestamp,
  uuid,
} from "drizzle-orm/pg-core";
import {
  createdAt,
  memoryKindEnum,
  signalOriginEnum,
  tasteAxisEnum,
  updatedAt,
} from "./_shared.js";
import { households } from "./households.js";
import { recipes } from "./recipes.js";
import { users } from "./users.js";

/**
 * AAMOS Memory-spegel (spec §32). Varje post är transparent, korrigerbar,
 * pausbar och raderbar via "Vad plattformen vet om mig".
 * Personligt minne är INTE automatiskt träningsdata (spec §32, §33).
 */
export const memoryItems = pgTable(
  "memory_items",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    userId: uuid("user_id").references(() => users.id, { onDelete: "cascade" }),
    householdId: uuid("household_id").references(() => households.id, { onDelete: "cascade" }),
    kind: memoryKindEnum("kind").notNull(),
    key: text("key").notNull(),
    summarySv: text("summary_sv").notNull(),
    value: jsonb("value"),
    origin: signalOriginEnum("origin").notNull(),
    confidence: doublePrecision("confidence").notNull().default(0.5),
    verifiedByUser: boolean("verified_by_user").notNull().default(false),
    paused: boolean("paused").notNull().default(false),
    lastUsedAt: timestamp("last_used_at", { withTimezone: true }),
    expiresAt: timestamp("expires_at", { withTimezone: true }),
    createdAt: createdAt(),
    updatedAt: updatedAt(),
  },
  (t) => [
    index("memory_items_user_idx").on(t.userId, t.kind),
    index("memory_items_household_idx").on(t.householdId, t.kind),
    index("memory_items_key_idx").on(t.key),
  ],
);

/** Smaksignaler (spec §30): skilj user_stated / observed / ai_inferred. */
export const tasteSignals = pgTable(
  "taste_signals",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    userId: uuid("user_id")
      .notNull()
      .references(() => users.id, { onDelete: "cascade" }),
    axis: tasteAxisEnum("axis").notNull(),
    direction: doublePrecision("direction").notNull(),
    strength: doublePrecision("strength").notNull().default(0.5),
    origin: signalOriginEnum("origin").notNull(),
    refRecipeId: uuid("ref_recipe_id").references(() => recipes.id, { onDelete: "set null" }),
    createdAt: createdAt(),
  },
  (t) => [index("taste_signals_user_idx").on(t.userId, t.axis)],
);

/** Food Memory (spec §39): långsiktiga matminnen kopplade till högtider och betyg. */
export const foodMemories = pgTable(
  "food_memories",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    userId: uuid("user_id").references(() => users.id, { onDelete: "cascade" }),
    householdId: uuid("household_id").references(() => households.id, { onDelete: "cascade" }),
    recipeId: uuid("recipe_id").references(() => recipes.id, { onDelete: "set null" }),
    titleSv: text("title_sv").notNull(),
    summarySv: text("summary_sv").notNull(),
    holidayTag: text("holiday_tag"),
    photoUrl: text("photo_url"),
    stars: doublePrecision("stars"),
    occurredAt: timestamp("occurred_at", { withTimezone: true }).notNull(),
    createdAt: createdAt(),
  },
  (t) => [
    index("food_memories_household_idx").on(t.householdId, t.occurredAt),
    index("food_memories_holiday_idx").on(t.holidayTag),
  ],
);
