ALTER TABLE "payment_batch_receipt_attachments" ADD COLUMN "receipt_ocr_version" INTEGER NOT NULL DEFAULT 0; ALTER TABLE "payment_batch_receipt_attachments" ADD COLUMN "receipt_ocr_state" VARCHAR(32) NOT NULL DEFAULT 'legacy', ADD COLUMN "receipt_ocr_started_at" TIMESTAMP(3); CREATE TABLE "payment_receipt_verifications" ( "id" UUID NOT NULL, "attachment_id" UUID NOT NULL, "source_sha256" VARCHAR(64) NOT NULL, "source_version" INTEGER NOT NULL, "status" VARCHAR(32) NOT NULL, "pages_expected" INTEGER NOT NULL, "pages_processed" INTEGER NOT NULL, "normalized_encrypted" TEXT NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "payment_receipt_verifications_pkey" PRIMARY KEY ("id"), CONSTRAINT "payment_receipt_verifications_pages_check" CHECK ( pages_expected >= 0 AND pages_processed BETWEEN 0 AND pages_expected ), CONSTRAINT "payment_receipt_verifications_attachment_id_fkey" FOREIGN KEY ("attachment_id") REFERENCES "payment_batch_receipt_attachments"("id") ON DELETE RESTRICT ON UPDATE CASCADE ); CREATE INDEX "payment_receipt_verifications_attachment_id_created_at_idx" ON "payment_receipt_verifications"("attachment_id", "created_at"); CREATE TABLE "payment_receipt_matches" ( "id" UUID NOT NULL, "verification_id" UUID NOT NULL, "item_id" UUID NOT NULL, "page_number" INTEGER NOT NULL CHECK (page_number BETWEEN 1 AND 20), "fingerprint" VARCHAR(64) NOT NULL, "key_id" VARCHAR(64) NOT NULL, CONSTRAINT "payment_receipt_matches_pkey" PRIMARY KEY ("id"), CONSTRAINT "payment_receipt_matches_verification_id_fkey" FOREIGN KEY ("verification_id") REFERENCES "payment_receipt_verifications"("id") ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT "payment_receipt_matches_item_id_fkey" FOREIGN KEY ("item_id") REFERENCES "payment_batch_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE ); CREATE UNIQUE INDEX "payment_receipt_matches_verification_id_page_number_key" ON "payment_receipt_matches"("verification_id", "page_number"); CREATE UNIQUE INDEX "payment_receipt_matches_verification_id_item_id_key" ON "payment_receipt_matches"("verification_id", "item_id"); CREATE INDEX "payment_receipt_matches_item_id_idx" ON "payment_receipt_matches"("item_id"); CREATE TABLE "payment_receipt_claims" ( "fingerprint" VARCHAR(64) NOT NULL, "key_id" VARCHAR(64) NOT NULL, "item_id" UUID NOT NULL, "verification_id" UUID NOT NULL, "created_at" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, CONSTRAINT "payment_receipt_claims_pkey" PRIMARY KEY ("fingerprint"), CONSTRAINT "payment_receipt_claims_item_id_fkey" FOREIGN KEY ("item_id") REFERENCES "payment_batch_items"("id") ON DELETE RESTRICT ON UPDATE CASCADE, CONSTRAINT "payment_receipt_claims_verification_id_fkey" FOREIGN KEY ("verification_id") REFERENCES "payment_receipt_verifications"("id") ON DELETE RESTRICT ON UPDATE CASCADE ); CREATE UNIQUE INDEX "payment_receipt_claims_item_id_key" ON "payment_receipt_claims"("item_id");