CREATE TABLE "agents" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" varchar(255) NOT NULL,
	"type" varchar(50) NOT NULL,
	"status" varchar(30) DEFAULT 'idle' NOT NULL,
	"current_task_id" uuid,
	"current_pipeline_id" uuid,
	"last_heartbeat" timestamp with time zone,
	"uptime_seconds" integer DEFAULT 0,
	"error_count_24h" integer DEFAULT 0,
	"capabilities" jsonb DEFAULT '[]'::jsonb,
	"config" jsonb DEFAULT '{}'::jsonb,
	"tasks_completed_total" integer DEFAULT 0,
	"avg_task_duration_seconds" 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 "approvals" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"task_id" uuid,
	"pipeline_id" uuid,
	"stage_name" varchar(50) NOT NULL,
	"type" varchar(30) NOT NULL,
	"status" varchar(30) DEFAULT 'pending' NOT NULL,
	"approved_by" uuid,
	"approved_at" timestamp with time zone,
	"rejection_reason" text,
	"conditions" jsonb DEFAULT '[]'::jsonb,
	"required_approvers" integer DEFAULT 1,
	"current_approvers" integer DEFAULT 0,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "assets" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"pipeline_id" uuid,
	"stage_name" varchar(50),
	"type" varchar(30) NOT NULL,
	"name" varchar(500) NOT NULL,
	"path" varchar(1000),
	"storage_key" varchar(1000) NOT NULL,
	"storage_bucket" varchar(255),
	"size_bytes" bigint,
	"content_type" varchar(255),
	"checksum_sha256" varchar(64),
	"version" integer DEFAULT 1,
	"previous_version_id" uuid,
	"generated_by" uuid,
	"generator_type" varchar(30),
	"metadata" jsonb DEFAULT '{}'::jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "audit_log" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"actor_type" varchar(30) NOT NULL,
	"actor_id" uuid,
	"actor_name" varchar(255),
	"action" varchar(100) NOT NULL,
	"entity_type" varchar(50) NOT NULL,
	"entity_id" uuid NOT NULL,
	"changes" jsonb DEFAULT '{}'::jsonb,
	"metadata" jsonb DEFAULT '{}'::jsonb,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "notifications" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"user_id" uuid,
	"type" varchar(50) NOT NULL,
	"title" varchar(500) NOT NULL,
	"body" text,
	"data" jsonb DEFAULT '{}'::jsonb,
	"channels" jsonb DEFAULT '["dashboard"]'::jsonb,
	"delivered_via" jsonb DEFAULT '[]'::jsonb,
	"read" boolean DEFAULT false,
	"read_at" timestamp with time zone,
	"dismissed" boolean DEFAULT false,
	"entity_type" varchar(50),
	"entity_id" uuid,
	"action_url" varchar(1000),
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "pipeline_stages" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"pipeline_id" uuid NOT NULL,
	"stage_name" varchar(50) NOT NULL,
	"stage_order" integer NOT NULL,
	"status" varchar(30) DEFAULT 'pending' NOT NULL,
	"requires_approval" boolean DEFAULT false,
	"approval_type" varchar(30),
	"auto_advance" boolean DEFAULT false,
	"validation_rules" jsonb DEFAULT '{}'::jsonb,
	"entered_at" timestamp with time zone,
	"completed_at" timestamp with time zone,
	"duration_seconds" integer,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
CREATE TABLE "pipelines" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"name" varchar(255) NOT NULL,
	"slug" varchar(255) NOT NULL,
	"template" varchar(100) DEFAULT 'mcp-server-standard' NOT NULL,
	"platform" varchar(100),
	"current_stage" varchar(50) DEFAULT 'intake' NOT NULL,
	"status" varchar(30) DEFAULT 'active' NOT NULL,
	"priority" varchar(20) DEFAULT 'medium' NOT NULL,
	"created_by" uuid,
	"assignee_id" uuid,
	"config" jsonb DEFAULT '{}'::jsonb,
	"metadata" jsonb DEFAULT '{}'::jsonb,
	"sla_deadline" timestamp with time zone,
	"started_at" timestamp with time zone DEFAULT now(),
	"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 "pipelines_slug_unique" UNIQUE("slug")
);
--> statement-breakpoint
CREATE TABLE "tasks" (
	"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
	"pipeline_id" uuid,
	"stage_name" varchar(50),
	"type" varchar(50) NOT NULL,
	"title" varchar(500) NOT NULL,
	"description" text,
	"context" jsonb DEFAULT '{}'::jsonb,
	"status" varchar(30) DEFAULT 'pending' NOT NULL,
	"priority" varchar(20) DEFAULT 'medium' NOT NULL,
	"assignee_id" uuid,
	"claimed_at" timestamp with time zone,
	"claimed_by" uuid,
	"decision" varchar(30),
	"decision_notes" text,
	"decision_data" jsonb DEFAULT '{}'::jsonb,
	"decided_at" timestamp with time zone,
	"decided_by" uuid,
	"sla_deadline" timestamp with time zone,
	"sla_warning_sent" boolean DEFAULT false,
	"sla_breached" boolean DEFAULT false,
	"escalation_level" integer DEFAULT 0,
	"blocks_stage_advance" boolean DEFAULT false,
	"blocks_pipeline_id" uuid,
	"created_at" timestamp with time zone DEFAULT now() NOT NULL,
	"updated_at" timestamp with time zone DEFAULT now() NOT NULL
);
--> statement-breakpoint
ALTER TABLE "agents" ADD CONSTRAINT "agents_current_task_id_tasks_id_fk" FOREIGN KEY ("current_task_id") REFERENCES "public"."tasks"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "agents" ADD CONSTRAINT "agents_current_pipeline_id_pipelines_id_fk" FOREIGN KEY ("current_pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "approvals" ADD CONSTRAINT "approvals_task_id_tasks_id_fk" FOREIGN KEY ("task_id") REFERENCES "public"."tasks"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "approvals" ADD CONSTRAINT "approvals_pipeline_id_pipelines_id_fk" FOREIGN KEY ("pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "assets" ADD CONSTRAINT "assets_pipeline_id_pipelines_id_fk" FOREIGN KEY ("pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "pipeline_stages" ADD CONSTRAINT "pipeline_stages_pipeline_id_pipelines_id_fk" FOREIGN KEY ("pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_pipeline_id_pipelines_id_fk" FOREIGN KEY ("pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE "tasks" ADD CONSTRAINT "tasks_blocks_pipeline_id_pipelines_id_fk" FOREIGN KEY ("blocks_pipeline_id") REFERENCES "public"."pipelines"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX "idx_agents_status" ON "agents" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_agents_type" ON "agents" USING btree ("type");--> statement-breakpoint
CREATE INDEX "idx_approvals_status" ON "approvals" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_approvals_pipeline" ON "approvals" USING btree ("pipeline_id");--> statement-breakpoint
CREATE INDEX "idx_approvals_task" ON "approvals" USING btree ("task_id");--> statement-breakpoint
CREATE INDEX "idx_assets_pipeline" ON "assets" USING btree ("pipeline_id");--> statement-breakpoint
CREATE INDEX "idx_assets_type" ON "assets" USING btree ("type");--> statement-breakpoint
CREATE INDEX "idx_assets_stage" ON "assets" USING btree ("pipeline_id","stage_name");--> statement-breakpoint
CREATE INDEX "idx_audit_entity" ON "audit_log" USING btree ("entity_type","entity_id");--> statement-breakpoint
CREATE INDEX "idx_audit_action" ON "audit_log" USING btree ("action");--> statement-breakpoint
CREATE INDEX "idx_audit_actor" ON "audit_log" USING btree ("actor_type","actor_id");--> statement-breakpoint
CREATE INDEX "idx_audit_time" ON "audit_log" USING btree ("created_at");--> statement-breakpoint
CREATE INDEX "idx_notifications_user_unread" ON "notifications" USING btree ("user_id","read");--> statement-breakpoint
CREATE INDEX "idx_notifications_entity" ON "notifications" USING btree ("entity_type","entity_id");--> statement-breakpoint
CREATE UNIQUE INDEX "idx_pipeline_stage_unique" ON "pipeline_stages" USING btree ("pipeline_id","stage_name");--> statement-breakpoint
CREATE INDEX "idx_pipeline_stages_pipeline" ON "pipeline_stages" USING btree ("pipeline_id");--> statement-breakpoint
CREATE INDEX "idx_pipeline_stages_status" ON "pipeline_stages" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_pipelines_status" ON "pipelines" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_pipelines_stage" ON "pipelines" USING btree ("current_stage");--> statement-breakpoint
CREATE INDEX "idx_pipelines_priority" ON "pipelines" USING btree ("priority");--> statement-breakpoint
CREATE INDEX "idx_pipelines_assignee" ON "pipelines" USING btree ("assignee_id");--> statement-breakpoint
CREATE INDEX "idx_tasks_status" ON "tasks" USING btree ("status");--> statement-breakpoint
CREATE INDEX "idx_tasks_priority_sla" ON "tasks" USING btree ("priority","sla_deadline");--> statement-breakpoint
CREATE INDEX "idx_tasks_assignee" ON "tasks" USING btree ("assignee_id");--> statement-breakpoint
CREATE INDEX "idx_tasks_pipeline" ON "tasks" USING btree ("pipeline_id");--> statement-breakpoint
CREATE INDEX "idx_tasks_pending" ON "tasks" USING btree ("status");