-- RFC-003 Phase 0 (GOO-187): projection_offset table.
--
-- Idempotency contract for CQRS projectors. Every projector dispatch
-- wraps `apply()` in a transaction that inserts (event_id, handler_name)
-- here. Re-deliveries hit the composite primary key, roll back, and the
-- projector observes a no-op.
--
-- Port:             apps/api/src/modules/read-models/domain/projection-offset-store.ts
-- Prisma adapter:   apps/api/src/modules/read-models/infrastructure/prisma-projection-offset-store.ts
-- Test harness:     apps/api/src/modules/read-models/testing/

-- CreateTable
CREATE TABLE "projection_offset" (
    "eventId" TEXT NOT NULL,
    "handlerName" TEXT NOT NULL,
    "appliedAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
    "payloadHash" TEXT,

    CONSTRAINT "projection_offset_pkey" PRIMARY KEY ("eventId", "handlerName")
);

-- CreateIndex (handler-scoped scans for reconciliation tooling)
CREATE INDEX "projection_offset_handlerName_appliedAt_idx" ON "projection_offset"("handlerName", "appliedAt" DESC);
