CREATE TABLE "instance_agents" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "created_at" timestamp with time zone DEFAULT now() NOT NULL,
    "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
    "name" text NOT NULL,
    "description" text,
    "token_prefix" text NOT NULL,
    "token_hash" text NOT NULL,
    "active" boolean DEFAULT true NOT NULL,
    "capabilities" jsonb DEFAULT '{"scan":true,"print":false}'::jsonb NOT NULL,
    "scanner_names" jsonb DEFAULT '[]'::jsonb NOT NULL,
    "printer_names" jsonb DEFAULT '[]'::jsonb NOT NULL,
    "last_seen_at" timestamp with time zone,
    "last_debug_info" jsonb,
    CONSTRAINT "instance_agents_token_hash_unique" UNIQUE("token_hash")
);
--> statement-breakpoint
CREATE TABLE "instance_agent_scan_jobs" (
    "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
    "created_at" timestamp with time zone DEFAULT now() NOT NULL,
    "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
    "tenant_id" bigint NOT NULL,
    "agent_id" uuid NOT NULL,
    "requested_by" uuid,
    "status" text DEFAULT 'pending' NOT NULL,
    "scanner_name" text,
    "requested_filename" text,
    "settings" jsonb DEFAULT '{}'::jsonb NOT NULL,
    "target" jsonb DEFAULT '{}'::jsonb NOT NULL,
    "agent_message" text,
    "attempts" integer DEFAULT 0 NOT NULL,
    "claimed_at" timestamp with time zone,
    "finished_at" timestamp with time zone,
    "file_id" uuid,
    CONSTRAINT "instance_agent_scan_jobs_tenant_id_tenants_id_fk" FOREIGN KEY ("tenant_id") REFERENCES "public"."tenants"("id") ON DELETE cascade ON UPDATE cascade,
    CONSTRAINT "instance_agent_scan_jobs_agent_id_instance_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."instance_agents"("id") ON DELETE cascade ON UPDATE cascade,
    CONSTRAINT "instance_agent_scan_jobs_requested_by_auth_users_id_fk" FOREIGN KEY ("requested_by") REFERENCES "public"."auth_users"("id") ON DELETE set null ON UPDATE cascade,
    CONSTRAINT "instance_agent_scan_jobs_file_id_files_id_fk" FOREIGN KEY ("file_id") REFERENCES "public"."files"("id") ON DELETE set null ON UPDATE cascade
);
--> statement-breakpoint
CREATE INDEX "instance_agent_scan_jobs_agent_status_idx" ON "instance_agent_scan_jobs" USING btree ("agent_id","status","created_at");
--> statement-breakpoint
CREATE INDEX "instance_agent_scan_jobs_tenant_idx" ON "instance_agent_scan_jobs" USING btree ("tenant_id","created_at");
