CREATE TYPE "public"."analysis_decision" AS ENUM('FBA', 'FBM', 'BUY', 'WATCH', 'SKIP');--> statement-breakpoint CREATE TYPE "public"."analysis_method" AS ENUM('llm', 'supplier_scoring');--> statement-breakpoint CREATE TYPE "public"."run_status" AS ENUM('running', 'ok', 'empty', 'failed', 'completed');--> statement-breakpoint CREATE TYPE "public"."run_type" AS ENUM('lead_analysis', 'category_analysis', 'supplier_upc', 'stalker', 'stalker_analysis');--> statement-breakpoint CREATE TABLE "analysis_revisions" ( "id" serial PRIMARY KEY NOT NULL, "run_item_id" integer NOT NULL, "observation_id" integer, "method" "analysis_method" NOT NULL, "decision" "analysis_decision" NOT NULL, "confidence" real, "reasoning" text, "analyzed_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "analysis_run_stats" ( "run_id" integer PRIMARY KEY NOT NULL, "processed_count" integer DEFAULT 0 NOT NULL, "analyzed_count" integer DEFAULT 0 NOT NULL, "available_count" integer DEFAULT 0 NOT NULL, "fba_count" integer DEFAULT 0 NOT NULL, "fbm_count" integer DEFAULT 0 NOT NULL, "buy_count" integer DEFAULT 0 NOT NULL, "watch_count" integer DEFAULT 0 NOT NULL, "skip_count" integer DEFAULT 0 NOT NULL ); --> statement-breakpoint CREATE TABLE "category_run_details" ( "run_id" integer PRIMARY KEY NOT NULL, "category_id" integer NOT NULL, "category_label" text NOT NULL, "checked_asin_count" integer DEFAULT 0 NOT NULL, "selection_parameters_json" text ); --> statement-breakpoint CREATE TABLE "product_identifiers" ( "id" serial PRIMARY KEY NOT NULL, "product_asin" text NOT NULL, "identifier_type" text NOT NULL, "identifier_value" text NOT NULL, "source" text NOT NULL, "confirmed_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "uq_product_identifier_type_value" UNIQUE("identifier_type","identifier_value") ); --> statement-breakpoint CREATE TABLE "product_observations" ( "id" serial PRIMARY KEY NOT NULL, "product_asin" text NOT NULL, "run_id" integer NOT NULL, "source" text NOT NULL, "marketplace" text DEFAULT 'US' NOT NULL, "current_price" real, "avg_price_90d" real, "sales_rank" integer, "sales_rank_avg_90d" integer, "monthly_sold" integer, "rank_drops_30d" integer, "rank_drops_90d" integer, "seller_count" integer, "amazon_is_seller" boolean, "amazon_buybox_share_pct_90d" real, "fba_fee" real, "fbm_fee" real, "referral_percent" real, "can_sell" boolean, "sellability_status" text, "sellability_reason" text, "raw_product_json" text, "fetched_at" timestamp with time zone NOT NULL ); --> statement-breakpoint CREATE TABLE "products" ( "asin" text PRIMARY KEY NOT NULL, "name" text, "brand" text, "category" text, "metadata_fetched_at" timestamp with time zone, "first_seen_at" timestamp with time zone DEFAULT now() NOT NULL, "last_seen_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "ck_products_asin" CHECK ("products"."asin" ~ '^[A-Z0-9]{10}$') ); --> statement-breakpoint CREATE TABLE "run_items" ( "id" serial PRIMARY KEY NOT NULL, "run_id" integer NOT NULL, "product_asin" text, "source_inventory_item_id" integer, "ordinal" integer, "source_row" integer, "status" text DEFAULT 'completed' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "runs" ( "id" serial PRIMARY KEY NOT NULL, "type" "run_type" NOT NULL, "parent_run_id" integer, "input_file" text, "output_file" text, "status" "run_status" DEFAULT 'running' NOT NULL, "error_message" text, "started_at" timestamp with time zone DEFAULT now() NOT NULL, "completed_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "sellers" ( "seller_id" text PRIMARY KEY NOT NULL, "seller_name" text, "rating" real, "rating_count" integer, "storefront_asin_total" integer, "persisted_inventory_sample_count" integer, "last_updated_at" timestamp with time zone NOT NULL, "raw_seller_json" text ); --> statement-breakpoint CREATE TABLE "sourcing_inputs" ( "run_item_id" integer PRIMARY KEY NOT NULL, "supplied_name" text, "supplied_brand" text, "supplied_category" text, "unit_cost" real, "avg_price_90d_sheet" real, "selling_price_sheet" real, "fba_net_sheet" real, "gross_profit_dollar" real, "gross_profit_pct" real, "net_profit_sheet" real, "roi_sheet" real, "moq" integer, "moq_cost" real, "qty_available" integer, "supplier" text, "source_url" text, "asin_link" text, "promo_coupon_code" text, "notes" text, "lead_date" text ); --> statement-breakpoint CREATE TABLE "stalker_inventory_items" ( "id" serial PRIMARY KEY NOT NULL, "run_id" integer NOT NULL, "seller_id" text NOT NULL, "product_asin" text NOT NULL, "observation_id" integer NOT NULL, "last_seen_at" timestamp with time zone NOT NULL, "raw_inventory_json" text, CONSTRAINT "uq_stalker_inventory_items_run_seller_asin" UNIQUE("run_id","seller_id","product_asin") ); --> statement-breakpoint CREATE TABLE "stalker_run_details" ( "run_id" integer PRIMARY KEY NOT NULL, "requested_asins" integer DEFAULT 0 NOT NULL, "skipped_asins" integer DEFAULT 0 NOT NULL, "scanned_asins" integer DEFAULT 0 NOT NULL, "source_asins_with_matches" integer DEFAULT 0 NOT NULL, "candidate_sellers" integer DEFAULT 0 NOT NULL, "qualifying_sellers" integer DEFAULT 0 NOT NULL, "matched_sellers" integer DEFAULT 0 NOT NULL, "seller_metadata_requests" integer DEFAULT 0 NOT NULL, "seller_storefront_requests" integer DEFAULT 0 NOT NULL, "inventory_sellability_checked_asins" integer DEFAULT 0 NOT NULL, "inventory_sellability_available_asins" integer DEFAULT 0 NOT NULL, "inventory_sellability_excluded_asins" integer DEFAULT 0 NOT NULL, "persisted_inventory_asins" integer DEFAULT 0 NOT NULL ); --> statement-breakpoint CREATE TABLE "stalker_scan_sellers" ( "id" serial PRIMARY KEY NOT NULL, "scan_id" integer NOT NULL, "seller_id" text NOT NULL, "offer_price" real, "condition" text, "is_fba" boolean, "stock" integer, "seller_rating" real, "seller_rating_count" integer, "raw_offer_json" text, CONSTRAINT "uq_stalker_scan_sellers_scan_seller" UNIQUE("scan_id","seller_id") ); --> statement-breakpoint CREATE TABLE "stalker_scans" ( "id" serial PRIMARY KEY NOT NULL, "run_id" integer NOT NULL, "source_product_asin" text NOT NULL, "observation_id" integer, "offer_count" integer DEFAULT 0 NOT NULL, "candidate_seller_count" integer DEFAULT 0 NOT NULL, "matched_seller_count" integer DEFAULT 0 NOT NULL, "fetched_at" timestamp with time zone NOT NULL, CONSTRAINT "uq_stalker_scans_run_source_product" UNIQUE("run_id","source_product_asin") ); --> statement-breakpoint CREATE TABLE "supplier_scores" ( "revision_id" integer PRIMARY KEY NOT NULL, "score" real, "sale_price" real, "fba_fee" real, "profit" real, "margin" real, "roi" real, "reason" text ); --> statement-breakpoint CREATE TABLE "upc_resolution_candidates" ( "run_item_id" integer NOT NULL, "product_asin" text NOT NULL, CONSTRAINT "upc_resolution_candidates_run_item_id_product_asin_pk" PRIMARY KEY("run_item_id","product_asin") ); --> statement-breakpoint CREATE TABLE "upc_resolutions" ( "run_item_id" integer PRIMARY KEY NOT NULL, "requested_upc" text NOT NULL, "normalized_upc" text NOT NULL, "provider" text NOT NULL, "status" text NOT NULL, "reason" text, "resolved_product_asin" text, "resolved_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint ALTER TABLE "analysis_revisions" ADD CONSTRAINT "analysis_revisions_run_item_id_run_items_id_fk" FOREIGN KEY ("run_item_id") REFERENCES "public"."run_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "analysis_revisions" ADD CONSTRAINT "analysis_revisions_observation_id_product_observations_id_fk" FOREIGN KEY ("observation_id") REFERENCES "public"."product_observations"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "analysis_run_stats" ADD CONSTRAINT "analysis_run_stats_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "category_run_details" ADD CONSTRAINT "category_run_details_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "product_identifiers" ADD CONSTRAINT "product_identifiers_product_asin_products_asin_fk" FOREIGN KEY ("product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "product_observations" ADD CONSTRAINT "product_observations_product_asin_products_asin_fk" FOREIGN KEY ("product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "product_observations" ADD CONSTRAINT "product_observations_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "run_items" ADD CONSTRAINT "run_items_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "run_items" ADD CONSTRAINT "run_items_product_asin_products_asin_fk" FOREIGN KEY ("product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "run_items" ADD CONSTRAINT "run_items_source_inventory_item_id_stalker_inventory_items_id_fk" FOREIGN KEY ("source_inventory_item_id") REFERENCES "public"."stalker_inventory_items"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "runs" ADD CONSTRAINT "runs_parent_run_id_runs_id_fk" FOREIGN KEY ("parent_run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sourcing_inputs" ADD CONSTRAINT "sourcing_inputs_run_item_id_run_items_id_fk" FOREIGN KEY ("run_item_id") REFERENCES "public"."run_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_inventory_items" ADD CONSTRAINT "stalker_inventory_items_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_inventory_items" ADD CONSTRAINT "stalker_inventory_items_seller_id_sellers_seller_id_fk" FOREIGN KEY ("seller_id") REFERENCES "public"."sellers"("seller_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_inventory_items" ADD CONSTRAINT "stalker_inventory_items_product_asin_products_asin_fk" FOREIGN KEY ("product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_inventory_items" ADD CONSTRAINT "stalker_inventory_items_observation_id_product_observations_id_fk" FOREIGN KEY ("observation_id") REFERENCES "public"."product_observations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_run_details" ADD CONSTRAINT "stalker_run_details_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_scan_sellers" ADD CONSTRAINT "stalker_scan_sellers_scan_id_stalker_scans_id_fk" FOREIGN KEY ("scan_id") REFERENCES "public"."stalker_scans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_scan_sellers" ADD CONSTRAINT "stalker_scan_sellers_seller_id_sellers_seller_id_fk" FOREIGN KEY ("seller_id") REFERENCES "public"."sellers"("seller_id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_scans" ADD CONSTRAINT "stalker_scans_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_scans" ADD CONSTRAINT "stalker_scans_source_product_asin_products_asin_fk" FOREIGN KEY ("source_product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "stalker_scans" ADD CONSTRAINT "stalker_scans_observation_id_product_observations_id_fk" FOREIGN KEY ("observation_id") REFERENCES "public"."product_observations"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint ALTER TABLE "supplier_scores" ADD CONSTRAINT "supplier_scores_revision_id_analysis_revisions_id_fk" FOREIGN KEY ("revision_id") REFERENCES "public"."analysis_revisions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "upc_resolution_candidates" ADD CONSTRAINT "upc_resolution_candidates_run_item_id_upc_resolutions_run_item_id_fk" FOREIGN KEY ("run_item_id") REFERENCES "public"."upc_resolutions"("run_item_id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "upc_resolution_candidates" ADD CONSTRAINT "upc_resolution_candidates_product_asin_products_asin_fk" FOREIGN KEY ("product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "upc_resolutions" ADD CONSTRAINT "upc_resolutions_run_item_id_run_items_id_fk" FOREIGN KEY ("run_item_id") REFERENCES "public"."run_items"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "upc_resolutions" ADD CONSTRAINT "upc_resolutions_resolved_product_asin_products_asin_fk" FOREIGN KEY ("resolved_product_asin") REFERENCES "public"."products"("asin") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE INDEX "idx_analysis_revisions_run_item_time" ON "analysis_revisions" USING btree ("run_item_id","analyzed_at");--> statement-breakpoint CREATE INDEX "idx_analysis_revisions_decision" ON "analysis_revisions" USING btree ("decision");--> statement-breakpoint CREATE INDEX "idx_product_identifiers_asin" ON "product_identifiers" USING btree ("product_asin");--> statement-breakpoint CREATE INDEX "idx_product_observations_product_time" ON "product_observations" USING btree ("product_asin","fetched_at" DESC NULLS LAST);--> statement-breakpoint CREATE INDEX "idx_product_observations_run_id" ON "product_observations" USING btree ("run_id");--> statement-breakpoint CREATE INDEX "idx_product_observations_sellability" ON "product_observations" USING btree ("sellability_status");--> statement-breakpoint CREATE INDEX "idx_products_name" ON "products" USING btree ("name");--> statement-breakpoint CREATE INDEX "idx_products_last_seen_at" ON "products" USING btree ("last_seen_at");--> statement-breakpoint CREATE INDEX "idx_run_items_run_id" ON "run_items" USING btree ("run_id");--> statement-breakpoint CREATE INDEX "idx_run_items_product_asin" ON "run_items" USING btree ("product_asin");--> statement-breakpoint CREATE INDEX "idx_runs_started_at" ON "runs" USING btree ("started_at");--> statement-breakpoint CREATE INDEX "idx_runs_type" ON "runs" USING btree ("type");--> statement-breakpoint CREATE INDEX "idx_runs_status" ON "runs" USING btree ("status");--> statement-breakpoint CREATE INDEX "idx_runs_parent_run_id" ON "runs" USING btree ("parent_run_id");--> statement-breakpoint CREATE INDEX "idx_stalker_inventory_seller_id" ON "stalker_inventory_items" USING btree ("seller_id");--> statement-breakpoint CREATE INDEX "idx_stalker_inventory_product_asin" ON "stalker_inventory_items" USING btree ("product_asin");--> statement-breakpoint CREATE INDEX "idx_stalker_scans_run_id" ON "stalker_scans" USING btree ("run_id");--> statement-breakpoint CREATE INDEX "idx_stalker_scans_source_asin" ON "stalker_scans" USING btree ("source_product_asin");--> statement-breakpoint CREATE INDEX "idx_upc_candidates_product_asin" ON "upc_resolution_candidates" USING btree ("product_asin");--> statement-breakpoint CREATE INDEX "idx_upc_resolutions_normalized_upc" ON "upc_resolutions" USING btree ("normalized_upc");