CREATE TABLE "agent_events" ( "id" bigserial PRIMARY KEY NOT NULL, "investigation_id" uuid NOT NULL, "seq" integer NOT NULL, "type" text NOT NULL, "payload" jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "claims" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "text" text NOT NULL, "status" text DEFAULT 'open' NOT NULL, "confidence" real DEFAULT 0.5 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "competitors" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "url" text, "description" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "evidence" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "source_id" uuid NOT NULL, "claim_id" uuid, "kind" text NOT NULL, "quote" text NOT NULL, "summary" text NOT NULL, "strength" real DEFAULT 0.5 NOT NULL, "fingerprint" text NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "hypotheses" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "parent_hypothesis_id" uuid, "title" text NOT NULL, "statement" text NOT NULL, "status" text DEFAULT 'proposed' NOT NULL, "confidence" real DEFAULT 0.5 NOT NULL, "rationale" text, "adversarial_checked" boolean DEFAULT false NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "hypothesis_evidence" ( "hypothesis_id" uuid NOT NULL, "evidence_id" uuid NOT NULL, "relation" text NOT NULL, "weight" real DEFAULT 0.5 NOT NULL, CONSTRAINT "hypothesis_evidence_hypothesis_id_evidence_id_pk" PRIMARY KEY("hypothesis_id","evidence_id") ); --> statement-breakpoint CREATE TABLE "investigation_steps" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "step_number" integer NOT NULL, "model" text NOT NULL, "input_tokens" integer DEFAULT 0 NOT NULL, "output_tokens" integer DEFAULT 0 NOT NULL, "cost_usd" real DEFAULT 0 NOT NULL, "latency_ms" integer DEFAULT 0 NOT NULL, "tool_name" text, "tool_args" jsonb, "tool_result_summary" text, "decision_summary" text, "error" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "investigations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid, "objective" text NOT NULL, "status" text DEFAULT 'pending' NOT NULL, "phase" text DEFAULT 'scouting' NOT NULL, "stop_reason" text, "budget" jsonb NOT NULL, "budget_used" jsonb NOT NULL, "prompt_version" text NOT NULL, "tool_schema_version" text NOT NULL, "model" text NOT NULL, "error" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "started_at" timestamp with time zone, "completed_at" timestamp with time zone ); --> statement-breakpoint CREATE TABLE "opportunities" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "hypothesis_id" uuid, "title" text NOT NULL, "summary" text NOT NULL, "problem" text NOT NULL, "why_now" text NOT NULL, "risks" text NOT NULL, "skeptic_case" text NOT NULL, "report_md" text, "status" text DEFAULT 'candidate' NOT NULL, "worth_score" real, "evidence_confidence" real, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "opportunity_competitors" ( "opportunity_id" uuid NOT NULL, "competitor_id" uuid NOT NULL, "note" text, CONSTRAINT "opportunity_competitors_opportunity_id_competitor_id_pk" PRIMARY KEY("opportunity_id","competitor_id") ); --> statement-breakpoint CREATE TABLE "opportunity_evidence" ( "opportunity_id" uuid NOT NULL, "evidence_id" uuid NOT NULL, "role" text DEFAULT 'context' NOT NULL, CONSTRAINT "opportunity_evidence_opportunity_id_evidence_id_pk" PRIMARY KEY("opportunity_id","evidence_id") ); --> statement-breakpoint CREATE TABLE "opportunity_scores" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "opportunity_id" uuid NOT NULL, "dimension" text NOT NULL, "score" real NOT NULL, "confidence" real NOT NULL, "reasoning" text NOT NULL, "evidence_ids" jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "saved_opportunities" ( "user_id" uuid NOT NULL, "opportunity_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "saved_opportunities_user_id_opportunity_id_pk" PRIMARY KEY("user_id","opportunity_id") ); --> statement-breakpoint CREATE TABLE "searches" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "investigation_id" uuid NOT NULL, "query" text NOT NULL, "intent" text, "provider" text DEFAULT 'firecrawl' NOT NULL, "result_count" integer DEFAULT 0 NOT NULL, "results" jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "source_contents" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "source_id" uuid NOT NULL, "content_hash" text NOT NULL, "markdown" text NOT NULL, "http_status" integer, "word_count" integer DEFAULT 0 NOT NULL, "retrieved_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "sources" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "url" text NOT NULL, "canonical_url" text NOT NULL, "domain" text NOT NULL, "title" text, "description" text, "first_seen_investigation_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "email" text, "name" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "users_email_unique" UNIQUE("email") ); --> statement-breakpoint ALTER TABLE "agent_events" ADD CONSTRAINT "agent_events_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "claims" ADD CONSTRAINT "claims_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "evidence" ADD CONSTRAINT "evidence_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "evidence" ADD CONSTRAINT "evidence_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "evidence" ADD CONSTRAINT "evidence_claim_id_claims_id_fk" FOREIGN KEY ("claim_id") REFERENCES "public"."claims"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "hypotheses" ADD CONSTRAINT "hypotheses_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "hypothesis_evidence" ADD CONSTRAINT "hypothesis_evidence_hypothesis_id_hypotheses_id_fk" FOREIGN KEY ("hypothesis_id") REFERENCES "public"."hypotheses"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "hypothesis_evidence" ADD CONSTRAINT "hypothesis_evidence_evidence_id_evidence_id_fk" FOREIGN KEY ("evidence_id") REFERENCES "public"."evidence"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "investigation_steps" ADD CONSTRAINT "investigation_steps_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "investigations" ADD CONSTRAINT "investigations_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunities" ADD CONSTRAINT "opportunities_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunities" ADD CONSTRAINT "opportunities_hypothesis_id_hypotheses_id_fk" FOREIGN KEY ("hypothesis_id") REFERENCES "public"."hypotheses"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunity_competitors" ADD CONSTRAINT "opportunity_competitors_opportunity_id_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."opportunities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunity_competitors" ADD CONSTRAINT "opportunity_competitors_competitor_id_competitors_id_fk" FOREIGN KEY ("competitor_id") REFERENCES "public"."competitors"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunity_evidence" ADD CONSTRAINT "opportunity_evidence_opportunity_id_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."opportunities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunity_evidence" ADD CONSTRAINT "opportunity_evidence_evidence_id_evidence_id_fk" FOREIGN KEY ("evidence_id") REFERENCES "public"."evidence"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "opportunity_scores" ADD CONSTRAINT "opportunity_scores_opportunity_id_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."opportunities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "saved_opportunities" ADD CONSTRAINT "saved_opportunities_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "saved_opportunities" ADD CONSTRAINT "saved_opportunities_opportunity_id_opportunities_id_fk" FOREIGN KEY ("opportunity_id") REFERENCES "public"."opportunities"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "searches" ADD CONSTRAINT "searches_investigation_id_investigations_id_fk" FOREIGN KEY ("investigation_id") REFERENCES "public"."investigations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "source_contents" ADD CONSTRAINT "source_contents_source_id_sources_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."sources"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint ALTER TABLE "sources" ADD CONSTRAINT "sources_first_seen_investigation_id_investigations_id_fk" FOREIGN KEY ("first_seen_investigation_id") REFERENCES "public"."investigations"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint CREATE UNIQUE INDEX "agent_events_seq_idx" ON "agent_events" USING btree ("investigation_id","seq");--> statement-breakpoint CREATE INDEX "claims_investigation_idx" ON "claims" USING btree ("investigation_id");--> statement-breakpoint CREATE UNIQUE INDEX "competitors_name_idx" ON "competitors" USING btree ("name");--> statement-breakpoint CREATE INDEX "evidence_investigation_idx" ON "evidence" USING btree ("investigation_id");--> statement-breakpoint CREATE UNIQUE INDEX "evidence_fingerprint_idx" ON "evidence" USING btree ("investigation_id","fingerprint");--> statement-breakpoint CREATE INDEX "hypotheses_investigation_idx" ON "hypotheses" USING btree ("investigation_id");--> statement-breakpoint CREATE INDEX "steps_investigation_idx" ON "investigation_steps" USING btree ("investigation_id","step_number");--> statement-breakpoint CREATE INDEX "investigations_status_idx" ON "investigations" USING btree ("status");--> statement-breakpoint CREATE INDEX "investigations_created_idx" ON "investigations" USING btree ("created_at");--> statement-breakpoint CREATE INDEX "opportunities_investigation_idx" ON "opportunities" USING btree ("investigation_id");--> statement-breakpoint CREATE INDEX "opportunities_score_idx" ON "opportunities" USING btree ("worth_score");--> statement-breakpoint CREATE UNIQUE INDEX "opportunity_scores_dim_idx" ON "opportunity_scores" USING btree ("opportunity_id","dimension");--> statement-breakpoint CREATE INDEX "searches_investigation_idx" ON "searches" USING btree ("investigation_id");--> statement-breakpoint CREATE INDEX "source_contents_source_idx" ON "source_contents" USING btree ("source_id","retrieved_at");--> statement-breakpoint CREATE INDEX "source_contents_hash_idx" ON "source_contents" USING btree ("content_hash");--> statement-breakpoint CREATE UNIQUE INDEX "sources_canonical_idx" ON "sources" USING btree ("canonical_url");--> statement-breakpoint CREATE INDEX "sources_domain_idx" ON "sources" USING btree ("domain");