CREATE TABLE `agent_access_rules` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`subject_type` text NOT NULL,
	`subject_id` text NOT NULL,
	`can_view` integer DEFAULT true NOT NULL,
	`can_edit` integer DEFAULT false NOT NULL,
	`can_test` integer DEFAULT false NOT NULL,
	`can_publish` integer DEFAULT false NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_access_subject` ON `agent_access_rules` (`agent_id`,`subject_type`,`subject_id`);--> statement-breakpoint
CREATE INDEX `idx_agent_access_workspace` ON `agent_access_rules` (`workspace_id`,`agent_id`);--> statement-breakpoint
CREATE TABLE `agent_channel_assignments` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`channel_id` text NOT NULL,
	`status` text DEFAULT 'assigned' NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`channel_id`) REFERENCES `channels`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_channel_assignment` ON `agent_channel_assignments` (`agent_id`,`channel_id`);--> statement-breakpoint
CREATE INDEX `idx_agent_channels_workspace` ON `agent_channel_assignments` (`workspace_id`,`agent_id`);--> statement-breakpoint
CREATE TABLE `agent_errors` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`category` text NOT NULL,
	`error_code` text NOT NULL,
	`summary` text NOT NULL,
	`resolution_hint` text DEFAULT '' NOT NULL,
	`status` text DEFAULT 'open' NOT NULL,
	`occurred_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`resolved_at` text,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_agent_errors_agent_status` ON `agent_errors` (`workspace_id`,`agent_id`,`status`,`occurred_at`);--> statement-breakpoint
CREATE TABLE `agent_test_cases` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`name` text NOT NULL,
	`input` text NOT NULL,
	`expected_behavior` text DEFAULT '' NOT NULL,
	`expected_action` text,
	`expected_handoff` integer DEFAULT false NOT NULL,
	`expected_source_id` text,
	`last_status` text DEFAULT 'not_run' NOT NULL,
	`last_run_at` text,
	`created_by` text NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`expected_source_id`) REFERENCES `knowledge_sources`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE INDEX `idx_agent_test_cases_agent` ON `agent_test_cases` (`workspace_id`,`agent_id`,`created_at`);--> statement-breakpoint
CREATE TABLE `agent_usage_budgets` (
	`agent_id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`monthly_credits` integer,
	`alert_threshold` integer DEFAULT 80 NOT NULL,
	`limit_policy` text DEFAULT 'notify' NOT NULL,
	`updated_by` text NOT NULL,
	`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_agent_usage_budget_workspace` ON `agent_usage_budgets` (`workspace_id`);--> statement-breakpoint
ALTER TABLE `conversations` ADD `agent_id` text REFERENCES agents(id);--> statement-breakpoint
ALTER TABLE `conversations` ADD `agent_version_id` text;--> statement-breakpoint
CREATE INDEX `idx_conversations_workspace_agent` ON `conversations` (`workspace_id`,`agent_id`,`updated_at`);--> statement-breakpoint
ALTER TABLE `customer_leads` ADD `created_by_agent_id` text REFERENCES agents(id);--> statement-breakpoint
CREATE INDEX `idx_customer_leads_workspace_agent` ON `customer_leads` (`workspace_id`,`created_by_agent_id`);--> statement-breakpoint
ALTER TABLE `workspace_user_preferences` ADD `agents_view` text DEFAULT 'grid' NOT NULL;--> statement-breakpoint
PRAGMA optimize;
