CREATE TABLE `ticket_comments` (
	`id` text PRIMARY KEY,
	`ticket_id` text NOT NULL,
	`actor_id` integer NOT NULL,
	`content` text NOT NULL,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	CONSTRAINT `fk_ticket_comments_ticket_id_tickets_id_fk` FOREIGN KEY (`ticket_id`) REFERENCES `tickets`(`id`) ON DELETE CASCADE,
	CONSTRAINT `fk_ticket_comments_actor_id_actors_id_fk` FOREIGN KEY (`actor_id`) REFERENCES `actors`(`id`) ON DELETE CASCADE
);
--> statement-breakpoint
ALTER TABLE `ticket_messages` ADD `is_private` integer DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `tickets` ADD `thread_id` text;--> statement-breakpoint
PRAGMA foreign_keys=OFF;--> statement-breakpoint
CREATE TABLE `__new_tickets` (
	`id` text PRIMARY KEY,
	`guild_id` integer NOT NULL,
	`local_id` integer NOT NULL,
	`status` text NOT NULL,
	`channel_id` text NOT NULL,
	`thread_id` text,
	`close_reason` text,
	`private_reason` text,
	`opened_at` integer NOT NULL,
	`closed_at` integer,
	`closed_by` integer,
	`opened_by` integer NOT NULL,
	`open_reason` text,
	`subject` integer NOT NULL,
	`claimed_by` integer,
	`claimed_at` integer,
	`thanked_at` integer,
	`warned_at` integer,
	`last_message_at` integer,
	`has_moderator_message` integer,
	`has_author_message` integer,
	CONSTRAINT `fk_tickets_guild_id_guilds_id_fk` FOREIGN KEY (`guild_id`) REFERENCES `guilds`(`id`) ON DELETE CASCADE,
	CONSTRAINT `fk_tickets_closed_by_actors_id_fk` FOREIGN KEY (`closed_by`) REFERENCES `actors`(`id`),
	CONSTRAINT `fk_tickets_opened_by_actors_id_fk` FOREIGN KEY (`opened_by`) REFERENCES `actors`(`id`),
	CONSTRAINT `fk_tickets_subject_actors_id_fk` FOREIGN KEY (`subject`) REFERENCES `actors`(`id`),
	CONSTRAINT `fk_tickets_claimed_by_actors_id_fk` FOREIGN KEY (`claimed_by`) REFERENCES `actors`(`id`)
);
--> statement-breakpoint
INSERT INTO `__new_tickets`(`id`, `guild_id`, `local_id`, `status`, `channel_id`, `close_reason`, `private_reason`, `opened_at`, `closed_at`, `closed_by`, `opened_by`, `open_reason`, `subject`, `claimed_by`, `claimed_at`, `thanked_at`, `warned_at`, `last_message_at`, `has_moderator_message`, `has_author_message`) SELECT `id`, `guild_id`, `local_id`, `status`, `channel_id`, `close_reason`, `private_reason`, `opened_at`, `closed_at`, `closed_by`, `opened_by`, `open_reason`, `subject`, `claimed_by`, `claimed_at`, `thanked_at`, `warned_at`, `last_message_at`, `has_moderator_message`, `has_author_message` FROM `tickets`;--> statement-breakpoint
DROP TABLE `tickets`;--> statement-breakpoint
ALTER TABLE `__new_tickets` RENAME TO `tickets`;--> statement-breakpoint
PRAGMA foreign_keys=ON;--> statement-breakpoint
CREATE UNIQUE INDEX `tickets_guild_local_idx` ON `tickets` (`guild_id`,`local_id`);--> statement-breakpoint
CREATE INDEX `ticket_comments_ticket_idx` ON `ticket_comments` (`ticket_id`);--> statement-breakpoint
CREATE INDEX `ticket_comments_actor_idx` ON `ticket_comments` (`actor_id`);