CREATE TABLE `lesson_plan_templates` (
	`id` varchar(128) NOT NULL,
	`name` varchar(100) NOT NULL,
	`type` varchar(50) NOT NULL,
	`scope` varchar(50) NOT NULL,
	`blocks` json NOT NULL,
	`creator_id` varchar(128),
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`updated_at` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `lesson_plan_templates_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `lesson_plan_versions` (
	`id` varchar(128) NOT NULL,
	`plan_id` varchar(128) NOT NULL,
	`version_no` int NOT NULL,
	`label` varchar(100),
	`content` json NOT NULL,
	`is_auto` boolean NOT NULL DEFAULT false,
	`creator_id` varchar(128) NOT NULL,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `lesson_plan_versions_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `lesson_plans` (
	`id` varchar(128) NOT NULL,
	`title` varchar(255) NOT NULL,
	`textbook_id` varchar(128),
	`chapter_id` varchar(128),
	`course_plan_item_id` varchar(128),
	`subject_id` varchar(128),
	`grade_id` varchar(128),
	`template_id` varchar(128),
	`template_name` varchar(100),
	`content` json NOT NULL,
	`status` varchar(50) NOT NULL DEFAULT 'draft',
	`creator_id` varchar(128) NOT NULL,
	`last_saved_at` timestamp,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`updated_at` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `lesson_plans_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `lesson_plan_templates` ADD CONSTRAINT `lesson_plan_templates_creator_id_users_id_fk` FOREIGN KEY (`creator_id`) REFERENCES `users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plan_versions` ADD CONSTRAINT `lesson_plan_versions_plan_id_lesson_plans_id_fk` FOREIGN KEY (`plan_id`) REFERENCES `lesson_plans`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plan_versions` ADD CONSTRAINT `lesson_plan_versions_creator_id_users_id_fk` FOREIGN KEY (`creator_id`) REFERENCES `users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plans` ADD CONSTRAINT `lesson_plans_textbook_id_textbooks_id_fk` FOREIGN KEY (`textbook_id`) REFERENCES `textbooks`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plans` ADD CONSTRAINT `lesson_plans_chapter_id_chapters_id_fk` FOREIGN KEY (`chapter_id`) REFERENCES `chapters`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plans` ADD CONSTRAINT `lesson_plans_subject_id_subjects_id_fk` FOREIGN KEY (`subject_id`) REFERENCES `subjects`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plans` ADD CONSTRAINT `lesson_plans_grade_id_grades_id_fk` FOREIGN KEY (`grade_id`) REFERENCES `grades`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `lesson_plans` ADD CONSTRAINT `lesson_plans_creator_id_users_id_fk` FOREIGN KEY (`creator_id`) REFERENCES `users`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `lpt_type_creator_idx` ON `lesson_plan_templates` (`type`,`creator_id`);--> statement-breakpoint
CREATE INDEX `lpv_plan_version_idx` ON `lesson_plan_versions` (`plan_id`,`version_no`);--> statement-breakpoint
CREATE INDEX `lpv_plan_created_idx` ON `lesson_plan_versions` (`plan_id`,`created_at`);--> statement-breakpoint
CREATE INDEX `lp_creator_idx` ON `lesson_plans` (`creator_id`);--> statement-breakpoint
CREATE INDEX `lp_status_idx` ON `lesson_plans` (`status`);--> statement-breakpoint
CREATE INDEX `lp_textbook_chapter_idx` ON `lesson_plans` (`textbook_id`,`chapter_id`);--> statement-breakpoint
CREATE INDEX `lp_subject_grade_idx` ON `lesson_plans` (`subject_id`,`grade_id`);