CREATE TABLE `agent_actions` (
	`id` text PRIMARY KEY NOT NULL,
	`agent_id` text NOT NULL,
	`action_key` text NOT NULL,
	`approval_mode` text DEFAULT 'disabled' NOT NULL,
	`enabled` integer DEFAULT false NOT NULL,
	`config_json` text DEFAULT '{}' NOT NULL,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_actions_agent_key` ON `agent_actions` (`agent_id`,`action_key`);--> statement-breakpoint
CREATE TABLE `agent_activity_logs` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text,
	`event_type` text NOT NULL,
	`summary` text NOT NULL,
	`metadata_json` text DEFAULT '{}' NOT NULL,
	`actor_user_id` 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
);
--> statement-breakpoint
CREATE INDEX `idx_agent_activity_workspace_agent` ON `agent_activity_logs` (`workspace_id`,`agent_id`,`created_at`);--> statement-breakpoint
CREATE TABLE `agent_guardrails` (
	`id` text PRIMARY KEY NOT NULL,
	`agent_id` text NOT NULL,
	`allowed_topics_json` text DEFAULT '[]' NOT NULL,
	`restricted_topics_json` text DEFAULT '[]' NOT NULL,
	`escalation_rules_json` text DEFAULT '[]' NOT NULL,
	`confidence_threshold` integer DEFAULT 55 NOT NULL,
	`unknown_answer_behavior` text DEFAULT 'handoff' NOT NULL,
	`business_hours_json` text DEFAULT '{"mode":"always"}' NOT NULL,
	`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_guardrails_agent` ON `agent_guardrails` (`agent_id`);--> statement-breakpoint
CREATE TABLE `agent_knowledge_links` (
	`id` text PRIMARY KEY NOT NULL,
	`agent_id` text NOT NULL,
	`source_id` text NOT NULL,
	`enabled` integer DEFAULT true NOT NULL,
	FOREIGN KEY (`agent_id`) REFERENCES `agents`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`source_id`) REFERENCES `knowledge_sources`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_knowledge_pair` ON `agent_knowledge_links` (`agent_id`,`source_id`);--> statement-breakpoint
CREATE TABLE `agent_onboarding_sessions` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text NOT NULL,
	`workspace_id` text,
	`agent_id` text,
	`current_step` text DEFAULT 'company' NOT NULL,
	`completed_steps_json` text DEFAULT '["account"]' NOT NULL,
	`industry` text,
	`goal` text,
	`template_id` text,
	`draft_json` text DEFAULT '{}' NOT NULL,
	`status` text DEFAULT 'in_progress' NOT NULL,
	`idempotency_key` text NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`updated_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 set null,
	FOREIGN KEY (`template_id`) REFERENCES `agent_templates`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_onboarding_idempotency` ON `agent_onboarding_sessions` (`idempotency_key`);--> statement-breakpoint
CREATE INDEX `idx_agent_onboarding_user_status` ON `agent_onboarding_sessions` (`user_id`,`status`,`updated_at`);--> statement-breakpoint
CREATE TABLE `agent_templates` (
	`id` text PRIMARY KEY NOT NULL,
	`slug` text NOT NULL,
	`name_ar` text NOT NULL,
	`name_en` text NOT NULL,
	`industry` text NOT NULL,
	`goal` text NOT NULL,
	`description_ar` text NOT NULL,
	`description_en` text NOT NULL,
	`default_instructions_ar` text NOT NULL,
	`default_instructions_en` text NOT NULL,
	`questions_json` text DEFAULT '[]' NOT NULL,
	`suggested_knowledge_json` text DEFAULT '[]' NOT NULL,
	`actions_json` text DEFAULT '[]' NOT NULL,
	`handoff_rules_json` text DEFAULT '[]' NOT NULL,
	`channels_json` text DEFAULT '["webchat"]' NOT NULL,
	`required_features_json` text DEFAULT '[]' NOT NULL,
	`minimum_plan` text,
	`status` text DEFAULT 'draft' NOT NULL,
	`featured` integer DEFAULT false NOT NULL,
	`sort_order` integer DEFAULT 0 NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_templates_slug` ON `agent_templates` (`slug`);--> statement-breakpoint
CREATE INDEX `idx_agent_templates_industry_goal` ON `agent_templates` (`industry`,`goal`,`status`);--> statement-breakpoint
CREATE TABLE `agent_test_runs` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`scenario` text DEFAULT 'custom' NOT NULL,
	`user_message` text NOT NULL,
	`response` text NOT NULL,
	`intent` text NOT NULL,
	`sources_json` text DEFAULT '[]' NOT NULL,
	`actions_json` text DEFAULT '[]' NOT NULL,
	`handoff_decision` text DEFAULT 'none' NOT NULL,
	`latency_ms` integer DEFAULT 0 NOT NULL,
	`credits_used` integer DEFAULT 0 NOT NULL,
	`status` 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
);
--> statement-breakpoint
CREATE INDEX `idx_agent_test_runs_agent` ON `agent_test_runs` (`agent_id`,`created_at`);--> statement-breakpoint
CREATE TABLE `agent_versions` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`version` integer NOT NULL,
	`state` text DEFAULT 'draft' NOT NULL,
	`config_json` text NOT NULL,
	`change_summary` text DEFAULT '' NOT NULL,
	`updated_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
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_agent_versions_agent_version` ON `agent_versions` (`agent_id`,`version`);--> statement-breakpoint
CREATE INDEX `idx_agent_versions_state` ON `agent_versions` (`agent_id`,`state`);--> statement-breakpoint
CREATE TABLE `knowledge_entries` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`source_id` text NOT NULL,
	`title` text NOT NULL,
	`content` 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 (`source_id`) REFERENCES `knowledge_sources`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE INDEX `idx_knowledge_entries_workspace_source` ON `knowledge_entries` (`workspace_id`,`source_id`);--> statement-breakpoint
CREATE TABLE `onboarding_analytics_events` (
	`id` text PRIMARY KEY NOT NULL,
	`user_id` text,
	`session_hash` text NOT NULL,
	`event_name` text NOT NULL,
	`step` text,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);
--> statement-breakpoint
CREATE INDEX `idx_onboarding_events_name` ON `onboarding_analytics_events` (`event_name`,`created_at`);--> statement-breakpoint
CREATE TABLE `onboarding_settings` (
	`id` text PRIMARY KEY NOT NULL,
	`signup_enabled` integer DEFAULT true NOT NULL,
	`free_agent_enabled` integer DEFAULT false NOT NULL,
	`require_company_name` integer DEFAULT true NOT NULL,
	`default_country` text DEFAULT 'SA' NOT NULL,
	`default_language` text DEFAULT 'ar' NOT NULL,
	`available_industries_json` text DEFAULT '[]' NOT NULL,
	`available_channels_json` text DEFAULT '["webchat"]' NOT NULL,
	`skip_rules_json` text DEFAULT '{}' NOT NULL,
	`updated_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL
);
--> statement-breakpoint
CREATE TABLE `workspace_memberships` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`user_id` text NOT NULL,
	`role` text DEFAULT 'member' NOT NULL,
	`status` text DEFAULT 'active' NOT NULL,
	`created_at` text DEFAULT CURRENT_TIMESTAMP NOT NULL,
	FOREIGN KEY (`workspace_id`) REFERENCES `workspaces`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `idx_workspace_membership_user_workspace` ON `workspace_memberships` (`user_id`,`workspace_id`);--> statement-breakpoint
CREATE INDEX `idx_workspace_membership_user` ON `workspace_memberships` (`user_id`,`status`);
--> statement-breakpoint
PRAGMA optimize;
