CREATE TYPE "public"."appeal_status" AS ENUM('PENDING', 'APPROVED', 'REJECTED');--> statement-breakpoint
CREATE TYPE "public"."llm_decision" AS ENUM('APPROVE', 'FLAG', 'REJECT');--> statement-breakpoint
CREATE TYPE "public"."llm_job_status" AS ENUM('PENDING', 'RUNNING', 'COMPLETED', 'FAILED', 'TIMED_OUT');--> statement-breakpoint
CREATE TYPE "public"."llm_job_type" AS ENUM('DEAL_MODERATION', 'IMAGE_APPROVAL', 'REVIEW_PRESCORING', 'VENDOR_VIOLATION');--> statement-breakpoint
CREATE TYPE "public"."llm_target_type" AS ENUM('DEAL', 'IMAGE', 'REVIEW_REMOVAL', 'VENDOR');--> statement-breakpoint
ALTER TYPE "public"."deal_state" ADD VALUE 'UNDER_REVIEW' BEFORE 'PENDING_APPROVAL';--> statement-breakpoint
CREATE TABLE "llm_jobs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"job_type" "llm_job_type" NOT NULL,
	"target_id" uuid NOT NULL,
	"target_type" "llm_target_type" NOT NULL,
	"status" "llm_job_status" DEFAULT 'PENDING' NOT NULL,
	"input_payload" jsonb NOT NULL,
	"output_payload" jsonb,
	"model_used" text,
	"prompt_snapshot" text,
	"decision" "llm_decision",
	"flag_reason" text,
	"started_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"retry_count" integer DEFAULT 0 NOT NULL,
	"last_error" text
);
--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "content_hash" text;--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "appeal_status" "appeal_status";--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "appeal_reason" text;--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "appeal_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "appeal_decided_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "deals" ADD COLUMN "sla_alerted_at" timestamp with time zone;--> statement-breakpoint
ALTER TABLE "vendors" ADD COLUMN "deal_violations_count" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
CREATE INDEX "llm_jobs_status_created_idx" ON "llm_jobs" USING btree ("status","created_at");--> statement-breakpoint
CREATE INDEX "llm_jobs_target_idx" ON "llm_jobs" USING btree ("target_id","job_type");--> statement-breakpoint
CREATE INDEX "deals_content_hash_idx" ON "deals" USING btree ("content_hash");