CREATE TABLE `courses` (
	`id` text PRIMARY KEY NOT NULL,
	`title` text NOT NULL,
	`subject` text NOT NULL,
	`status` text DEFAULT 'processing' NOT NULL,
	`stage` text,
	`cost_ai` real DEFAULT 0,
	`cost_audio` real DEFAULT 0,
	`audit_report` text,
	`audit_score` integer,
	`organisation` text,
	`created_at` text DEFAULT (datetime('now')) NOT NULL
);
--> statement-breakpoint
CREATE TABLE `uploads` (
	`id` text PRIMARY KEY NOT NULL,
	`course_id` text NOT NULL,
	`filename` text NOT NULL,
	`type` text NOT NULL,
	`stored_path` text NOT NULL,
	`extracted_text` text,
	`created_at` text DEFAULT (datetime('now')) NOT NULL,
	FOREIGN KEY (`course_id`) REFERENCES `courses`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `topics` (
	`id` text PRIMARY KEY NOT NULL,
	`course_id` text NOT NULL,
	`title` text NOT NULL,
	`description` text NOT NULL,
	`order` integer NOT NULL,
	`prerequisite_topic_ids` text DEFAULT '[]' NOT NULL,
	`difficulty` integer DEFAULT 1 NOT NULL,
	`relevant_files` text,
	`status` text DEFAULT 'pending' NOT NULL,
	FOREIGN KEY (`course_id`) REFERENCES `courses`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `lessons` (
	`id` text PRIMARY KEY NOT NULL,
	`topic_id` text NOT NULL,
	`content` text NOT NULL,
	`tts_provider` text,
	`cost_ai` real DEFAULT 0,
	`cost_audio` real DEFAULT 0,
	`cost_branch_ai` real DEFAULT 0,
	`cost_branch_audio` real DEFAULT 0,
	`cost_total` real DEFAULT 0,
	`branch_status` text DEFAULT 'pending' NOT NULL,
	`created_at` text DEFAULT (datetime('now')) NOT NULL,
	FOREIGN KEY (`topic_id`) REFERENCES `topics`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `quiz_questions` (
	`id` text PRIMARY KEY NOT NULL,
	`topic_id` text NOT NULL,
	`question` text NOT NULL,
	`type` text NOT NULL,
	`options` text,
	`answer` text NOT NULL,
	`explanation` text NOT NULL,
	FOREIGN KEY (`topic_id`) REFERENCES `topics`(`id`) ON UPDATE no action ON DELETE no action
);
--> statement-breakpoint
CREATE TABLE `user_progress` (
	`id` text PRIMARY KEY NOT NULL,
	`course_id` text NOT NULL,
	`topic_id` text NOT NULL,
	`lesson_complete` integer DEFAULT false NOT NULL,
	`quiz_score` integer,
	`took_branches` integer DEFAULT false NOT NULL,
	`branch_count` integer DEFAULT 0 NOT NULL,
	`updated_at` text DEFAULT (datetime('now')) NOT NULL,
	FOREIGN KEY (`course_id`) REFERENCES `courses`(`id`) ON UPDATE no action ON DELETE no action,
	FOREIGN KEY (`topic_id`) REFERENCES `topics`(`id`) ON UPDATE no action ON DELETE no action
);
