CREATE TYPE "public"."grab_source" AS ENUM('auto', 'manual');--> statement-breakpoint
CREATE TYPE "public"."grab_status" AS ENUM('queued', 'downloading', 'completed', 'importing', 'imported', 'failed', 'orphaned');--> statement-breakpoint
CREATE TABLE "grabs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_item_id" uuid NOT NULL,
	"season_number" integer,
	"episode_number" integer,
	"info_hash" text NOT NULL,
	"release_title" text NOT NULL,
	"size_bytes" bigint,
	"magnet_url" text,
	"download_url" text,
	"indexer_name" text NOT NULL,
	"origin" text,
	"seeders" integer,
	"quality" "quality",
	"score" integer,
	"score_reasons" jsonb DEFAULT '[]'::jsonb,
	"source" "grab_source" NOT NULL,
	"requested_by" uuid,
	"status" "grab_status" DEFAULT 'queued' NOT NULL,
	"status_detail" text,
	"progress" integer DEFAULT 0,
	"content_path" text,
	"imported_path" text,
	"imported_at" timestamp,
	"created_at" timestamp DEFAULT now() NOT NULL,
	"updated_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "release_blocklist" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"info_hash" text NOT NULL,
	"release_title" text,
	"media_item_id" uuid,
	"reason" text,
	"permanent" boolean DEFAULT false NOT NULL,
	"created_by" uuid,
	"created_at" timestamp DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "search_runs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"media_item_id" uuid,
	"season_number" integer,
	"episode_number" integer,
	"source" "grab_source" NOT NULL,
	"query" text,
	"indexers_queried" integer DEFAULT 0 NOT NULL,
	"results_found" integer DEFAULT 0 NOT NULL,
	"results_accepted" integer DEFAULT 0 NOT NULL,
	"skipped" jsonb DEFAULT '[]'::jsonb,
	"errors" jsonb DEFAULT '[]'::jsonb,
	"grab_id" uuid,
	"decision" text,
	"started_at" timestamp DEFAULT now() NOT NULL,
	"duration_ms" integer
);
--> statement-breakpoint
ALTER TABLE "grabs" ADD CONSTRAINT "grabs_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 "grabs" ADD CONSTRAINT "grabs_requested_by_users_id_fk" FOREIGN KEY ("requested_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "release_blocklist" ADD CONSTRAINT "release_blocklist_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 "release_blocklist" ADD CONSTRAINT "release_blocklist_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 "search_runs" ADD CONSTRAINT "search_runs_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 "search_runs" ADD CONSTRAINT "search_runs_grab_id_grabs_id_fk" FOREIGN KEY ("grab_id") REFERENCES "public"."grabs"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "grabs_info_hash_idx" ON "grabs" USING btree ("info_hash");--> statement-breakpoint
CREATE INDEX "grabs_status_idx" ON "grabs" USING btree ("status");--> statement-breakpoint
CREATE INDEX "grabs_media_item_idx" ON "grabs" USING btree ("media_item_id","season_number","episode_number");--> statement-breakpoint
CREATE UNIQUE INDEX "release_blocklist_info_hash_idx" ON "release_blocklist" USING btree ("info_hash");--> statement-breakpoint
CREATE INDEX "search_runs_media_item_idx" ON "search_runs" USING btree ("media_item_id","started_at");