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');--> statement-breakpoint
CREATE TABLE "analysis_results" (
	"id" serial PRIMARY KEY NOT NULL,
	"run_id" integer NOT NULL,
	"asin" text NOT NULL,
	"product_name" text,
	"brand" text,
	"category" text,
	"upc" 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,
	"current_price" real,
	"avg_price_90d" real,
	"sales_rank" integer,
	"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" text,
	"sellability_status" text,
	"sellability_reason" text,
	"supplier_score" real,
	"supplier_profit" real,
	"supplier_margin" real,
	"supplier_roi" real,
	"supplier_reason" text,
	"upc_lookup_status" text,
	"upc_lookup_reason" text,
	"candidate_asins" text,
	"verdict" text NOT NULL,
	"confidence" real,
	"reasoning" text,
	"fetched_at" timestamp with time zone NOT NULL
);
--> statement-breakpoint
CREATE TABLE "category_product_results" (
	"id" serial PRIMARY KEY NOT NULL,
	"asin" text NOT NULL,
	"run_id" integer NOT NULL,
	"name" text NOT NULL,
	"brand" text,
	"category" text,
	"unit_cost" real,
	"current_price" real,
	"avg_price_90d" real,
	"avg_price_90d_sheet" real,
	"selling_price_sheet" real,
	"sales_rank" integer,
	"sales_rank_avg_90d" integer,
	"seller_count" integer,
	"amazon_is_seller" boolean,
	"amazon_buybox_share_pct_90d" real,
	"monthly_sold" integer,
	"rank_drops_30d" integer,
	"rank_drops_90d" integer,
	"fba_fee" real,
	"fbm_fee" real,
	"referral_percent" real,
	"can_sell" text,
	"sellability_status" text,
	"sellability_reason" text,
	"verdict" text NOT NULL,
	"confidence" real NOT NULL,
	"reasoning" text,
	"fetched_at" timestamp with time zone NOT NULL,
	CONSTRAINT "category_product_results_asin_unique" UNIQUE("asin")
);
--> statement-breakpoint
CREATE TABLE "runs" (
	"id" serial PRIMARY KEY NOT NULL,
	"type" "run_type" NOT NULL,
	"input_file" text,
	"output_file" text,
	"status" "run_status" DEFAULT 'running' NOT NULL,
	"error_message" text,
	"total_products" integer,
	"fba_count" integer,
	"fbm_count" integer,
	"skip_count" integer,
	"category_id" integer,
	"category_label" text,
	"top_asins_checked" integer,
	"available_asins" integer,
	"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 "stalker_asin_scans" (
	"id" serial PRIMARY KEY NOT NULL,
	"run_id" integer NOT NULL,
	"source_asin" text NOT NULL,
	"title" text,
	"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,
	"raw_product_json" text,
	CONSTRAINT "uq_stalker_scans_run_asin" UNIQUE("run_id","source_asin")
);
--> statement-breakpoint
CREATE TABLE "stalker_asin_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_asin_sellers_scan_seller" UNIQUE("scan_id","seller_id")
);
--> statement-breakpoint
CREATE TABLE "stalker_runs" (
	"id" serial PRIMARY KEY NOT NULL,
	"input_file" text NOT NULL,
	"started_at" timestamp with time zone NOT NULL,
	"completed_at" timestamp with time zone,
	"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,
	"status" text NOT NULL,
	"error_message" text
);
--> statement-breakpoint
CREATE TABLE "stalker_seller_inventory" (
	"id" serial PRIMARY KEY NOT NULL,
	"run_id" integer NOT NULL,
	"seller_id" text NOT NULL,
	"asin" text NOT NULL,
	"can_sell" boolean,
	"sellability_status" text,
	"sellability_reason" text,
	"product_title" text,
	"brand" text,
	"category_tree" text,
	"current_price" real,
	"avg_price_90d" real,
	"sales_rank" integer,
	"monthly_sold" integer,
	"seller_count" integer,
	"amazon_is_seller" boolean,
	"raw_product_json" text,
	"last_seen_at" timestamp with time zone NOT NULL,
	"raw_inventory_json" text,
	CONSTRAINT "uq_stalker_inventory_run_seller_asin" UNIQUE("run_id","seller_id","asin")
);
--> statement-breakpoint
ALTER TABLE "analysis_results" ADD CONSTRAINT "analysis_results_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "category_product_results" ADD CONSTRAINT "category_product_results_run_id_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."runs"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "stalker_asin_scans" ADD CONSTRAINT "stalker_asin_scans_run_id_stalker_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."stalker_runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "stalker_asin_sellers" ADD CONSTRAINT "stalker_asin_sellers_scan_id_stalker_asin_scans_id_fk" FOREIGN KEY ("scan_id") REFERENCES "public"."stalker_asin_scans"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "stalker_asin_sellers" ADD CONSTRAINT "stalker_asin_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_seller_inventory" ADD CONSTRAINT "stalker_seller_inventory_run_id_stalker_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."stalker_runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "stalker_seller_inventory" ADD CONSTRAINT "stalker_seller_inventory_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
CREATE INDEX "idx_analysis_results_run_id" ON "analysis_results" USING btree ("run_id");--> statement-breakpoint
CREATE INDEX "idx_analysis_results_asin" ON "analysis_results" USING btree ("asin");--> statement-breakpoint
CREATE INDEX "idx_analysis_results_verdict" ON "analysis_results" USING btree ("verdict");--> statement-breakpoint
CREATE INDEX "idx_analysis_results_sellability_status" ON "analysis_results" USING btree ("sellability_status");--> statement-breakpoint
CREATE INDEX "idx_analysis_results_fetched_at" ON "analysis_results" USING btree ("fetched_at");--> statement-breakpoint
CREATE INDEX "idx_category_results_run_id" ON "category_product_results" USING btree ("run_id");--> statement-breakpoint
CREATE INDEX "idx_category_results_verdict" ON "category_product_results" USING btree ("verdict");--> statement-breakpoint
CREATE INDEX "idx_category_results_sellability_status" ON "category_product_results" USING btree ("sellability_status");--> statement-breakpoint
CREATE INDEX "idx_category_results_fetched_at" ON "category_product_results" USING btree ("fetched_at");--> 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_stalker_scans_run_id" ON "stalker_asin_scans" USING btree ("run_id");--> statement-breakpoint
CREATE INDEX "idx_stalker_scans_source_asin" ON "stalker_asin_scans" USING btree ("source_asin");--> statement-breakpoint
CREATE INDEX "idx_stalker_runs_started_at" ON "stalker_runs" USING btree ("started_at");--> statement-breakpoint
CREATE INDEX "idx_stalker_inventory_seller_id" ON "stalker_seller_inventory" USING btree ("seller_id");--> statement-breakpoint
CREATE INDEX "idx_stalker_inventory_asin" ON "stalker_seller_inventory" USING btree ("asin");--> statement-breakpoint
CREATE INDEX "idx_stalker_inventory_product_title" ON "stalker_seller_inventory" USING btree ("product_title");