-- DB schema standardization migration
-- 1. Rename columns
ALTER TABLE `projects` RENAME COLUMN `archived_at` TO `deleted_at`;
ALTER TABLE `models` RENAME COLUMN `model_id` TO `external_id`;

-- 2. Add deleted_at to remaining tables
ALTER TABLE `providers` ADD COLUMN `deleted_at` text;
ALTER TABLE `models` ADD COLUMN `deleted_at` text;
ALTER TABLE `conversations` ADD COLUMN `deleted_at` text;
ALTER TABLE `materials` ADD COLUMN `deleted_at` text;
ALTER TABLE `messages` ADD COLUMN `deleted_at` text;

-- 3. Add updated_at to messages
ALTER TABLE `messages` ADD COLUMN `updated_at` text NOT NULL DEFAULT '';

-- 4. Drop unique indexes (enforcement moves to app layer)
DROP INDEX IF EXISTS `projects_name_unique`;
DROP INDEX IF EXISTS `providers_name_unique`;
DROP INDEX IF EXISTS `models_provider_id_model_id_unique`;

-- 5. Rebuild messages table (FK cascade → no action, add updated_at + deleted_at in-table, add CHECK on role)
CREATE TABLE `messages_new` (
	`id` text PRIMARY KEY NOT NULL,
	`conversation_id` text NOT NULL,
	`role` text NOT NULL CHECK (`role` IN ('assistant', 'system', 'user')),
	`content` text NOT NULL DEFAULT '',
	`parts` text,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL DEFAULT '',
	`deleted_at` text,
	FOREIGN KEY (`conversation_id`) REFERENCES `conversations`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `messages_new` (`id`, `conversation_id`, `role`, `content`, `parts`, `created_at`, `updated_at`, `deleted_at`)
SELECT `id`, `conversation_id`, `role`, `content`, `parts`, `created_at`, '', NULL FROM `messages`;
--> statement-breakpoint
DROP TABLE `messages`;
--> statement-breakpoint
ALTER TABLE `messages_new` RENAME TO `messages`;
--> statement-breakpoint
CREATE INDEX `messages_conversation_id_idx` ON `messages` (`conversation_id`);
--> statement-breakpoint

-- 6. Rebuild conversations table (model_id nullable, add deleted_at in-table)
CREATE TABLE `conversations_new` (
	`id` text PRIMARY KEY NOT NULL,
	`project_id` text NOT NULL,
	`model_id` text,
	`title` text NOT NULL DEFAULT '新会话',
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	FOREIGN KEY (`model_id`) REFERENCES `models`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `conversations_new` (`id`, `project_id`, `model_id`, `title`, `created_at`, `updated_at`, `deleted_at`)
SELECT `id`, `project_id`, `model_id`, `title`, `created_at`, `updated_at`, NULL FROM `conversations`;
--> statement-breakpoint
DROP TABLE `conversations`;
--> statement-breakpoint
ALTER TABLE `conversations_new` RENAME TO `conversations`;
--> statement-breakpoint
CREATE INDEX `conversations_project_id_idx` ON `conversations` (`project_id`);
--> statement-breakpoint
CREATE INDEX `conversations_model_id_idx` ON `conversations` (`model_id`);
--> statement-breakpoint

-- 7. Rebuild providers table (add deleted_at in-table, add CHECK on type)
CREATE TABLE `providers_new` (
	`id` text PRIMARY KEY NOT NULL,
	`name` text NOT NULL,
	`type` text NOT NULL DEFAULT 'openai-compatible' CHECK (`type` IN ('anthropic', 'openai', 'openai-compatible')),
	`api_key` text NOT NULL,
	`base_url` text NOT NULL,
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	`deleted_at` text
);
--> statement-breakpoint
INSERT INTO `providers_new` (`id`, `name`, `type`, `api_key`, `base_url`, `created_at`, `updated_at`, `deleted_at`)
SELECT `id`, `name`, `type`, `api_key`, `base_url`, `created_at`, `updated_at`, NULL FROM `providers`;
--> statement-breakpoint
DROP TABLE `providers`;
--> statement-breakpoint
ALTER TABLE `providers_new` RENAME TO `providers`;
--> statement-breakpoint

-- 8. Rebuild materials table (add deleted_at in-table, add CHECK on status)
CREATE TABLE `materials_new` (
	`id` text PRIMARY KEY NOT NULL,
	`project_id` text NOT NULL,
	`associated_date` text NOT NULL,
	`description` text NOT NULL,
	`status` text NOT NULL DEFAULT 'pending' CHECK (`status` IN ('pending', 'approved', 'discarded')),
	`created_at` text NOT NULL,
	`updated_at` text NOT NULL,
	`deleted_at` text,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
INSERT INTO `materials_new` (`id`, `project_id`, `associated_date`, `description`, `status`, `created_at`, `updated_at`, `deleted_at`)
SELECT `id`, `project_id`, `associated_date`, `description`, `status`, `created_at`, `updated_at`, NULL FROM `materials`;
--> statement-breakpoint
DROP TABLE `materials`;
--> statement-breakpoint
ALTER TABLE `materials_new` RENAME TO `materials`;
--> statement-breakpoint
CREATE INDEX `materials_project_id_idx` ON `materials` (`project_id`);
