CREATE TABLE "business_event_outbox" (
	"id" text PRIMARY KEY NOT NULL,
	"organization_id" text NOT NULL,
	"event_type" text NOT NULL,
	"schema_version" integer NOT NULL,
	"entity_type" text NOT NULL,
	"entity_id" text NOT NULL,
	"correlation_id" text NOT NULL,
	"causation_id" text,
	"event_envelope" jsonb NOT NULL,
	"delivery_status" text DEFAULT 'pending' NOT NULL,
	"attempt_count" integer DEFAULT 0 NOT NULL,
	"available_at" timestamp with time zone DEFAULT now() NOT NULL,
	"processing_started_at" timestamp with time zone,
	"dispatched_at" timestamp with time zone,
	"failed_at" timestamp with time zone,
	"dead_lettered_at" timestamp with time zone,
	"last_error_code" text,
	"last_error_message" text,
	"occurred_at" timestamp with time zone NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "projection_consumer_checkpoints" (
	"id" text PRIMARY KEY NOT NULL,
	"organization_id" text NOT NULL,
	"consumer_name" text NOT NULL,
	"projection_name" text NOT NULL,
	"event_id" text NOT NULL,
	"event_type" text NOT NULL,
	"schema_version" integer NOT NULL,
	"processing_status" text DEFAULT 'pending' NOT NULL,
	"attempt_count" integer DEFAULT 0 NOT NULL,
	"first_attempted_at" timestamp with time zone,
	"last_attempted_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"next_retry_at" timestamp with time zone,
	"error_code" text,
	"error_message" text,
	"processing_duration_ms" integer,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "projection_dead_letters" (
	"id" text PRIMARY KEY NOT NULL,
	"organization_id" text NOT NULL,
	"consumer_name" text NOT NULL,
	"projection_name" text NOT NULL,
	"event_id" text NOT NULL,
	"event_type" text NOT NULL,
	"schema_version" integer NOT NULL,
	"attempt_count" integer NOT NULL,
	"error_code" text NOT NULL,
	"error_message" text NOT NULL,
	"failure_metadata" jsonb,
	"status" text DEFAULT 'open' NOT NULL,
	"resolved_at" timestamp with time zone,
	"resolved_by" text,
	"resolution_reason" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "projection_health_snapshots" (
	"id" text PRIMARY KEY NOT NULL,
	"organization_id" text NOT NULL,
	"projection_name" text NOT NULL,
	"consumer_name" text NOT NULL,
	"status" text DEFAULT 'healthy' NOT NULL,
	"last_processed_event_id" text,
	"last_successful_processing_at" timestamp with time zone,
	"pending_count" integer DEFAULT 0 NOT NULL,
	"retry_count" integer DEFAULT 0 NOT NULL,
	"dead_letter_count" integer DEFAULT 0 NOT NULL,
	"unsupported_version_count" integer DEFAULT 0 NOT NULL,
	"processing_lag_ms" integer DEFAULT 0 NOT NULL,
	"average_processing_duration_ms" integer DEFAULT 0 NOT NULL,
	"last_rebuild_at" timestamp with time zone,
	"rebuild_source" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "projection_rebuild_runs" (
	"id" text PRIMARY KEY NOT NULL,
	"organization_id" text NOT NULL,
	"projection_name" text NOT NULL,
	"entity_type" text,
	"entity_id" text,
	"occurred_from" timestamp with time zone,
	"occurred_to" timestamp with time zone,
	"dry_run" boolean DEFAULT true NOT NULL,
	"reset_existing" boolean DEFAULT false NOT NULL,
	"source_strategy" text NOT NULL,
	"status" text DEFAULT 'pending' NOT NULL,
	"processed_count" integer DEFAULT 0 NOT NULL,
	"skipped_count" integer DEFAULT 0 NOT NULL,
	"failed_count" integer DEFAULT 0 NOT NULL,
	"error_code" text,
	"error_message" text,
	"requested_by" text NOT NULL,
	"started_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE INDEX "business_event_outbox_status_available_idx" ON "business_event_outbox" USING btree ("delivery_status","available_at");--> statement-breakpoint
CREATE INDEX "business_event_outbox_org_event_idx" ON "business_event_outbox" USING btree ("organization_id","event_type");--> statement-breakpoint
CREATE UNIQUE INDEX "projection_checkpoints_consumer_event_idx" ON "projection_consumer_checkpoints" USING btree ("consumer_name","event_id");--> statement-breakpoint
CREATE INDEX "projection_checkpoints_status_retry_idx" ON "projection_consumer_checkpoints" USING btree ("processing_status","next_retry_at");--> statement-breakpoint
CREATE UNIQUE INDEX "projection_dead_letters_consumer_event_idx" ON "projection_dead_letters" USING btree ("consumer_name","event_id");--> statement-breakpoint
CREATE INDEX "projection_dead_letters_org_status_idx" ON "projection_dead_letters" USING btree ("organization_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "projection_health_org_consumer_idx" ON "projection_health_snapshots" USING btree ("organization_id","consumer_name");--> statement-breakpoint
CREATE INDEX "projection_rebuild_runs_org_projection_idx" ON "projection_rebuild_runs" USING btree ("organization_id","projection_name");