CREATE TABLE `audit_logs` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`actor_user_id` text NOT NULL,
	`action` text NOT NULL,
	`target_label` 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
);
--> statement-breakpoint
CREATE INDEX `idx_audit_logs_workspace_id` ON `audit_logs` (`workspace_id`,`created_at`);--> statement-breakpoint
CREATE TABLE `channels` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`provider` text NOT NULL,
	`name` text NOT NULL,
	`status` text DEFAULT 'setup' NOT NULL,
	`config_label` text DEFAULT 'جاهز للإعداد' 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 INDEX `idx_channels_workspace_id` ON `channels` (`workspace_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `idx_channels_workspace_provider` ON `channels` (`workspace_id`,`provider`);--> statement-breakpoint
CREATE TABLE `knowledge_sources` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`name` text NOT NULL,
	`source_type` text NOT NULL,
	`object_key` text,
	`source_url` text,
	`mime_type` text,
	`size_bytes` integer DEFAULT 0 NOT NULL,
	`status` text DEFAULT 'ready' 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 INDEX `idx_knowledge_sources_workspace_id` ON `knowledge_sources` (`workspace_id`);--> statement-breakpoint
CREATE TABLE `simulation_messages` (
	`id` text PRIMARY KEY NOT NULL,
	`workspace_id` text NOT NULL,
	`agent_id` text NOT NULL,
	`role` text NOT NULL,
	`content` text NOT NULL,
	`intent` text,
	`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_simulation_messages_agent_id` ON `simulation_messages` (`agent_id`,`created_at`);--> statement-breakpoint
ALTER TABLE `agents` ADD `description` text DEFAULT '' NOT NULL;--> statement-breakpoint
ALTER TABLE `agents` ADD `instructions` text DEFAULT '' NOT NULL;--> statement-breakpoint
ALTER TABLE `agents` ADD `tone` text DEFAULT 'ودود واحترافي' NOT NULL;--> statement-breakpoint
ALTER TABLE `agents` ADD `language` text DEFAULT 'العربية' NOT NULL;--> statement-breakpoint
ALTER TABLE `agents` ADD `welcome_message` text DEFAULT 'أهلًا بك، كيف يمكنني مساعدتك؟' NOT NULL;--> statement-breakpoint
ALTER TABLE `agents` ADD `handoff_threshold` integer DEFAULT 45 NOT NULL;