CREATE TABLE "berth_pdf_versions" (
	"id" text PRIMARY KEY NOT NULL,
	"berth_id" text NOT NULL,
	"version_number" integer NOT NULL,
	"storage_key" text NOT NULL,
	"file_name" text NOT NULL,
	"file_size_bytes" integer NOT NULL,
	"content_sha256" text NOT NULL,
	"uploaded_by" text NOT NULL,
	"uploaded_at" timestamp with time zone DEFAULT now() NOT NULL,
	"download_url_expires_at" timestamp with time zone,
	"parse_results" jsonb
);
--> statement-breakpoint
ALTER TABLE "berths" ADD COLUMN "current_pdf_version_id" text;--> statement-breakpoint
ALTER TABLE "berth_pdf_versions" ADD CONSTRAINT "berth_pdf_versions_berth_id_berths_id_fk" FOREIGN KEY ("berth_id") REFERENCES "public"."berths"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE UNIQUE INDEX "berth_pdf_versions_berth_version_idx" ON "berth_pdf_versions" USING btree ("berth_id","version_number");--> statement-breakpoint
CREATE INDEX "idx_bpv_berth" ON "berth_pdf_versions" USING btree ("berth_id","uploaded_at");--> statement-breakpoint
-- berths.current_pdf_version_id -> berth_pdf_versions.id (added after both tables
-- exist to break the circular FK declaration; ON DELETE SET NULL so deleting the
-- pointed-at row keeps the berth and just clears the pointer).
ALTER TABLE "berths" ADD CONSTRAINT "berths_current_pdf_version_id_fk"
  FOREIGN KEY ("current_pdf_version_id") REFERENCES "public"."berth_pdf_versions"("id")
  ON DELETE SET NULL ON UPDATE NO ACTION;