-- Fix agents table FK constraints: initiative_id and account_id need ON DELETE SET NULL
-- SQLite cannot ALTER column constraints, so we must recreate the table

CREATE TABLE `agents_new` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`task_id` text REFERENCES `tasks`(`id`) ON DELETE SET NULL,
	`initiative_id` text REFERENCES `initiatives`(`id`) ON DELETE SET NULL,
	`session_id` text,
	`worktree_id` text NOT NULL,
	`provider` text DEFAULT 'claude' NOT NULL,
	`account_id` text REFERENCES `accounts`(`id`) ON DELETE SET NULL,
	`status` text DEFAULT 'idle' NOT NULL,
	`mode` text DEFAULT 'execute' NOT NULL,
	`pid` integer,
	`exit_code` integer,
	`output_file_path` text,
	`result` text,
	`pending_questions` text,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	`user_dismissed_at` integer
);--> statement-breakpoint
INSERT INTO `agents_new` SELECT
	`id`, `name`, `task_id`, `initiative_id`, `session_id`, `worktree_id`,
	`provider`, `account_id`, `status`, `mode`, `pid`, `exit_code`,
	`output_file_path`, `result`, `pending_questions`,
	`created_at`, `updated_at`, `user_dismissed_at`
FROM `agents`;--> statement-breakpoint
DROP TABLE `agents`;--> statement-breakpoint
ALTER TABLE `agents_new` RENAME TO `agents`;--> statement-breakpoint
CREATE UNIQUE INDEX `agents_name_unique` ON `agents` (`name`);
