CREATE TABLE "agent_audit_log" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "team_id" uuid, "agent_name" text NOT NULL, "action_type" text NOT NULL, "description" text NOT NULL, "before_state" jsonb, "after_state" jsonb, "requires_approval" boolean DEFAULT false NOT NULL, "approved" boolean, "approved_by" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agent_group_members" ( "group_id" uuid NOT NULL, "agent_id" uuid NOT NULL, "role_in_group" text DEFAULT 'worker' NOT NULL, "execution_order" integer DEFAULT 0 NOT NULL, CONSTRAINT "agent_group_members_group_id_agent_id_pk" PRIMARY KEY("group_id","agent_id") ); --> statement-breakpoint CREATE TABLE "agent_groups" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "name" text NOT NULL, "description" text, "strategy" text DEFAULT 'parallel' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agent_jobs" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "user_id" uuid NOT NULL, "job_type" text NOT NULL, "status" text DEFAULT 'queued' NOT NULL, "input" jsonb NOT NULL, "output" jsonb, "started_at" timestamp with time zone, "completed_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "agents" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "team_id" uuid, "name" text NOT NULL, "role" text, "system_prompt" text, "model" text DEFAULT 'claude-haiku-4-5' NOT NULL, "tools" jsonb DEFAULT '[]'::jsonb NOT NULL, "config" jsonb DEFAULT '{}'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "cron_schedules" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "created_by" uuid NOT NULL, "name" text NOT NULL, "cron_expr" text NOT NULL, "job_type" text NOT NULL, "job_config" jsonb DEFAULT '{}'::jsonb NOT NULL, "enabled" boolean DEFAULT true NOT NULL, "last_run_at" timestamp with time zone, "next_run_at" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "messages" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "sender_id" uuid NOT NULL, "type" text NOT NULL, "subtype" text NOT NULL, "content" jsonb NOT NULL, "reference_id" uuid, "routing" jsonb, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "proactive_patterns" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "trigger" jsonb NOT NULL, "suggestion_type" text NOT NULL, "template" text NOT NULL, "enabled" boolean DEFAULT true NOT NULL ); --> statement-breakpoint CREATE TABLE "scout_findings" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid, "team_id" uuid, "source" text NOT NULL, "category" text NOT NULL, "title" text NOT NULL, "summary" text, "relevance_score" real DEFAULT 0 NOT NULL, "url" text, "status" text DEFAULT 'new' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "suggestions_log" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "user_id" uuid NOT NULL, "pattern_id" uuid NOT NULL, "context" jsonb NOT NULL, "status" text DEFAULT 'pending' NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "tasks" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "title" text NOT NULL, "description" text, "status" text DEFAULT 'open' NOT NULL, "priority" text DEFAULT 'normal' NOT NULL, "created_by" uuid NOT NULL, "assigned_to" uuid, "parent_task_id" uuid, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "team_entities" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "entity_type" text NOT NULL, "name" text NOT NULL, "properties" jsonb DEFAULT '{}'::jsonb NOT NULL, "shared_by" uuid NOT NULL, "valid_from" timestamp with time zone DEFAULT now() NOT NULL, "valid_to" timestamp with time zone, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "team_members" ( "team_id" uuid NOT NULL, "user_id" uuid NOT NULL, "role" text DEFAULT 'member' NOT NULL, "role_description" text, "interests" jsonb, "joined_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "team_members_team_id_user_id_pk" PRIMARY KEY("team_id","user_id") ); --> statement-breakpoint CREATE TABLE "team_relations" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "source_id" uuid NOT NULL, "target_id" uuid NOT NULL, "relation_type" text NOT NULL, "confidence" real DEFAULT 1 NOT NULL, "properties" jsonb DEFAULT '{}'::jsonb NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "team_resources" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "team_id" uuid NOT NULL, "resource_type" text NOT NULL, "name" text NOT NULL, "description" text, "config" jsonb NOT NULL, "shared_by" uuid NOT NULL, "rating" real DEFAULT 0 NOT NULL, "use_count" integer DEFAULT 0 NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL ); --> statement-breakpoint CREATE TABLE "teams" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "name" text NOT NULL, "slug" text NOT NULL, "owner_id" uuid NOT NULL, "created_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "teams_slug_unique" UNIQUE("slug") ); --> statement-breakpoint CREATE TABLE "users" ( "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, "clerk_id" text NOT NULL, "display_name" text NOT NULL, "email" text NOT NULL, "avatar_url" text, "mind_path" text, "created_at" timestamp with time zone DEFAULT now() NOT NULL, "updated_at" timestamp with time zone DEFAULT now() NOT NULL, CONSTRAINT "users_clerk_id_unique" UNIQUE("clerk_id"), CONSTRAINT "users_email_unique" UNIQUE("email") ); --> statement-breakpoint ALTER TABLE "agent_audit_log" ADD CONSTRAINT "agent_audit_log_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_audit_log" ADD CONSTRAINT "agent_audit_log_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_audit_log" ADD CONSTRAINT "agent_audit_log_approved_by_users_id_fk" FOREIGN KEY ("approved_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_group_members" ADD CONSTRAINT "agent_group_members_group_id_agent_groups_id_fk" FOREIGN KEY ("group_id") REFERENCES "public"."agent_groups"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_group_members" ADD CONSTRAINT "agent_group_members_agent_id_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agents"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_groups" ADD CONSTRAINT "agent_groups_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_jobs" ADD CONSTRAINT "agent_jobs_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agent_jobs" ADD CONSTRAINT "agent_jobs_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agents" ADD CONSTRAINT "agents_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "agents" ADD CONSTRAINT "agents_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cron_schedules" ADD CONSTRAINT "cron_schedules_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "cron_schedules" ADD CONSTRAINT "cron_schedules_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "messages" ADD CONSTRAINT "messages_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "messages" ADD CONSTRAINT "messages_sender_id_users_id_fk" FOREIGN KEY ("sender_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "scout_findings" ADD CONSTRAINT "scout_findings_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "scout_findings" ADD CONSTRAINT "scout_findings_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "suggestions_log" ADD CONSTRAINT "suggestions_log_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "suggestions_log" ADD CONSTRAINT "suggestions_log_pattern_id_proactive_patterns_id_fk" FOREIGN KEY ("pattern_id") REFERENCES "public"."proactive_patterns"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_created_by_users_id_fk" FOREIGN KEY ("created_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "tasks" ADD CONSTRAINT "tasks_assigned_to_users_id_fk" FOREIGN KEY ("assigned_to") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_entities" ADD CONSTRAINT "team_entities_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_entities" ADD CONSTRAINT "team_entities_shared_by_users_id_fk" FOREIGN KEY ("shared_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_members" ADD CONSTRAINT "team_members_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_members" ADD CONSTRAINT "team_members_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_relations" ADD CONSTRAINT "team_relations_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_relations" ADD CONSTRAINT "team_relations_source_id_team_entities_id_fk" FOREIGN KEY ("source_id") REFERENCES "public"."team_entities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_relations" ADD CONSTRAINT "team_relations_target_id_team_entities_id_fk" FOREIGN KEY ("target_id") REFERENCES "public"."team_entities"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_resources" ADD CONSTRAINT "team_resources_team_id_teams_id_fk" FOREIGN KEY ("team_id") REFERENCES "public"."teams"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "team_resources" ADD CONSTRAINT "team_resources_shared_by_users_id_fk" FOREIGN KEY ("shared_by") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;--> statement-breakpoint ALTER TABLE "teams" ADD CONSTRAINT "teams_owner_id_users_id_fk" FOREIGN KEY ("owner_id") REFERENCES "public"."users"("id") ON DELETE no action ON UPDATE no action;