CREATE TABLE `error_book_items` (
	`id` varchar(128) NOT NULL,
	`student_id` varchar(128) NOT NULL,
	`question_id` varchar(128) NOT NULL,
	`source_type` enum('exam','homework','manual') NOT NULL DEFAULT 'manual',
	`source_id` varchar(128),
	`student_answer` json,
	`correct_answer` json,
	`subject_id` varchar(128),
	`knowledge_point_ids` json,
	`error_status` enum('new','learning','mastered','archived') NOT NULL DEFAULT 'new',
	`mastery_level` int NOT NULL DEFAULT 0,
	`next_review_at` timestamp,
	`review_interval` int NOT NULL DEFAULT 1,
	`review_count` int NOT NULL DEFAULT 0,
	`correct_streak` int NOT NULL DEFAULT 0,
	`note` text,
	`error_tags` json,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	`updated_at` timestamp NOT NULL DEFAULT (now()) ON UPDATE CURRENT_TIMESTAMP,
	CONSTRAINT `error_book_items_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
CREATE TABLE `error_book_reviews` (
	`id` varchar(128) NOT NULL,
	`item_id` varchar(128) NOT NULL,
	`student_id` varchar(128) NOT NULL,
	`review_result` enum('again','hard','good','easy') NOT NULL,
	`reviewed_at` timestamp NOT NULL DEFAULT (now()),
	`new_interval` int,
	`new_mastery_level` int,
	`created_at` timestamp NOT NULL DEFAULT (now()),
	CONSTRAINT `error_book_reviews_id` PRIMARY KEY(`id`)
);
--> statement-breakpoint
ALTER TABLE `error_book_items` ADD CONSTRAINT `error_book_items_student_id_users_id_fk` FOREIGN KEY (`student_id`) REFERENCES `users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `error_book_items` ADD CONSTRAINT `error_book_items_question_id_questions_id_fk` FOREIGN KEY (`question_id`) REFERENCES `questions`(`id`) ON DELETE no action ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `error_book_reviews` ADD CONSTRAINT `error_book_reviews_item_id_error_book_items_id_fk` FOREIGN KEY (`item_id`) REFERENCES `error_book_items`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
ALTER TABLE `error_book_reviews` ADD CONSTRAINT `error_book_reviews_student_id_users_id_fk` FOREIGN KEY (`student_id`) REFERENCES `users`(`id`) ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
CREATE INDEX `eb_item_student_idx` ON `error_book_items` (`student_id`);--> statement-breakpoint
CREATE INDEX `eb_item_student_status_idx` ON `error_book_items` (`student_id`,`error_status`);--> statement-breakpoint
CREATE INDEX `eb_item_student_review_idx` ON `error_book_items` (`student_id`,`next_review_at`);--> statement-breakpoint
CREATE INDEX `eb_item_question_idx` ON `error_book_items` (`question_id`);--> statement-breakpoint
CREATE INDEX `eb_item_subject_idx` ON `error_book_items` (`subject_id`);--> statement-breakpoint
CREATE INDEX `eb_item_source_idx` ON `error_book_items` (`source_type`,`source_id`);--> statement-breakpoint
CREATE INDEX `eb_review_item_idx` ON `error_book_reviews` (`item_id`);--> statement-breakpoint
CREATE INDEX `eb_review_student_idx` ON `error_book_reviews` (`student_id`);--> statement-breakpoint
CREATE INDEX `eb_review_student_reviewed_idx` ON `error_book_reviews` (`student_id`,`reviewed_at`);