CREATE TYPE "public"."pig_account_side" AS ENUM('supply', 'demand', 'both');--> statement-breakpoint
CREATE TYPE "public"."pig_activity_type" AS ENUM('note', 'email', 'call', 'meeting', 'slack', 'buzz', 'stage_change', 'contract_event', 'task', 'agent_action');--> statement-breakpoint
CREATE TYPE "public"."pig_affiliation_kind" AS ENUM('staff', 'founder', 'alumni', 'contributor', 'resident', 'advisor', 'investor', 'customer_reference', 'unknown');--> statement-breakpoint
CREATE TYPE "public"."pig_agent_task_kind" AS ENUM('enrich_account', 'enrich_contact', 'write_brief', 'match_capacity', 'detect_idle_capacity', 'summarise_pipeline', 'watch_renewal', 'research_supplier');--> statement-breakpoint
CREATE TYPE "public"."pig_agent_task_outcome" AS ENUM('succeeded', 'failed', 'skipped', 'cancelled');--> statement-breakpoint
CREATE TYPE "public"."pig_confidence_grade" AS ENUM('confirmed', 'probable', 'unverified', 'disputed');--> statement-breakpoint
CREATE TYPE "public"."pig_contract_status" AS ENUM('draft', 'in_review', 'in_negotiation', 'out_for_signature', 'executed', 'expired', 'terminated');--> statement-breakpoint
CREATE TYPE "public"."pig_contract_type" AS ENUM('msa', 'dpa', 'sla', 'order_form', 'capacity_commitment', 'nda', 'amendment');--> statement-breakpoint
CREATE TYPE "public"."pig_customer_segment" AS ENUM('frontier_lab', 'applied_ai_startup', 'enterprise', 'research_institution', 'sovereign', 'individual_developer');--> statement-breakpoint
CREATE TYPE "public"."pig_demand_stage" AS ENUM('qualification', 'legal', 'scoping', 'proposal', 'procurement', 'poc', 'deployment', 'expansion', 'closed_won', 'closed_lost');--> statement-breakpoint
CREATE TYPE "public"."pig_fact_band" AS ENUM('verified', 'probable', 'possible');--> statement-breakpoint
CREATE TYPE "public"."pig_fact_status" AS ENUM('applied', 'proposed', 'dismissed', 'superseded');--> statement-breakpoint
CREATE TYPE "public"."pig_gpu_socket" AS ENUM('PCIe', 'SXM2', 'SXM3', 'SXM4', 'SXM5', 'SXM6');--> statement-breakpoint
CREATE TYPE "public"."pig_interconnect_type" AS ENUM('Infiniband', 'RoCE', 'NVLink', 'Ethernet', 'Unknown');--> statement-breakpoint
CREATE TYPE "public"."pig_product_line" AS ENUM('compute_ondemand', 'compute_reserved', 'compute_spot', 'post_training', 'inference', 'evaluations', 'platform');--> statement-breakpoint
CREATE TYPE "public"."pig_record_source" AS ENUM('manual', 'seed', 'agent', 'prime_api', 'slack', 'buzz', 'import');--> statement-breakpoint
CREATE TYPE "public"."pig_security_tier" AS ENUM('secure_cloud', 'community_cloud');--> statement-breakpoint
CREATE TYPE "public"."pig_sla_kind" AS ENUM('none', 'credits_policy', 'negotiated');--> statement-breakpoint
CREATE TYPE "public"."pig_sla_metric" AS ENUM('uptime_pct', 'node_availability_pct', 'node_replacement_hours', 'mttr_hours', 'support_response_hours', 'throughput_floor');--> statement-breakpoint
CREATE TYPE "public"."pig_stock_status" AS ENUM('Available', 'Low', 'Medium', 'High', 'Unavailable');--> statement-breakpoint
CREATE TYPE "public"."pig_supplier_type" AS ENUM('hyperscaler', 'neocloud', 'regional_operator', 'datacenter', 'broker', 'depin', 'community');--> statement-breakpoint
CREATE TYPE "public"."pig_supply_stage" AS ENUM('sourced', 'qualifying', 'technical_diligence', 'financial_diligence', 'pricing', 'contracting', 'onboarding', 'live', 'renewal', 'churned', 'rejected');--> statement-breakpoint
CREATE TYPE "public"."pig_team" AS ENUM('supply', 'demand', 'research');--> statement-breakpoint
CREATE TYPE "public"."pig_team_role" AS ENUM('member', 'lead', 'admin');--> statement-breakpoint
CREATE TABLE "api_keys" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid NOT NULL,
	"name" text NOT NULL,
	"key_hash" text NOT NULL,
	"key_prefix" text NOT NULL,
	"scopes" jsonb DEFAULT '["read"]'::jsonb NOT NULL,
	"last_used_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"revoked_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "invites" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"code_hash" text NOT NULL,
	"email" text,
	"team" "pig_team",
	"role" "pig_team_role" DEFAULT 'member' NOT NULL,
	"created_by_user_id" uuid,
	"expires_at" timestamp with time zone,
	"uses_remaining" jsonb DEFAULT '1'::jsonb NOT NULL,
	"redeemed_by_user_id" uuid,
	"redeemed_at" timestamp with time zone,
	"revoked_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "team_memberships" (
	"user_id" uuid NOT NULL,
	"team" "pig_team" NOT NULL,
	"role" "pig_team_role" DEFAULT 'member' NOT NULL,
	"is_primary" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "team_memberships_user_id_team_pk" PRIMARY KEY("user_id","team")
);
--> statement-breakpoint
CREATE TABLE "users" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"auth_subject" uuid,
	"email" text NOT NULL,
	"name" text NOT NULL,
	"handle" text,
	"avatar_url" text,
	"title" text,
	"timezone" text,
	"is_platform_admin" boolean DEFAULT false NOT NULL,
	"deactivated_at" timestamp with time zone,
	"last_seen_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "account_stats" (
	"account_id" uuid PRIMARY KEY NOT NULL,
	"open_deals" integer DEFAULT 0 NOT NULL,
	"contact_count" integer DEFAULT 0 NOT NULL,
	"refreshed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "accounts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" text NOT NULL,
	"domain" text,
	"website" text,
	"description" text,
	"logo_url" text,
	"side" "pig_account_side" DEFAULT 'demand' NOT NULL,
	"supplier_type" "pig_supplier_type",
	"customer_segment" "pig_customer_segment",
	"country" text,
	"region" text,
	"jurisdiction" text,
	"ultimate_parent_account_id" uuid,
	"ultimate_parent_name" text,
	"ultimate_parent_country" text,
	"ownership_verified_at" timestamp with time zone,
	"linkedin_url" text,
	"twitter_url" text,
	"github_org" text,
	"owner_user_id" uuid,
	"source" "pig_record_source" DEFAULT 'manual' NOT NULL,
	"source_url" text,
	"confidence" "pig_confidence_grade" DEFAULT 'confirmed' NOT NULL,
	"last_activity_at" timestamp with time zone,
	"archived_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "activities" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"type" "pig_activity_type" NOT NULL,
	"subject" text,
	"body" text,
	"account_id" uuid,
	"contact_id" uuid,
	"demand_deal_id" uuid,
	"supply_deal_id" uuid,
	"actor_user_id" uuid,
	"actor_agent" text,
	"occurred_at" timestamp with time zone DEFAULT now() NOT NULL,
	"meta" jsonb,
	"external_id" text,
	"source" "pig_record_source" DEFAULT 'manual' NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "channel_links" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"platform" text NOT NULL,
	"channel_id" text NOT NULL,
	"channel_name" text,
	"account_id" uuid,
	"notify_on" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "contacts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid,
	"full_name" text NOT NULL,
	"first_name" text,
	"last_name" text,
	"title" text,
	"email" text,
	"phone" text,
	"linkedin_url" text,
	"twitter_handle" text,
	"github_handle" text,
	"website_url" text,
	"avatar_url" text,
	"affiliation" "pig_affiliation_kind" DEFAULT 'unknown' NOT NULL,
	"departed_at" timestamp with time zone,
	"is_decision_maker" boolean DEFAULT false NOT NULL,
	"owner_user_id" uuid,
	"source" "pig_record_source" DEFAULT 'manual' NOT NULL,
	"source_url" text,
	"confidence" "pig_confidence_grade" DEFAULT 'confirmed' NOT NULL,
	"confidence_note" text,
	"last_activity_at" timestamp with time zone,
	"archived_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "capacity_commitments" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"site_id" uuid,
	"supply_deal_id" uuid,
	"name" text NOT NULL,
	"gpu_type" text NOT NULL,
	"socket" "pig_gpu_socket",
	"gpu_count" integer NOT NULL,
	"interconnect_type" "pig_interconnect_type" DEFAULT 'Unknown' NOT NULL,
	"security_tier" "pig_security_tier" DEFAULT 'secure_cloud' NOT NULL,
	"starts_at" timestamp with time zone NOT NULL,
	"ends_at" timestamp with time zone NOT NULL,
	"total_gpu_hours" numeric(16, 2) NOT NULL,
	"cost_per_gpu_hour_cents" integer NOT NULL,
	"currency" text DEFAULT 'USD' NOT NULL,
	"shape" jsonb,
	"colocate_with" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"is_contiguous" boolean DEFAULT true NOT NULL,
	"minimum_spend_cents" integer,
	"is_auto_renew" boolean DEFAULT false NOT NULL,
	"notice_days" integer,
	"take_or_pay_floor_pct" numeric(6, 2),
	"prepaid_pct" numeric(6, 2),
	"prepaid_amount_cents" integer,
	"useful_life_years" numeric(4, 2),
	"salvage_value_pct" numeric(6, 2),
	"depreciation_start_at" timestamp with time zone,
	"cost_of_capital_bps" integer,
	"financing_instrument" text,
	"oversubscription_pct" numeric(6, 2) DEFAULT '0' NOT NULL,
	"notes" text,
	"terminated_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "inventory_listings" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid,
	"site_id" uuid,
	"external_cloud_id" text,
	"provider_slug" text,
	"gpu_type" text NOT NULL,
	"socket" "pig_gpu_socket",
	"gpu_count" integer NOT NULL,
	"gpu_memory_gb" integer,
	"vcpu" integer,
	"memory_gb" integer,
	"disk_gb" integer,
	"internet_mbps" integer,
	"interconnect_gbps" integer,
	"interconnect_type" "pig_interconnect_type" DEFAULT 'Unknown' NOT NULL,
	"region" text,
	"country" text,
	"security_tier" "pig_security_tier" DEFAULT 'secure_cloud' NOT NULL,
	"stock_status" "pig_stock_status" DEFAULT 'Available' NOT NULL,
	"is_spot" boolean DEFAULT false NOT NULL,
	"provisioning_minutes" integer,
	"prepaid_hours" numeric(12, 2),
	"on_demand_price_cents" integer,
	"community_price_cents" integer,
	"price_is_variable" boolean DEFAULT false NOT NULL,
	"currency" text DEFAULT 'USD' NOT NULL,
	"images" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"raw" jsonb,
	"source" "pig_record_source" DEFAULT 'manual' NOT NULL,
	"observed_at" timestamp with time zone DEFAULT now() 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 "sites" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"name" text NOT NULL,
	"external_data_center_id" text,
	"country" text,
	"country_code" text,
	"region" text,
	"city" text,
	"jurisdiction" text,
	"power_mw" numeric(10, 3),
	"pue" numeric(4, 2),
	"certifications" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"uptime_history_pct" numeric(6, 3),
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "supply_deals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"site_id" uuid,
	"name" text NOT NULL,
	"stage" "pig_supply_stage" DEFAULT 'sourced' NOT NULL,
	"stage_changed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"owner_user_id" uuid,
	"primary_contact_id" uuid,
	"gpu_type" text,
	"gpu_count" integer,
	"interconnect_type" "pig_interconnect_type",
	"target_cost_per_gpu_hour_cents" integer,
	"term_months" integer,
	"available_from" timestamp with time zone,
	"technical_verdict" text,
	"technical_verdict_by" uuid,
	"technical_verdict_at" timestamp with time zone,
	"technical_notes" text,
	"financial_verdict" text,
	"financial_verdict_by" uuid,
	"financial_verdict_at" timestamp with time zone,
	"financial_notes" text,
	"rejection_reason" text,
	"closed_at" timestamp with time zone,
	"last_activity_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "capacity_requests" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"demand_deal_id" uuid NOT NULL,
	"gpu_type" text,
	"gpu_type_alternatives" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"gpu_count" integer NOT NULL,
	"requires_high_speed_interconnect" boolean DEFAULT false NOT NULL,
	"min_interconnect_type" "pig_interconnect_type",
	"min_security_tier" "pig_security_tier" DEFAULT 'secure_cloud' NOT NULL,
	"allowed_regions" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"excluded_jurisdictions" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"starts_at" timestamp with time zone,
	"ends_at" timestamp with time zone,
	"total_gpu_hours" numeric(16, 2),
	"max_price_per_gpu_hour_cents" integer,
	"required_certifications" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "deal_contacts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"demand_deal_id" uuid NOT NULL,
	"contact_id" uuid NOT NULL,
	"role" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "demand_deals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"name" text NOT NULL,
	"description" text,
	"product_line" "pig_product_line" DEFAULT 'compute_reserved' NOT NULL,
	"stage" "pig_demand_stage" DEFAULT 'qualification' NOT NULL,
	"stage_changed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"owner_user_id" uuid,
	"primary_contact_id" uuid,
	"acv_cents" integer,
	"tcv_cents" integer,
	"currency" text DEFAULT 'USD' NOT NULL,
	"term_months" integer,
	"probability" numeric(4, 3),
	"expected_close_date" timestamp with time zone,
	"closed_at" timestamp with time zone,
	"closed_reason" text,
	"msa_executed" boolean DEFAULT false NOT NULL,
	"dpa_executed" boolean DEFAULT false NOT NULL,
	"parent_deal_id" uuid,
	"last_activity_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "allocations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"capacity_commitment_id" uuid NOT NULL,
	"demand_deal_id" uuid,
	"internal_team" text,
	"gpu_hours" numeric(16, 2) NOT NULL,
	"price_per_gpu_hour_cents" integer DEFAULT 0 NOT NULL,
	"currency" text DEFAULT 'USD' NOT NULL,
	"starts_at" timestamp with time zone NOT NULL,
	"ends_at" timestamp with time zone NOT NULL,
	"status" text DEFAULT 'planned' NOT NULL,
	"hold_expires_at" timestamp with time zone,
	"hold_opportunity_cost_cents" integer,
	"guarantee_type" text DEFAULT 'committed' NOT NULL,
	"priority" integer DEFAULT 100 NOT NULL,
	"compliance_decision_id" uuid,
	"created_by_user_id" uuid,
	"notes" text,
	"released_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "contract_obligations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"contract_id" uuid NOT NULL,
	"title" text NOT NULL,
	"description" text,
	"kind" text DEFAULT 'milestone' NOT NULL,
	"due_at" timestamp with time zone NOT NULL,
	"completed_at" timestamp with time zone,
	"owner_user_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "contracts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"type" "pig_contract_type" NOT NULL,
	"status" "pig_contract_status" DEFAULT 'draft' NOT NULL,
	"side" text DEFAULT 'demand' NOT NULL,
	"title" text NOT NULL,
	"external_reference" text,
	"demand_deal_id" uuid,
	"supply_deal_id" uuid,
	"capacity_commitment_id" uuid,
	"parent_contract_id" uuid,
	"contracting_party_name" text,
	"take_or_pay_floor_pct" numeric(6, 2),
	"prepaid_pct" numeric(6, 2),
	"termination_tier" text,
	"assignable_on_default" boolean DEFAULT false NOT NULL,
	"assignment_deadline_business_days" integer,
	"effective_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"executed_at" timestamp with time zone,
	"terminated_at" timestamp with time zone,
	"is_auto_renew" boolean DEFAULT false NOT NULL,
	"notice_days" integer,
	"value_cents" integer,
	"currency" text DEFAULT 'USD' NOT NULL,
	"governing_law" text,
	"document_url" text,
	"owner_user_id" uuid,
	"notes" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sla_metric_targets" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"sla_term_id" uuid NOT NULL,
	"metric" "pig_sla_metric" NOT NULL,
	"target_value" numeric(12, 3) NOT NULL,
	"unit" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sla_terms" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"contract_id" uuid NOT NULL,
	"kind" "pig_sla_kind" DEFAULT 'credits_policy' NOT NULL,
	"uptime_target_pct" numeric(6, 3),
	"node_replacement_hours" integer,
	"mttr_hours" integer,
	"support_response_hours" integer,
	"measurement_window" text DEFAULT 'monthly' NOT NULL,
	"measurement_unit" text DEFAULT 'cluster' NOT NULL,
	"remedy_type" text DEFAULT 'service_credit' NOT NULL,
	"abatement_trigger_value" integer,
	"abatement_trigger_unit" text,
	"claim_deadline_value" integer,
	"claim_deadline_unit" text DEFAULT 'days' NOT NULL,
	"credit_expiry_months" integer,
	"is_sole_remedy" boolean DEFAULT true NOT NULL,
	"spare_pool_obligation" text,
	"spare_pool_scope" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"maintenance_classes" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"reasonable_endeavours_days_per_year" integer,
	"rca_delivery_hours" integer,
	"credit_schedule" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"credit_cap_pct" numeric(6, 3),
	"exclusions" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "compliance_artifacts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"claim" text NOT NULL,
	"scope" text,
	"is_certified" boolean DEFAULT false NOT NULL,
	"soc2_type" text,
	"observation_window_start" timestamp with time zone,
	"observation_window_end" timestamp with time zone,
	"audit_firm" text,
	"carve_out_method" text,
	"products_in_scope" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"evidence_url" text,
	"verified_by_user_id" uuid,
	"verified_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "compliance_decisions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"allocation_id" uuid,
	"account_id" uuid NOT NULL,
	"beneficial_owner_name" text,
	"ultimate_parent_country" text,
	"physical_jurisdiction" text,
	"end_use" text,
	"decision" text DEFAULT 'needs_review' NOT NULL,
	"rationale" text,
	"rule_version" text,
	"decided_by_user_id" uuid,
	"decided_at" timestamp with time zone,
	"re_evaluation_triggers" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"superseded_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "export_authorizations" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid NOT NULL,
	"authorization_type" text DEFAULT 'none' NOT NULL,
	"reference" text,
	"scope_notes" text,
	"issued_at" timestamp with time zone,
	"expires_at" timestamp with time zone,
	"evidence_url" text,
	"verified_by_user_id" uuid,
	"verified_at" timestamp with time zone,
	"volatile" 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 "agent_actions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"agent_run_id" uuid,
	"type" text NOT NULL,
	"target_type" text,
	"target_id" text,
	"summary" text,
	"idempotency_key" text NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"external_id" text,
	"error" text,
	"metadata" jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "agent_runs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"agent_task_id" uuid,
	"agent" text DEFAULT 'piggy' NOT NULL,
	"principal_user_id" uuid,
	"status" text DEFAULT 'running' NOT NULL,
	"model" text,
	"input_tokens" integer,
	"output_tokens" integer,
	"cost_micro_cents" integer,
	"input" jsonb,
	"result" jsonb,
	"summary" text,
	"error" text,
	"started_at" timestamp with time zone DEFAULT now() NOT NULL,
	"finished_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "agent_tasks" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"kind" "pig_agent_task_kind" NOT NULL,
	"subject" text NOT NULL,
	"reason" text,
	"payload" jsonb,
	"priority" integer DEFAULT 0 NOT NULL,
	"budget" integer DEFAULT 4 NOT NULL,
	"attempts" integer DEFAULT 0 NOT NULL,
	"max_attempts" integer DEFAULT 3 NOT NULL,
	"due_at" timestamp with time zone DEFAULT now() NOT NULL,
	"leased_until" timestamp with time zone,
	"leased_by" text,
	"started_at" timestamp with time zone,
	"finished_at" timestamp with time zone,
	"outcome" "pig_agent_task_outcome",
	"error" text,
	"requested_by_user_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "facts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"account_id" uuid,
	"contact_id" uuid,
	"field" text NOT NULL,
	"value" text NOT NULL,
	"score" numeric(4, 3) NOT NULL,
	"band" "pig_fact_band" NOT NULL,
	"status" "pig_fact_status" DEFAULT 'proposed' NOT NULL,
	"evidence" jsonb,
	"source_url" text,
	"method" text,
	"agent_run_id" uuid,
	"decided_by_user_id" uuid,
	"decided_at" timestamp with time zone,
	"observed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"superseded_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "field_definitions" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"entity" text NOT NULL,
	"key" text NOT NULL,
	"label" text NOT NULL,
	"type" text DEFAULT 'text' NOT NULL,
	"agent_filled" boolean DEFAULT true NOT NULL,
	"agent_brief" text,
	"is_required" boolean DEFAULT false NOT NULL,
	"show_on_table" boolean DEFAULT false NOT NULL,
	"position" integer DEFAULT 0 NOT NULL,
	"archived_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "field_options" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"field_id" uuid NOT NULL,
	"label" text NOT NULL,
	"position" integer DEFAULT 0 NOT NULL,
	"archived_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "field_values" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"field_id" uuid NOT NULL,
	"account_id" uuid,
	"contact_id" uuid,
	"demand_deal_id" uuid,
	"supply_deal_id" uuid,
	"text_value" text,
	"number_value" numeric(24, 4),
	"date_value" timestamp with time zone,
	"bool_value" boolean,
	"option_id" uuid,
	"user_value" uuid,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "api_keys" ADD CONSTRAINT "api_keys_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invites" ADD CONSTRAINT "invites_created_by_user_id_users_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "invites" ADD CONSTRAINT "invites_redeemed_by_user_id_users_id_fk" FOREIGN KEY ("redeemed_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "team_memberships" ADD CONSTRAINT "team_memberships_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "account_stats" ADD CONSTRAINT "account_stats_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "accounts" ADD CONSTRAINT "accounts_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "activities" ADD CONSTRAINT "activities_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "activities" ADD CONSTRAINT "activities_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "activities" ADD CONSTRAINT "activities_actor_user_id_users_id_fk" FOREIGN KEY ("actor_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "channel_links" ADD CONSTRAINT "channel_links_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contacts" ADD CONSTRAINT "contacts_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contacts" ADD CONSTRAINT "contacts_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "capacity_commitments" ADD CONSTRAINT "capacity_commitments_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "capacity_commitments" ADD CONSTRAINT "capacity_commitments_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "capacity_commitments" ADD CONSTRAINT "capacity_commitments_supply_deal_id_supply_deals_id_fk" FOREIGN KEY ("supply_deal_id") REFERENCES "public"."supply_deals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_listings" ADD CONSTRAINT "inventory_listings_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "inventory_listings" ADD CONSTRAINT "inventory_listings_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sites" ADD CONSTRAINT "sites_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_site_id_sites_id_fk" FOREIGN KEY ("site_id") REFERENCES "public"."sites"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_primary_contact_id_contacts_id_fk" FOREIGN KEY ("primary_contact_id") REFERENCES "public"."contacts"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_technical_verdict_by_users_id_fk" FOREIGN KEY ("technical_verdict_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "supply_deals" ADD CONSTRAINT "supply_deals_financial_verdict_by_users_id_fk" FOREIGN KEY ("financial_verdict_by") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "capacity_requests" ADD CONSTRAINT "capacity_requests_demand_deal_id_demand_deals_id_fk" FOREIGN KEY ("demand_deal_id") REFERENCES "public"."demand_deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deal_contacts" ADD CONSTRAINT "deal_contacts_demand_deal_id_demand_deals_id_fk" FOREIGN KEY ("demand_deal_id") REFERENCES "public"."demand_deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "deal_contacts" ADD CONSTRAINT "deal_contacts_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "demand_deals" ADD CONSTRAINT "demand_deals_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "demand_deals" ADD CONSTRAINT "demand_deals_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "demand_deals" ADD CONSTRAINT "demand_deals_primary_contact_id_contacts_id_fk" FOREIGN KEY ("primary_contact_id") REFERENCES "public"."contacts"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocations" ADD CONSTRAINT "allocations_capacity_commitment_id_capacity_commitments_id_fk" FOREIGN KEY ("capacity_commitment_id") REFERENCES "public"."capacity_commitments"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocations" ADD CONSTRAINT "allocations_demand_deal_id_demand_deals_id_fk" FOREIGN KEY ("demand_deal_id") REFERENCES "public"."demand_deals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "allocations" ADD CONSTRAINT "allocations_created_by_user_id_users_id_fk" FOREIGN KEY ("created_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contract_obligations" ADD CONSTRAINT "contract_obligations_contract_id_contracts_id_fk" FOREIGN KEY ("contract_id") REFERENCES "public"."contracts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contract_obligations" ADD CONSTRAINT "contract_obligations_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contracts" ADD CONSTRAINT "contracts_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contracts" ADD CONSTRAINT "contracts_demand_deal_id_demand_deals_id_fk" FOREIGN KEY ("demand_deal_id") REFERENCES "public"."demand_deals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contracts" ADD CONSTRAINT "contracts_supply_deal_id_supply_deals_id_fk" FOREIGN KEY ("supply_deal_id") REFERENCES "public"."supply_deals"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contracts" ADD CONSTRAINT "contracts_capacity_commitment_id_capacity_commitments_id_fk" FOREIGN KEY ("capacity_commitment_id") REFERENCES "public"."capacity_commitments"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "contracts" ADD CONSTRAINT "contracts_owner_user_id_users_id_fk" FOREIGN KEY ("owner_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sla_metric_targets" ADD CONSTRAINT "sla_metric_targets_sla_term_id_sla_terms_id_fk" FOREIGN KEY ("sla_term_id") REFERENCES "public"."sla_terms"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sla_terms" ADD CONSTRAINT "sla_terms_contract_id_contracts_id_fk" FOREIGN KEY ("contract_id") REFERENCES "public"."contracts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compliance_artifacts" ADD CONSTRAINT "compliance_artifacts_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compliance_artifacts" ADD CONSTRAINT "compliance_artifacts_verified_by_user_id_users_id_fk" FOREIGN KEY ("verified_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compliance_decisions" ADD CONSTRAINT "compliance_decisions_allocation_id_allocations_id_fk" FOREIGN KEY ("allocation_id") REFERENCES "public"."allocations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compliance_decisions" ADD CONSTRAINT "compliance_decisions_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "compliance_decisions" ADD CONSTRAINT "compliance_decisions_decided_by_user_id_users_id_fk" FOREIGN KEY ("decided_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "export_authorizations" ADD CONSTRAINT "export_authorizations_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "export_authorizations" ADD CONSTRAINT "export_authorizations_verified_by_user_id_users_id_fk" FOREIGN KEY ("verified_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_actions" ADD CONSTRAINT "agent_actions_agent_run_id_agent_runs_id_fk" FOREIGN KEY ("agent_run_id") REFERENCES "public"."agent_runs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_runs" ADD CONSTRAINT "agent_runs_agent_task_id_agent_tasks_id_fk" FOREIGN KEY ("agent_task_id") REFERENCES "public"."agent_tasks"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_runs" ADD CONSTRAINT "agent_runs_principal_user_id_users_id_fk" FOREIGN KEY ("principal_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agent_tasks" ADD CONSTRAINT "agent_tasks_requested_by_user_id_users_id_fk" FOREIGN KEY ("requested_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "facts" ADD CONSTRAINT "facts_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "facts" ADD CONSTRAINT "facts_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "facts" ADD CONSTRAINT "facts_decided_by_user_id_users_id_fk" FOREIGN KEY ("decided_by_user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_options" ADD CONSTRAINT "field_options_field_id_field_definitions_id_fk" FOREIGN KEY ("field_id") REFERENCES "public"."field_definitions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_field_id_field_definitions_id_fk" FOREIGN KEY ("field_id") REFERENCES "public"."field_definitions"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_account_id_accounts_id_fk" FOREIGN KEY ("account_id") REFERENCES "public"."accounts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_contact_id_contacts_id_fk" FOREIGN KEY ("contact_id") REFERENCES "public"."contacts"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_demand_deal_id_demand_deals_id_fk" FOREIGN KEY ("demand_deal_id") REFERENCES "public"."demand_deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_supply_deal_id_supply_deals_id_fk" FOREIGN KEY ("supply_deal_id") REFERENCES "public"."supply_deals"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_option_id_field_options_id_fk" FOREIGN KEY ("option_id") REFERENCES "public"."field_options"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "field_values" ADD CONSTRAINT "field_values_user_value_users_id_fk" FOREIGN KEY ("user_value") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "api_keys_key_hash_key" ON "api_keys" USING btree ("key_hash");--> statement-breakpoint
CREATE INDEX "api_keys_user_idx" ON "api_keys" USING btree ("user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "invites_code_hash_key" ON "invites" USING btree ("code_hash");--> statement-breakpoint
CREATE INDEX "invites_email_idx" ON "invites" USING btree ("email");--> statement-breakpoint
CREATE INDEX "team_memberships_team_idx" ON "team_memberships" USING btree ("team");--> statement-breakpoint
CREATE UNIQUE INDEX "users_auth_subject_key" ON "users" USING btree ("auth_subject");--> statement-breakpoint
CREATE UNIQUE INDEX "users_email_key" ON "users" USING btree ("email");--> statement-breakpoint
CREATE UNIQUE INDEX "users_handle_key" ON "users" USING btree ("handle");--> statement-breakpoint
CREATE UNIQUE INDEX "accounts_domain_key" ON "accounts" USING btree ("domain");--> statement-breakpoint
CREATE INDEX "accounts_side_idx" ON "accounts" USING btree ("side");--> statement-breakpoint
CREATE INDEX "accounts_owner_idx" ON "accounts" USING btree ("owner_user_id");--> statement-breakpoint
CREATE INDEX "accounts_name_idx" ON "accounts" USING btree ("name");--> statement-breakpoint
CREATE INDEX "activities_account_idx" ON "activities" USING btree ("account_id","occurred_at");--> statement-breakpoint
CREATE INDEX "activities_contact_idx" ON "activities" USING btree ("contact_id","occurred_at");--> statement-breakpoint
CREATE INDEX "activities_demand_deal_idx" ON "activities" USING btree ("demand_deal_id","occurred_at");--> statement-breakpoint
CREATE INDEX "activities_supply_deal_idx" ON "activities" USING btree ("supply_deal_id","occurred_at");--> statement-breakpoint
CREATE UNIQUE INDEX "activities_external_id_key" ON "activities" USING btree ("external_id");--> statement-breakpoint
CREATE UNIQUE INDEX "channel_links_platform_channel_key" ON "channel_links" USING btree ("platform","channel_id");--> statement-breakpoint
CREATE INDEX "channel_links_account_idx" ON "channel_links" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "contacts_account_idx" ON "contacts" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "contacts_email_idx" ON "contacts" USING btree ("email");--> statement-breakpoint
CREATE INDEX "contacts_name_idx" ON "contacts" USING btree ("full_name");--> statement-breakpoint
CREATE INDEX "contacts_confidence_idx" ON "contacts" USING btree ("confidence");--> statement-breakpoint
CREATE INDEX "capacity_commitments_account_idx" ON "capacity_commitments" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "capacity_commitments_gpu_type_idx" ON "capacity_commitments" USING btree ("gpu_type");--> statement-breakpoint
CREATE INDEX "capacity_commitments_window_idx" ON "capacity_commitments" USING btree ("starts_at","ends_at");--> statement-breakpoint
CREATE UNIQUE INDEX "inventory_listings_external_key" ON "inventory_listings" USING btree ("external_cloud_id","gpu_type","socket","gpu_count","security_tier");--> statement-breakpoint
CREATE INDEX "inventory_listings_gpu_type_idx" ON "inventory_listings" USING btree ("gpu_type");--> statement-breakpoint
CREATE INDEX "inventory_listings_stock_idx" ON "inventory_listings" USING btree ("stock_status");--> statement-breakpoint
CREATE INDEX "inventory_listings_interconnect_idx" ON "inventory_listings" USING btree ("interconnect_type");--> statement-breakpoint
CREATE INDEX "inventory_listings_account_idx" ON "inventory_listings" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "inventory_listings_observed_idx" ON "inventory_listings" USING btree ("observed_at");--> statement-breakpoint
CREATE INDEX "sites_account_idx" ON "sites" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "sites_country_idx" ON "sites" USING btree ("country_code");--> statement-breakpoint
CREATE INDEX "supply_deals_account_idx" ON "supply_deals" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "supply_deals_stage_idx" ON "supply_deals" USING btree ("stage");--> statement-breakpoint
CREATE INDEX "supply_deals_owner_idx" ON "supply_deals" USING btree ("owner_user_id");--> statement-breakpoint
CREATE INDEX "capacity_requests_deal_idx" ON "capacity_requests" USING btree ("demand_deal_id");--> statement-breakpoint
CREATE INDEX "capacity_requests_gpu_type_idx" ON "capacity_requests" USING btree ("gpu_type");--> statement-breakpoint
CREATE INDEX "capacity_requests_window_idx" ON "capacity_requests" USING btree ("starts_at","ends_at");--> statement-breakpoint
CREATE INDEX "deal_contacts_deal_idx" ON "deal_contacts" USING btree ("demand_deal_id");--> statement-breakpoint
CREATE INDEX "deal_contacts_contact_idx" ON "deal_contacts" USING btree ("contact_id");--> statement-breakpoint
CREATE INDEX "demand_deals_account_idx" ON "demand_deals" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "demand_deals_stage_idx" ON "demand_deals" USING btree ("stage");--> statement-breakpoint
CREATE INDEX "demand_deals_owner_idx" ON "demand_deals" USING btree ("owner_user_id");--> statement-breakpoint
CREATE INDEX "demand_deals_close_date_idx" ON "demand_deals" USING btree ("expected_close_date");--> statement-breakpoint
CREATE INDEX "allocations_commitment_idx" ON "allocations" USING btree ("capacity_commitment_id");--> statement-breakpoint
CREATE INDEX "allocations_deal_idx" ON "allocations" USING btree ("demand_deal_id");--> statement-breakpoint
CREATE INDEX "allocations_status_idx" ON "allocations" USING btree ("status");--> statement-breakpoint
CREATE INDEX "allocations_window_idx" ON "allocations" USING btree ("starts_at","ends_at");--> statement-breakpoint
CREATE INDEX "allocations_hold_expiry_idx" ON "allocations" USING btree ("hold_expires_at");--> statement-breakpoint
CREATE INDEX "contract_obligations_contract_idx" ON "contract_obligations" USING btree ("contract_id");--> statement-breakpoint
CREATE INDEX "contract_obligations_due_idx" ON "contract_obligations" USING btree ("due_at");--> statement-breakpoint
CREATE INDEX "contract_obligations_owner_idx" ON "contract_obligations" USING btree ("owner_user_id");--> statement-breakpoint
CREATE INDEX "contracts_account_idx" ON "contracts" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "contracts_type_idx" ON "contracts" USING btree ("type");--> statement-breakpoint
CREATE INDEX "contracts_status_idx" ON "contracts" USING btree ("status");--> statement-breakpoint
CREATE INDEX "contracts_expiry_idx" ON "contracts" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "contracts_demand_deal_idx" ON "contracts" USING btree ("demand_deal_id");--> statement-breakpoint
CREATE INDEX "sla_metric_targets_term_idx" ON "sla_metric_targets" USING btree ("sla_term_id");--> statement-breakpoint
CREATE INDEX "sla_terms_contract_idx" ON "sla_terms" USING btree ("contract_id");--> statement-breakpoint
CREATE INDEX "compliance_artifacts_account_idx" ON "compliance_artifacts" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "compliance_artifacts_claim_idx" ON "compliance_artifacts" USING btree ("claim");--> statement-breakpoint
CREATE INDEX "compliance_artifacts_expiry_idx" ON "compliance_artifacts" USING btree ("expires_at");--> statement-breakpoint
CREATE INDEX "compliance_decisions_allocation_idx" ON "compliance_decisions" USING btree ("allocation_id");--> statement-breakpoint
CREATE INDEX "compliance_decisions_account_idx" ON "compliance_decisions" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "compliance_decisions_decision_idx" ON "compliance_decisions" USING btree ("decision");--> statement-breakpoint
CREATE INDEX "export_authorizations_account_idx" ON "export_authorizations" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "export_authorizations_expiry_idx" ON "export_authorizations" USING btree ("expires_at");--> statement-breakpoint
CREATE UNIQUE INDEX "agent_actions_idempotency_key" ON "agent_actions" USING btree ("idempotency_key");--> statement-breakpoint
CREATE INDEX "agent_actions_run_idx" ON "agent_actions" USING btree ("agent_run_id");--> statement-breakpoint
CREATE INDEX "agent_runs_task_idx" ON "agent_runs" USING btree ("agent_task_id");--> statement-breakpoint
CREATE INDEX "agent_runs_principal_idx" ON "agent_runs" USING btree ("principal_user_id");--> statement-breakpoint
CREATE UNIQUE INDEX "agent_tasks_pending_key" ON "agent_tasks" USING btree ("kind","subject") WHERE "agent_tasks"."finished_at" IS NULL;--> statement-breakpoint
CREATE INDEX "agent_tasks_claimable_idx" ON "agent_tasks" USING btree ("due_at","priority");--> statement-breakpoint
CREATE INDEX "agent_tasks_lease_idx" ON "agent_tasks" USING btree ("leased_until");--> statement-breakpoint
CREATE INDEX "facts_account_idx" ON "facts" USING btree ("account_id");--> statement-breakpoint
CREATE INDEX "facts_contact_idx" ON "facts" USING btree ("contact_id");--> statement-breakpoint
CREATE INDEX "facts_status_idx" ON "facts" USING btree ("status");--> statement-breakpoint
CREATE INDEX "facts_field_idx" ON "facts" USING btree ("field");--> statement-breakpoint
CREATE UNIQUE INDEX "field_definitions_entity_key" ON "field_definitions" USING btree ("entity","key");--> statement-breakpoint
CREATE INDEX "field_options_field_idx" ON "field_options" USING btree ("field_id");--> statement-breakpoint
CREATE UNIQUE INDEX "field_values_account_key" ON "field_values" USING btree ("field_id","account_id");--> statement-breakpoint
CREATE UNIQUE INDEX "field_values_contact_key" ON "field_values" USING btree ("field_id","contact_id");--> statement-breakpoint
CREATE UNIQUE INDEX "field_values_demand_deal_key" ON "field_values" USING btree ("field_id","demand_deal_id");--> statement-breakpoint
CREATE UNIQUE INDEX "field_values_supply_deal_key" ON "field_values" USING btree ("field_id","supply_deal_id");