import {
  pgTable,
  text,
  timestamp,
  uuid,
  date,
  jsonb,
  index,
} from "drizzle-orm/pg-core";
import { families } from "./family";
import { children } from "./family";
import { memories } from "./media";

// ---------------------------------------------------------------------------
// Wardrobe schema
//
// RLS NOTE: garments and garment_wears carry a `family_isolation` policy in
// prod (see drizzle/manual/06-wardrobe-rls.sql). Policies are not modelled
// in these pgTable definitions — they live in the DB and are managed
// separately. Do not assume Drizzle will recreate them.
// ---------------------------------------------------------------------------

export const garments = pgTable(
  "garments",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    familyId: uuid("family_id")
      .notNull()
      .references(() => families.id),
    childId: uuid("child_id")
      .notNull()
      .references(() => children.id),
    name: text("name"),
    category: text("category").notNull(),
    sizeLabel: text("size_label").notNull(),
    colors: text("colors").array().default([]),
    seasons: text("seasons").array().default([]),
    occasionTags: text("occasion_tags").array().default([]),
    imageKey: text("image_key").notNull(),
    thumbKey: text("thumb_key").notNull(),
    status: text("status").notNull().default("active"),
    acquiredVia: text("acquired_via"),
    giftFrom: text("gift_from"),
    visionMetadata: jsonb("vision_metadata"),
    createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true }).defaultNow().notNull(),
  },
  (table) => [
    index("garments_family_idx").on(table.familyId),
    index("garments_child_idx").on(table.childId),
    index("garments_status_idx").on(table.status),
  ]
);

export const garmentWears = pgTable(
  "garment_wears",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    familyId: uuid("family_id")
      .notNull()
      .references(() => families.id),
    garmentId: uuid("garment_id")
      .notNull()
      .references(() => garments.id, { onDelete: "cascade" }),
    wornOn: date("worn_on").notNull(),
    memoryId: uuid("memory_id").references(() => memories.id),
    createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
  },
  (table) => [
    index("garment_wears_garment_idx").on(table.garmentId),
    index("garment_wears_family_idx").on(table.familyId),
  ]
);

export type Garment = typeof garments.$inferSelect;
export type NewGarment = typeof garments.$inferInsert;
export type GarmentWear = typeof garmentWears.$inferSelect;
export type NewGarmentWear = typeof garmentWears.$inferInsert;

export const GARMENT_CATEGORIES = [
  "onesie",
  "top",
  "bottom",
  "dress",
  "outerwear",
  "sleepwear",
  "accessory",
] as const;

export type GarmentCategory = (typeof GARMENT_CATEGORIES)[number];

export const GARMENT_SIZE_ORDER = [
  "preemie",
  "newborn",
  "0-3m",
  "3-6m",
  "6-9m",
  "9-12m",
  "12-18m",
  "18-24m",
  "2-3y",
  "3-4y",
  "4-5y",
] as const;

export type GarmentSizeLabel = (typeof GARMENT_SIZE_ORDER)[number];

// W9: Saved outfits
export const outfits = pgTable(
  "outfits",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    familyId: uuid("family_id")
      .notNull()
      .references(() => families.id),
    childId: uuid("child_id")
      .notNull()
      .references(() => children.id),
    name: text("name").notNull(),
    garmentIds: uuid("garment_ids").array().notNull().default([]),
    occasionTags: text("occasion_tags").array().default([]),
    createdAt: timestamp("created_at", { withTimezone: true }).defaultNow().notNull(),
  },
  (table) => [
    index("outfits_family_idx").on(table.familyId),
    index("outfits_child_idx").on(table.childId),
  ]
);

export type Outfit = typeof outfits.$inferSelect;
export type NewOutfit = typeof outfits.$inferInsert;
