CREATE TYPE "public"."capability" AS ENUM('issuer', 'vendor');--> statement-breakpoint
CREATE TYPE "public"."category_change_kind" AS ENUM('split', 'merge', 'retire', 'remap');--> statement-breakpoint
CREATE TYPE "public"."grant_provenance" AS ENUM('manual', 'split_event', 'merge_event');--> statement-breakpoint
CREATE TYPE "public"."amendment_class" AS ENUM('substantive', 'clarification');--> statement-breakpoint
CREATE TYPE "public"."confirmation_state" AS ENUM('unconfirmed', 'confirmed', 'corrected');--> statement-breakpoint
CREATE TYPE "public"."requirement_kind" AS ENUM('mandatory_gate', 'scored_criterion', 'certification_form', 'compliance_schedule', 'pricing', 'narrative');--> statement-breakpoint
CREATE TYPE "public"."solicitation_stage" AS ENUM('draft', 'open', 'evaluating', 'finalists', 'awarded', 'cancelled');--> statement-breakpoint
CREATE TYPE "public"."coverage" AS ENUM('answered', 'not_answered', 'indeterminate');--> statement-breakpoint
CREATE TYPE "public"."participation_state" AS ENUM('invited', 'opened', 'acknowledged', 'intends_to_bid', 'submitted', 'withdrawn');--> statement-breakpoint
CREATE TYPE "public"."disclosure_class" AS ENUM('vendor_identity', 'participation_signal', 'scenario_quantity', 'answer_text', 'exception_text', 'price_quote', 'derived_cost', 'vault_document', 'own_scoring');--> statement-breakpoint
CREATE TYPE "public"."release_level" AS ENUM('none', 'invited', 'acknowledged', 'submitted', 'open', 'evaluating', 'finalist', 'awarded');--> statement-breakpoint
CREATE TYPE "public"."expiry_state" AS ENUM('current', 'expiring_soon', 'expired');--> statement-breakpoint
CREATE TYPE "public"."tier" AS ENUM('base', 'pro', 'enterprise');--> statement-breakpoint
CREATE TYPE "public"."sweep_finding_kind" AS ENUM('asserted_obligation', 'uncovered_span');--> statement-breakpoint
CREATE TABLE "actor" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"email" text NOT NULL,
	"display_name" text NOT NULL,
	CONSTRAINT "actor_email_unique" UNIQUE("email")
);
--> statement-breakpoint
CREATE TABLE "membership" (
	"actor_id" uuid NOT NULL,
	"org_id" uuid NOT NULL,
	CONSTRAINT "membership_actor_id_org_id_pk" PRIMARY KEY("actor_id","org_id")
);
--> statement-breakpoint
CREATE TABLE "org_capability" (
	"org_id" uuid NOT NULL,
	"capability" "capability" NOT NULL,
	"granted_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "org_capability_org_id_capability_pk" PRIMARY KEY("org_id","capability")
);
--> statement-breakpoint
CREATE TABLE "organization" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"legal_name" text NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "roster_entry" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"retailer_org_id" uuid NOT NULL,
	"name" text NOT NULL,
	"contact_email" text,
	"duns" text,
	"claimed_vendor_org_id" uuid,
	"claimed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "roster_entry_retailer_org_id_name_unique" UNIQUE("retailer_org_id","name")
);
--> statement-breakpoint
CREATE TABLE "category_change_event" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"kind" "category_change_kind" NOT NULL,
	"retailer_org_id" uuid,
	"from_ids" uuid[] NOT NULL,
	"to_ids" uuid[] NOT NULL,
	"rows_written" integer DEFAULT 0 NOT NULL,
	"performed_by" uuid NOT NULL,
	"performed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "eligibility" (
	"retailer_org_id" uuid NOT NULL,
	"vendor_org_id" uuid NOT NULL,
	"local_category_id" uuid NOT NULL,
	"provenance" "grant_provenance" DEFAULT 'manual' NOT NULL,
	"provenance_event_id" uuid,
	"granted_at" timestamp with time zone DEFAULT now() NOT NULL,
	"revoked_at" timestamp with time zone,
	CONSTRAINT "eligibility_retailer_org_id_vendor_org_id_local_category_id_pk" PRIMARY KEY("retailer_org_id","vendor_org_id","local_category_id")
);
--> statement-breakpoint
CREATE TABLE "local_category" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"retailer_org_id" uuid NOT NULL,
	"label" text NOT NULL,
	"spine_leaf_id" uuid,
	"valid_from" timestamp with time zone DEFAULT now() NOT NULL,
	"valid_to" timestamp with time zone,
	"superseded_by" uuid[] DEFAULT '{}' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "spine_node" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"label" text NOT NULL,
	"group_id" uuid,
	"unspsc_families" text[] DEFAULT '{}' NOT NULL,
	"valid_from" timestamp with time zone DEFAULT now() NOT NULL,
	"valid_to" timestamp with time zone,
	"superseded_by" uuid[] DEFAULT '{}' NOT NULL
);
--> statement-breakpoint
CREATE TABLE "criterion" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"label" text NOT NULL,
	"max_points" numeric(8, 2) NOT NULL,
	"weight" numeric(8, 2) NOT NULL,
	"scale_anchors" jsonb DEFAULT '[]'::jsonb NOT NULL
);
--> statement-breakpoint
ALTER TABLE "criterion" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "criterion_requirement" (
	"criterion_id" uuid NOT NULL,
	"requirement_id" uuid NOT NULL,
	"authored_by" uuid NOT NULL,
	"frozen_at" timestamp with time zone,
	CONSTRAINT "criterion_requirement_criterion_id_requirement_id_pk" PRIMARY KEY("criterion_id","requirement_id")
);
--> statement-breakpoint
ALTER TABLE "criterion_requirement" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "evaluation_scenario" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"declared_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "price_line" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"label" text NOT NULL,
	"unit" text NOT NULL
);
--> statement-breakpoint
CREATE TABLE "requirement" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"ordinal" integer NOT NULL,
	"kind" "requirement_kind" NOT NULL,
	"text" text NOT NULL,
	"response_obligation" text,
	"provenance_document_id" uuid,
	"provenance_page" integer,
	"provenance_span" jsonb,
	"confirmation_state" "confirmation_state" DEFAULT 'unconfirmed' NOT NULL,
	"confirmed_by" uuid,
	"confirmed_at" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "requirement" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "scenario_quantity" (
	"scenario_id" uuid NOT NULL,
	"price_line_id" uuid NOT NULL,
	"quantity" numeric(14, 3) NOT NULL,
	CONSTRAINT "scenario_quantity_scenario_id_price_line_id_pk" PRIMARY KEY("scenario_id","price_line_id")
);
--> statement-breakpoint
ALTER TABLE "scenario_quantity" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "solicitation" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"issuer_org_id" uuid NOT NULL,
	"local_category_id" uuid NOT NULL,
	"title" text NOT NULL,
	"stage" "solicitation_stage" DEFAULT 'draft' NOT NULL,
	"issued_at" timestamp with time zone,
	"responses_due_at" timestamp with time zone,
	"acknowledgement_required" boolean DEFAULT false NOT NULL,
	"volume_band" text
);
--> statement-breakpoint
ALTER TABLE "solicitation" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "solicitation_amendment" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"class" "amendment_class" NOT NULL,
	"summary" text NOT NULL,
	"requirement_ids" uuid[] DEFAULT '{}' NOT NULL,
	"deadline_extended_to" timestamp with time zone,
	"authored_by" uuid NOT NULL,
	"authored_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "solicitation_document" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"kind" text NOT NULL,
	"blob_ref" text NOT NULL,
	"sha256" text NOT NULL
);
--> statement-breakpoint
ALTER TABLE "solicitation_document" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "answer" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"requirement_id" uuid NOT NULL,
	"coverage" "coverage" NOT NULL,
	"text" text,
	"provenance_document_id" uuid,
	"provenance_page" integer,
	"provenance_span" jsonb,
	"vendor_attested" boolean DEFAULT false NOT NULL,
	"reopened_by_amendment_id" uuid
);
--> statement-breakpoint
ALTER TABLE "answer" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "derived_cost" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"scenario_id" uuid NOT NULL,
	"amount" numeric(16, 2) NOT NULL,
	"computed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "derived_cost" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "exception" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"requirement_id" uuid,
	"text" text NOT NULL,
	"price_delta" numeric(14, 2)
);
--> statement-breakpoint
ALTER TABLE "exception" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "participation" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"vendor_org_id" uuid NOT NULL,
	"state" "participation_state" DEFAULT 'invited' NOT NULL,
	"acknowledged_at" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "participation" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "price_quote" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"price_line_id" uuid NOT NULL,
	"rate_structure" jsonb NOT NULL
);
--> statement-breakpoint
ALTER TABLE "price_quote" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "response" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"vendor_org_id" uuid NOT NULL,
	"submitted_at" timestamp with time zone,
	"attested_by" uuid,
	"attested_at" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "response" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "response_document" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"blob_ref" text NOT NULL,
	"sha256" text NOT NULL
);
--> statement-breakpoint
ALTER TABLE "response_document" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "gate_result" (
	"response_id" uuid NOT NULL,
	"requirement_id" uuid NOT NULL,
	"passed" boolean NOT NULL,
	"computed_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "gate_result" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "rubric_amendment" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"reason" text NOT NULL,
	"voided_score_ids" uuid[] DEFAULT '{}' NOT NULL,
	"authored_by" uuid NOT NULL,
	"authored_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "score" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"criterion_id" uuid NOT NULL,
	"points" numeric(8, 2) NOT NULL,
	"committed_by" uuid NOT NULL,
	"committed_at" timestamp with time zone DEFAULT now() NOT NULL,
	"derived_from_suggestion_id" uuid,
	"voided_by_amendment_id" uuid
);
--> statement-breakpoint
ALTER TABLE "score" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "suggestion" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"response_id" uuid NOT NULL,
	"criterion_id" uuid NOT NULL,
	"proposed_points" numeric(8, 2),
	"anchor_matched" text,
	"citations" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"confidence" numeric(4, 3),
	"model_version" text NOT NULL,
	"no_evidence_found" boolean DEFAULT false NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "suggestion" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "export_event" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"actor_id" uuid NOT NULL,
	"org_id" uuid NOT NULL,
	"capability" text NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"classes" text[] NOT NULL,
	"row_count" integer NOT NULL,
	"at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "release_matrix" (
	"version" integer NOT NULL,
	"class" "disclosure_class" NOT NULL,
	"level" "release_level" NOT NULL,
	"released" text NOT NULL,
	"effective_from" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "release_matrix_version_class_level_pk" PRIMARY KEY("version","class","level")
);
--> statement-breakpoint
CREATE TABLE "document_type" (
	"id" text PRIMARY KEY NOT NULL,
	"label" text NOT NULL,
	"expiry_rule" text
);
--> statement-breakpoint
CREATE TABLE "retailer_acceptance" (
	"document_id" uuid NOT NULL,
	"retailer_org_id" uuid NOT NULL,
	"accepted_at" timestamp with time zone DEFAULT now() NOT NULL,
	"vendor_disclosed_name" text,
	CONSTRAINT "retailer_acceptance_document_id_retailer_org_id_pk" PRIMARY KEY("document_id","retailer_org_id")
);
--> statement-breakpoint
ALTER TABLE "retailer_acceptance" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "vault_document" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"vendor_org_id" uuid NOT NULL,
	"type_id" text NOT NULL,
	"blob_ref" text NOT NULL,
	"sha256" text NOT NULL,
	"expires_at" timestamp with time zone,
	"expiry_state" "expiry_state" DEFAULT 'current' NOT NULL,
	"expiry_computed_at" timestamp with time zone
);
--> statement-breakpoint
ALTER TABLE "vault_document" ENABLE ROW LEVEL SECURITY;--> statement-breakpoint
CREATE TABLE "entitlement" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"beneficiary_org_id" uuid NOT NULL,
	"payer_org_id" uuid NOT NULL,
	"tier" "tier" NOT NULL,
	"seats" integer,
	"started_at" timestamp with time zone DEFAULT now() NOT NULL,
	"lapsed_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "confirmation_event" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"requirement_id" uuid NOT NULL,
	"confirmed_by" uuid NOT NULL,
	"corrected" text,
	"at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "sweep_finding" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"document_id" uuid NOT NULL,
	"kind" "sweep_finding_kind" NOT NULL,
	"page" integer NOT NULL,
	"span" jsonb NOT NULL,
	"quoted_text" text NOT NULL,
	"resolved_by_requirement_id" uuid,
	"dismissed_by" uuid,
	"dismissed_at" timestamp with time zone
);
--> statement-breakpoint
CREATE TABLE "vendor_omission_report" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"solicitation_id" uuid NOT NULL,
	"vendor_org_id" uuid NOT NULL,
	"body" text NOT NULL,
	"reported_at" timestamp with time zone DEFAULT now() NOT NULL,
	"acted_on_at" timestamp with time zone,
	"acted_on_by" uuid
);
--> statement-breakpoint
ALTER TABLE "membership" ADD CONSTRAINT "membership_actor_id_actor_id_fk" FOREIGN KEY ("actor_id") REFERENCES "public"."actor"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "membership" ADD CONSTRAINT "membership_org_id_organization_id_fk" FOREIGN KEY ("org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "org_capability" ADD CONSTRAINT "org_capability_org_id_organization_id_fk" FOREIGN KEY ("org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "roster_entry" ADD CONSTRAINT "roster_entry_retailer_org_id_organization_id_fk" FOREIGN KEY ("retailer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "roster_entry" ADD CONSTRAINT "roster_entry_claimed_vendor_org_id_organization_id_fk" FOREIGN KEY ("claimed_vendor_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "category_change_event" ADD CONSTRAINT "category_change_event_retailer_org_id_organization_id_fk" FOREIGN KEY ("retailer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "eligibility" ADD CONSTRAINT "eligibility_retailer_org_id_organization_id_fk" FOREIGN KEY ("retailer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "eligibility" ADD CONSTRAINT "eligibility_vendor_org_id_organization_id_fk" FOREIGN KEY ("vendor_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "eligibility" ADD CONSTRAINT "eligibility_local_category_id_local_category_id_fk" FOREIGN KEY ("local_category_id") REFERENCES "public"."local_category"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "local_category" ADD CONSTRAINT "local_category_retailer_org_id_organization_id_fk" FOREIGN KEY ("retailer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "local_category" ADD CONSTRAINT "local_category_spine_leaf_id_spine_node_id_fk" FOREIGN KEY ("spine_leaf_id") REFERENCES "public"."spine_node"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "criterion" ADD CONSTRAINT "criterion_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "criterion_requirement" ADD CONSTRAINT "criterion_requirement_criterion_id_criterion_id_fk" FOREIGN KEY ("criterion_id") REFERENCES "public"."criterion"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "criterion_requirement" ADD CONSTRAINT "criterion_requirement_requirement_id_requirement_id_fk" FOREIGN KEY ("requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "evaluation_scenario" ADD CONSTRAINT "evaluation_scenario_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "price_line" ADD CONSTRAINT "price_line_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "requirement" ADD CONSTRAINT "requirement_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "requirement" ADD CONSTRAINT "requirement_provenance_document_id_solicitation_document_id_fk" FOREIGN KEY ("provenance_document_id") REFERENCES "public"."solicitation_document"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "scenario_quantity" ADD CONSTRAINT "scenario_quantity_scenario_id_evaluation_scenario_id_fk" FOREIGN KEY ("scenario_id") REFERENCES "public"."evaluation_scenario"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "scenario_quantity" ADD CONSTRAINT "scenario_quantity_price_line_id_price_line_id_fk" FOREIGN KEY ("price_line_id") REFERENCES "public"."price_line"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "solicitation" ADD CONSTRAINT "solicitation_issuer_org_id_organization_id_fk" FOREIGN KEY ("issuer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "solicitation" ADD CONSTRAINT "solicitation_local_category_id_local_category_id_fk" FOREIGN KEY ("local_category_id") REFERENCES "public"."local_category"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "solicitation_amendment" ADD CONSTRAINT "solicitation_amendment_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "solicitation_document" ADD CONSTRAINT "solicitation_document_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "answer" ADD CONSTRAINT "answer_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "answer" ADD CONSTRAINT "answer_requirement_id_requirement_id_fk" FOREIGN KEY ("requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "answer" ADD CONSTRAINT "answer_provenance_document_id_response_document_id_fk" FOREIGN KEY ("provenance_document_id") REFERENCES "public"."response_document"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "derived_cost" ADD CONSTRAINT "derived_cost_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "derived_cost" ADD CONSTRAINT "derived_cost_scenario_id_evaluation_scenario_id_fk" FOREIGN KEY ("scenario_id") REFERENCES "public"."evaluation_scenario"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "exception" ADD CONSTRAINT "exception_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "exception" ADD CONSTRAINT "exception_requirement_id_requirement_id_fk" FOREIGN KEY ("requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "participation" ADD CONSTRAINT "participation_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "participation" ADD CONSTRAINT "participation_vendor_org_id_organization_id_fk" FOREIGN KEY ("vendor_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "price_quote" ADD CONSTRAINT "price_quote_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "price_quote" ADD CONSTRAINT "price_quote_price_line_id_price_line_id_fk" FOREIGN KEY ("price_line_id") REFERENCES "public"."price_line"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "response" ADD CONSTRAINT "response_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "response" ADD CONSTRAINT "response_vendor_org_id_organization_id_fk" FOREIGN KEY ("vendor_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "response_document" ADD CONSTRAINT "response_document_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "gate_result" ADD CONSTRAINT "gate_result_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "gate_result" ADD CONSTRAINT "gate_result_requirement_id_requirement_id_fk" FOREIGN KEY ("requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "score" ADD CONSTRAINT "score_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "score" ADD CONSTRAINT "score_criterion_id_criterion_id_fk" FOREIGN KEY ("criterion_id") REFERENCES "public"."criterion"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "score" ADD CONSTRAINT "score_derived_from_suggestion_id_suggestion_id_fk" FOREIGN KEY ("derived_from_suggestion_id") REFERENCES "public"."suggestion"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "suggestion" ADD CONSTRAINT "suggestion_response_id_response_id_fk" FOREIGN KEY ("response_id") REFERENCES "public"."response"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "suggestion" ADD CONSTRAINT "suggestion_criterion_id_criterion_id_fk" FOREIGN KEY ("criterion_id") REFERENCES "public"."criterion"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "retailer_acceptance" ADD CONSTRAINT "retailer_acceptance_document_id_vault_document_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."vault_document"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "retailer_acceptance" ADD CONSTRAINT "retailer_acceptance_retailer_org_id_organization_id_fk" FOREIGN KEY ("retailer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vault_document" ADD CONSTRAINT "vault_document_vendor_org_id_organization_id_fk" FOREIGN KEY ("vendor_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vault_document" ADD CONSTRAINT "vault_document_type_id_document_type_id_fk" FOREIGN KEY ("type_id") REFERENCES "public"."document_type"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "entitlement" ADD CONSTRAINT "entitlement_beneficiary_org_id_organization_id_fk" FOREIGN KEY ("beneficiary_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "entitlement" ADD CONSTRAINT "entitlement_payer_org_id_organization_id_fk" FOREIGN KEY ("payer_org_id") REFERENCES "public"."organization"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "confirmation_event" ADD CONSTRAINT "confirmation_event_requirement_id_requirement_id_fk" FOREIGN KEY ("requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sweep_finding" ADD CONSTRAINT "sweep_finding_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sweep_finding" ADD CONSTRAINT "sweep_finding_document_id_solicitation_document_id_fk" FOREIGN KEY ("document_id") REFERENCES "public"."solicitation_document"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "sweep_finding" ADD CONSTRAINT "sweep_finding_resolved_by_requirement_id_requirement_id_fk" FOREIGN KEY ("resolved_by_requirement_id") REFERENCES "public"."requirement"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "vendor_omission_report" ADD CONSTRAINT "vendor_omission_report_solicitation_id_solicitation_id_fk" FOREIGN KEY ("solicitation_id") REFERENCES "public"."solicitation"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE POLICY "scenario_quantity_read" ON "scenario_quantity" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_solicitation(
      (SELECT solicitation_id FROM evaluation_scenario WHERE id = scenario_id)
    ), 'scenario_quantity'));--> statement-breakpoint
CREATE POLICY "answer_read" ON "answer" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_response(response_id), 'answer_text'));--> statement-breakpoint
CREATE POLICY "derived_cost_read" ON "derived_cost" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_response(response_id), 'derived_cost'));--> statement-breakpoint
CREATE POLICY "exception_read" ON "exception" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_response(response_id), 'exception_text'));--> statement-breakpoint
CREATE POLICY "price_quote_read" ON "price_quote" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_response(response_id), 'price_quote'));--> statement-breakpoint
CREATE POLICY "score_read" ON "score" AS PERMISSIVE FOR SELECT TO public USING (class_released(release_level_for_response(response_id), 'own_scoring'));