CREATE TABLE `character_relationships` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`character_a_id` integer NOT NULL,
	`character_b_id` integer NOT NULL,
	`relationship_type` text NOT NULL,
	`description` text,
	`strength` integer DEFAULT 50 NOT NULL,
	`is_antagonistic` integer DEFAULT false NOT NULL,
	`created_at` integer,
	`updated_at` integer,
	FOREIGN KEY (`character_a_id`) REFERENCES `characters`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`character_b_id`) REFERENCES `characters`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE UNIQUE INDEX `character_relationships_unique_pair` ON `character_relationships` (`character_a_id`,`character_b_id`);--> statement-breakpoint
CREATE TABLE `characters` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`project_id` integer NOT NULL,
	`name` text NOT NULL,
	`slug` text NOT NULL,
	`role` text DEFAULT 'supporting' NOT NULL,
	`bio` text,
	`description` text,
	`arc` text,
	`arc_type` text,
	`age` integer,
	`gender` text,
	`voice` text,
	`traits` text,
	`motivation` text,
	`conflict` text,
	`secret` text,
	`image_url` text,
	`created_at` integer,
	`updated_at` integer,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `projects` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`name` text NOT NULL,
	`description` text,
	`owner_id` integer NOT NULL,
	`is_public` integer DEFAULT false NOT NULL,
	`theme` text,
	`created_at` integer DEFAULT '"2026-04-24T14:30:03.715Z"' NOT NULL,
	`updated_at` integer DEFAULT '"2026-04-24T14:30:03.715Z"' NOT NULL,
	FOREIGN KEY (`owner_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `revision_changes` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`revision_id` integer NOT NULL,
	`change_type` text NOT NULL,
	`element_type` text,
	`old_content` text,
	`new_content` text,
	`scene_number` integer,
	`line_number` integer,
	`page_number` integer,
	`created_at` integer NOT NULL,
	FOREIGN KEY (`revision_id`) REFERENCES `revisions`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `revision_changes_revision_idx` ON `revision_changes` (`revision_id`);--> statement-breakpoint
CREATE INDEX `revision_changes_type_idx` ON `revision_changes` (`change_type`);--> statement-breakpoint
CREATE TABLE `revisions` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`script_id` integer NOT NULL,
	`version_number` integer NOT NULL,
	`branch_name` text DEFAULT 'main' NOT NULL,
	`parent_revision_id` integer,
	`title` text NOT NULL,
	`summary` text,
	`content` text NOT NULL,
	`author_id` integer NOT NULL,
	`status` text DEFAULT 'draft' NOT NULL,
	`reviewed_by_id` integer,
	`reviewed_at` integer,
	`created_at` integer NOT NULL,
	`updated_at` integer NOT NULL,
	FOREIGN KEY (`script_id`) REFERENCES `scripts`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`author_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`reviewed_by_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE INDEX `revisions_script_version_idx` ON `revisions` (`script_id`,`version_number`);--> statement-breakpoint
CREATE INDEX `revisions_script_branch_idx` ON `revisions` (`script_id`,`branch_name`);--> statement-breakpoint
CREATE INDEX `revisions_author_idx` ON `revisions` (`author_id`);--> statement-breakpoint
CREATE TABLE `scene_characters` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`scene_id` integer NOT NULL,
	`character_id` integer NOT NULL,
	`screen_time` integer,
	`dialogue_lines` integer DEFAULT 0,
	FOREIGN KEY (`scene_id`) REFERENCES `scenes`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`character_id`) REFERENCES `characters`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `scenes` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`project_id` integer NOT NULL,
	`title` text NOT NULL,
	`content` text DEFAULT '' NOT NULL,
	`order` integer DEFAULT 0 NOT NULL,
	`created_at` integer,
	`updated_at` integer,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `scripts` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`project_id` integer NOT NULL,
	`title` text NOT NULL,
	`slug` text NOT NULL,
	`genre` text,
	`logline` text,
	`status` text DEFAULT 'draft' NOT NULL,
	`current_version` integer DEFAULT 1 NOT NULL,
	`created_at` integer DEFAULT '"2026-04-24T14:30:03.720Z"' NOT NULL,
	`updated_at` integer DEFAULT '"2026-04-24T14:30:03.720Z"' NOT NULL,
	FOREIGN KEY (`project_id`) REFERENCES `projects`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `users` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`email` text NOT NULL,
	`username` text NOT NULL,
	`full_name` text,
	`avatar_url` text,
	`role` text DEFAULT 'viewer' NOT NULL,
	`is_active` integer DEFAULT true NOT NULL,
	`last_login_at` integer,
	`created_at` integer DEFAULT '"2026-04-24T14:30:03.711Z"' NOT NULL,
	`updated_at` integer DEFAULT '"2026-04-24T14:30:03.711Z"' NOT NULL
);
--> statement-breakpoint
CREATE UNIQUE INDEX `users_email_unique` ON `users` (`email`);--> statement-breakpoint
CREATE UNIQUE INDEX `users_username_unique` ON `users` (`username`);