import {
  jobRuns
} from "./chunk-OKXZ63IA.js";
import {
  domainEvents
} from "./chunk-OFRRBC7M.js";

// runtime/subsystems/bridge/bridge-delivery.schema.ts
import {
  index,
  jsonb,
  pgEnum,
  pgTable,
  text,
  timestamp,
  unique,
  uuid
} from "drizzle-orm/pg-core";
import { sql } from "drizzle-orm";
var bridgeDeliveryStatusEnum = pgEnum("bridge_delivery_status", [
  "pending",
  "delivered",
  "skipped",
  "failed"
]);
var bridgeDelivery = pgTable(
  "bridge_delivery",
  {
    id: uuid("id").primaryKey().defaultRandom(),
    /** FK to the source event in the outbox. */
    eventId: uuid("event_id").notNull().references(() => domainEvents.id),
    /**
     * Stable codegen-emitted identifier for the (job, trigger) pair, of the
     * form `<job_type>#<triggerIndex>` (BRIDGE-6). Forms the second half of
     * the UNIQUE idempotency key.
     */
    triggerId: text("trigger_id").notNull(),
    /**
     * Wrapper `job_run.id` (the framework `@framework/bridge_delivery` run
     * that drove this delivery). Nullable: the facade-eager path
     * (`publishAndStart` Case B) pre-writes a delivered row with no wrapper.
     */
    wrapperRunId: uuid("wrapper_run_id").references(() => jobRuns.id),
    /**
     * Spawned user `job_run.id`. Null until status is `delivered`; remains
     * null for `skipped` and `failed` deliveries.
     */
    userRunId: uuid("user_run_id").references(() => jobRuns.id),
    status: bridgeDeliveryStatusEnum("status").notNull().default("pending"),
    /** Populated when status=`skipped` (e.g. `'when_returned_false'`, `'trigger_unregistered'`). */
    skipReason: text("skip_reason"),
    /** Populated when status=`failed`. Mirrors `job_run.error` shape. */
    error: jsonb("error").$type(),
    /**
     * Emitted unconditionally and nullable (JOB-8 / SYNC-6 precedent).
     * Enforcement gated on `BRIDGE_MULTI_TENANT` at the service layer
     * (BRIDGE-8); no DB constraint.
     */
    tenantId: text("tenant_id"),
    attemptedAt: timestamp("attempted_at", { withTimezone: true }).notNull().defaultNow(),
    deliveredAt: timestamp("delivered_at", { withTimezone: true })
  },
  (t) => ({
    /**
     * Idempotency ledger. Outbox replays and facade-vs-drain collisions both
     * dedup through this constraint.
     */
    uqBridgeDeliveryEventTrigger: unique("uq_bridge_delivery_event_trigger").on(
      t.eventId,
      t.triggerId
    ),
    /** Lookup all deliveries for an event (fanout report, debugging). */
    idxBridgeDeliveryEvent: index("idx_bridge_delivery_event").on(t.eventId),
    /**
     * Ops dashboard filter — only the actionable states. Partial index keeps
     * it small at scale (the bulk of rows will be `delivered`).
     */
    idxBridgeDeliveryStatus: index("idx_bridge_delivery_status").on(t.status).where(sql`${t.status} IN ('pending','failed')`),
    /**
     * Reverse lookup from a spawned user run back to its delivery row.
     * Partial — most rows in the bridge ledger but only successful
     * deliveries have a `user_run_id`.
     */
    idxBridgeDeliveryUserRun: index("idx_bridge_delivery_user_run").on(t.userRunId).where(sql`${t.userRunId} IS NOT NULL`)
  })
);

export {
  bridgeDeliveryStatusEnum,
  bridgeDelivery
};
//# sourceMappingURL=chunk-2TVVBC53.js.map