CREATE TYPE "public"."attempt_provider" AS ENUM('web_push', 'apns', 'fcm');--> statement-breakpoint
CREATE TYPE "public"."attempt_status" AS ENUM('pending', 'sent', 'failed', 'skipped');--> statement-breakpoint
CREATE TYPE "public"."delivery_status" AS ENUM('accepted', 'processing', 'completed', 'failed', 'partial');--> statement-breakpoint
CREATE TYPE "public"."device_platform" AS ENUM('web', 'ios', 'android');--> statement-breakpoint
CREATE TYPE "public"."device_status" AS ENUM('active', 'disabled', 'invalid');--> statement-breakpoint
CREATE TYPE "public"."instance_status" AS ENUM('active', 'blocked', 'disabled');--> statement-breakpoint
CREATE TYPE "public"."payload_mode" AS ENUM('minimal', 'rich');--> statement-breakpoint
CREATE TABLE "audit_logs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"actor" text NOT NULL,
	"action" text NOT NULL,
	"instance_id" uuid,
	"meta" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "delivery_attempts" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"delivery_job_id" uuid NOT NULL,
	"device_id" uuid NOT NULL,
	"provider" "attempt_provider" NOT NULL,
	"status" "attempt_status" DEFAULT 'pending' NOT NULL,
	"provider_message_id" text,
	"error_code" text,
	"error_message" text,
	"sent_at" timestamp with time zone,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "delivery_jobs" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"delivery_job_id" text NOT NULL,
	"instance_id" uuid NOT NULL,
	"idempotency_key" text NOT NULL,
	"status" "delivery_status" DEFAULT 'accepted' NOT NULL,
	"priority" text DEFAULT 'normal' NOT NULL,
	"collapse_key" text,
	"ttl_seconds" integer DEFAULT 3600 NOT NULL,
	"accepted_count" integer DEFAULT 0 NOT NULL,
	"rejected_count" integer DEFAULT 0 NOT NULL,
	"sent_count" integer DEFAULT 0 NOT NULL,
	"failed_count" integer DEFAULT 0 NOT NULL,
	"last_error_code" text,
	"last_error_message" text,
	"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,
	CONSTRAINT "delivery_jobs_delivery_job_id_unique" UNIQUE("delivery_job_id")
);
--> statement-breakpoint
CREATE TABLE "push_devices" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"central_device_id" text NOT NULL,
	"instance_id" uuid NOT NULL,
	"local_device_id" text NOT NULL,
	"platform" "device_platform" NOT NULL,
	"status" "device_status" DEFAULT 'active' NOT NULL,
	"provider_token_encrypted" text,
	"web_push_subscription" jsonb,
	"meta" jsonb DEFAULT '{}'::jsonb NOT NULL,
	"last_seen_at" timestamp with time zone DEFAULT now() NOT NULL,
	"disabled_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,
	CONSTRAINT "push_devices_central_device_id_unique" UNIQUE("central_device_id")
);
--> statement-breakpoint
CREATE TABLE "push_instances" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"instance_id" text NOT NULL,
	"name" text NOT NULL,
	"base_url" text NOT NULL,
	"status" "instance_status" DEFAULT 'active' NOT NULL,
	"payload_mode" "payload_mode" DEFAULT 'minimal' NOT NULL,
	"capabilities" jsonb DEFAULT '[]'::jsonb NOT NULL,
	"rate_limit_per_minute" integer DEFAULT 120 NOT NULL,
	"daily_quota" integer DEFAULT 10000 NOT NULL,
	"current_secret_encrypted" text NOT NULL,
	"current_secret_preview" text NOT NULL,
	"next_secret_encrypted" text,
	"next_secret_preview" text,
	"notes" text,
	"last_heartbeat_at" timestamp with time zone,
	"last_heartbeat_version" text,
	"last_heartbeat_ip" text,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
	CONSTRAINT "push_instances_instance_id_unique" UNIQUE("instance_id")
);
--> statement-breakpoint
ALTER TABLE "audit_logs" ADD CONSTRAINT "audit_logs_instance_id_push_instances_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."push_instances"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "delivery_attempts" ADD CONSTRAINT "delivery_attempts_delivery_job_id_delivery_jobs_id_fk" FOREIGN KEY ("delivery_job_id") REFERENCES "public"."delivery_jobs"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "delivery_attempts" ADD CONSTRAINT "delivery_attempts_device_id_push_devices_id_fk" FOREIGN KEY ("device_id") REFERENCES "public"."push_devices"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "delivery_jobs" ADD CONSTRAINT "delivery_jobs_instance_id_push_instances_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."push_instances"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "push_devices" ADD CONSTRAINT "push_devices_instance_id_push_instances_id_fk" FOREIGN KEY ("instance_id") REFERENCES "public"."push_instances"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "audit_logs_instance_created_idx" ON "audit_logs" USING btree ("instance_id","created_at");--> statement-breakpoint
CREATE INDEX "delivery_attempts_job_idx" ON "delivery_attempts" USING btree ("delivery_job_id");--> statement-breakpoint
CREATE INDEX "delivery_attempts_device_idx" ON "delivery_attempts" USING btree ("device_id");--> statement-breakpoint
CREATE UNIQUE INDEX "delivery_jobs_delivery_job_id_idx" ON "delivery_jobs" USING btree ("delivery_job_id");--> statement-breakpoint
CREATE UNIQUE INDEX "delivery_jobs_instance_idempotency_idx" ON "delivery_jobs" USING btree ("instance_id","idempotency_key");--> statement-breakpoint
CREATE INDEX "delivery_jobs_instance_created_idx" ON "delivery_jobs" USING btree ("instance_id","created_at");--> statement-breakpoint
CREATE UNIQUE INDEX "push_devices_central_device_id_idx" ON "push_devices" USING btree ("central_device_id");--> statement-breakpoint
CREATE UNIQUE INDEX "push_devices_instance_local_device_idx" ON "push_devices" USING btree ("instance_id","local_device_id");--> statement-breakpoint
CREATE INDEX "push_devices_instance_status_idx" ON "push_devices" USING btree ("instance_id","status");--> statement-breakpoint
CREATE UNIQUE INDEX "push_instances_instance_id_idx" ON "push_instances" USING btree ("instance_id");--> statement-breakpoint
CREATE INDEX "push_instances_status_idx" ON "push_instances" USING btree ("status");