CREATE TABLE `lesson_subscriptions` (
	`id` integer PRIMARY KEY AUTOINCREMENT NOT NULL,
	`user_id` integer NOT NULL,
	`lesson_id` integer NOT NULL,
	`created_at` integer DEFAULT (unixepoch()) NOT NULL,
	FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON UPDATE no action ON DELETE cascade,
	FOREIGN KEY (`lesson_id`) REFERENCES `lessons`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
ALTER TABLE `card_progress` ADD `user_id` integer REFERENCES users(id);--> statement-breakpoint
ALTER TABLE `lessons` ADD `owner_id` integer REFERENCES users(id);--> statement-breakpoint
ALTER TABLE `lessons` ADD `visibility` text DEFAULT 'private' NOT NULL;--> statement-breakpoint
ALTER TABLE `lessons` ADD `is_curated` integer DEFAULT false NOT NULL;--> statement-breakpoint
ALTER TABLE `lessons` ADD `source_lesson_id` integer REFERENCES lessons(id);--> statement-breakpoint
ALTER TABLE `sessions` ADD `user_id` integer REFERENCES users(id);--> statement-breakpoint
CREATE INDEX `lesson_subscriptions_user_idx` ON `lesson_subscriptions` (`user_id`);--> statement-breakpoint
CREATE INDEX `lesson_subscriptions_lesson_idx` ON `lesson_subscriptions` (`lesson_id`);--> statement-breakpoint
CREATE UNIQUE INDEX `lesson_subscriptions_user_lesson_unique` ON `lesson_subscriptions` (`user_id`,`lesson_id`);--> statement-breakpoint
CREATE INDEX `card_progress_user_idx` ON `card_progress` (`user_id`,`next_due_at`);--> statement-breakpoint
CREATE INDEX `lessons_owner_idx` ON `lessons` (`owner_id`);--> statement-breakpoint
CREATE INDEX `lessons_visibility_idx` ON `lessons` (`visibility`,`is_curated`);--> statement-breakpoint
CREATE INDEX `sessions_user_idx` ON `sessions` (`user_id`,`status`);