CREATE TYPE "public"."external_id_source" AS ENUM('tvdb', 'tmdb', 'imdb', 'plex');--> statement-breakpoint
CREATE TYPE "public"."media_type" AS ENUM('tv_series', 'movie');--> statement-breakpoint
CREATE TYPE "public"."season_status" AS ENUM('upcoming', 'airing', 'complete');--> statement-breakpoint
CREATE TYPE "public"."series_status" AS ENUM('upcoming', 'continuing', 'ended');--> statement-breakpoint
CREATE TYPE "public"."watching_now_scope" AS ENUM('show', 'season');--> statement-breakpoint
CREATE TYPE "public"."watchlist_source" AS ENUM('plex', 'manual');--> statement-breakpoint
CREATE TYPE "public"."override_type" AS ENUM('force_live', 'force_archive', 'force_quality', 'force_monitor', 'force_ignore', 'temporary_promotion');--> statement-breakpoint
CREATE TYPE "public"."policy_trigger" AS ENUM('scheduled', 'manual', 'demand_change', 'metadata_refresh');--> statement-breakpoint
CREATE TYPE "public"."quality" AS ENUM('sd', '720p', '1080p', '4k');--> statement-breakpoint
CREATE TABLE "episodes" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"season_id" uuid NOT NULL,
	"episode_number" integer NOT NULL,
	"title" text,
	"overview" text,
	"air_date" date
);
--> statement-breakpoint
CREATE TABLE "external_ids" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_item_id" uuid NOT NULL,
	"source" "external_id_source" NOT NULL,
	"external_id" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "media_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_type" "media_type" NOT NULL,
	"title" text NOT NULL,
	"sort_title" text,
	"overview" text,
	"year" integer,
	"poster_path" text,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "movies" (
	"id" uuid PRIMARY KEY NOT NULL,
	"release_date" date,
	"runtime_minutes" integer,
	"status" text,
	"metadata_refreshed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "seasons" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"series_id" uuid NOT NULL,
	"season_number" integer NOT NULL,
	"title" text,
	"episode_count" integer,
	"air_date" date,
	"status" "season_status" DEFAULT 'upcoming' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "series" (
	"id" uuid PRIMARY KEY NOT NULL,
	"status" "series_status" DEFAULT 'upcoming' NOT NULL,
	"network" text,
	"first_air_date" date,
	"last_air_date" date,
	"is_currently_relevant" text,
	"metadata_refreshed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "classics" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_item_id" uuid NOT NULL,
	"note" text,
	"added_by" uuid,
	"added_at" timestamp DEFAULT now() NOT NULL,
	CONSTRAINT "classics_media_item_id_unique" UNIQUE("media_item_id")
);
--> statement-breakpoint
CREATE TABLE "watching_now_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"media_item_id" uuid NOT NULL,
	"scope" "watching_now_scope" DEFAULT 'show' NOT NULL,
	"season_number" integer,
	"added_at" timestamp DEFAULT now() NOT NULL,
	"removed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "watchlist_items" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"media_item_id" uuid NOT NULL,
	"source" "watchlist_source" DEFAULT 'plex' NOT NULL,
	"added_at" timestamp DEFAULT now() NOT NULL,
	"removed_at" timestamp
);
--> statement-breakpoint
CREATE TABLE "admin_overrides" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_item_id" uuid NOT NULL,
	"season_number" integer,
	"episode_number" integer,
	"override_type" "override_type" NOT NULL,
	"value" jsonb,
	"reason" text,
	"expires_at" timestamp,
	"created_by" uuid,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "desired_states" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"policy_run_id" uuid,
	"media_item_id" uuid NOT NULL,
	"season_number" integer,
	"episode_number" integer,
	"wanted" boolean DEFAULT false NOT NULL,
	"storage_tier" "storage_tier",
	"min_quality" "quality",
	"preferred_quality" "quality",
	"monitored" boolean DEFAULT false NOT NULL,
	"reason_codes" text[] DEFAULT '{}' NOT NULL,
	"computed_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "policy_runs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"trigger" "policy_trigger" NOT NULL,
	"started_at" timestamp DEFAULT now() NOT NULL,
	"completed_at" timestamp,
	"items_evaluated" integer,
	"items_changed" integer
);
--> statement-breakpoint
ALTER TABLE "episodes" ADD CONSTRAINT "episodes_season_id_seasons_id_fk" FOREIGN KEY ("season_id") REFERENCES "public"."seasons"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "external_ids" ADD CONSTRAINT "external_ids_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "movies" ADD CONSTRAINT "movies_id_media_items_id_fk" FOREIGN KEY ("id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "seasons" ADD CONSTRAINT "seasons_series_id_series_id_fk" FOREIGN KEY ("series_id") REFERENCES "public"."series"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "series" ADD CONSTRAINT "series_id_media_items_id_fk" FOREIGN KEY ("id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "classics" ADD CONSTRAINT "classics_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "classics" ADD CONSTRAINT "classics_added_by_users_id_fk" FOREIGN KEY ("added_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "watching_now_items" ADD CONSTRAINT "watching_now_items_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "watching_now_items" ADD CONSTRAINT "watching_now_items_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "watchlist_items" ADD CONSTRAINT "watchlist_items_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "watchlist_items" ADD CONSTRAINT "watchlist_items_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "admin_overrides" ADD CONSTRAINT "admin_overrides_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "admin_overrides" ADD CONSTRAINT "admin_overrides_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "desired_states" ADD CONSTRAINT "desired_states_policy_run_id_policy_runs_id_fk" FOREIGN KEY ("policy_run_id") REFERENCES "public"."policy_runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "desired_states" ADD CONSTRAINT "desired_states_media_item_id_media_items_id_fk" FOREIGN KEY ("media_item_id") REFERENCES "public"."media_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "external_ids_source_external_id_idx" ON "external_ids" USING btree ("source","external_id");