import {
  pgTable,
  pgEnum,
  uuid,
  text,
  integer,
  timestamp,
} from "drizzle-orm/pg-core";
import { users } from "./users";
import { mediaItems } from "./media";

export const watchlistSourceEnum = pgEnum("watchlist_source", ["plex", "manual"]);
export const watchingNowScopeEnum = pgEnum("watching_now_scope", ["show", "season"]);

// Items synced from Plex watchlists or added manually.
// Default behavior: archive-tier 720p.
export const watchlistItems = pgTable("watchlist_items", {
  id: uuid("id").primaryKey().defaultRandom(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  mediaItemId: uuid("media_item_id").notNull().references(() => mediaItems.id, { onDelete: "cascade" }),
  source: watchlistSourceEnum("source").notNull().default("plex"),
  addedAt: timestamp("added_at").notNull().defaultNow(),
  removedAt: timestamp("removed_at"),
});

// Active watching intent. Drives promotion to live storage.
// Each user has a slot quota (5 TV, 10 movies by default).
export const watchingNowItems = pgTable("watching_now_items", {
  id: uuid("id").primaryKey().defaultRandom(),
  userId: uuid("user_id").notNull().references(() => users.id, { onDelete: "cascade" }),
  mediaItemId: uuid("media_item_id").notNull().references(() => mediaItems.id, { onDelete: "cascade" }),
  scope: watchingNowScopeEnum("scope").notNull().default("show"),
  // Set when scope is "season".
  seasonNumber: integer("season_number"),
  slotNumber: integer("slot_number"),
  addedAt: timestamp("added_at").notNull().defaultNow(),
  removedAt: timestamp("removed_at"),
});

// Admin-managed list of movies that remain live permanently regardless of age.
export const classics = pgTable("classics", {
  id: uuid("id").primaryKey().defaultRandom(),
  mediaItemId: uuid("media_item_id").notNull().references(() => mediaItems.id, { onDelete: "cascade" }).unique(),
  note: text("note"),
  addedBy: uuid("added_by").references(() => users.id),
  addedAt: timestamp("added_at").notNull().defaultNow(),
});
