-- Phase 3 migration. Run ONLY if you imported schema.sql before this change.
-- 1) A bookmark must not count as "completed", so completed_at becomes nullable.
ALTER TABLE lesson_progress MODIFY completed_at DATETIME NULL DEFAULT NULL;

-- 2) Per-course "continue learning" pointer.
CREATE TABLE IF NOT EXISTS course_resume (
  user_id    INT UNSIGNED NOT NULL,
  course_id  INT UNSIGNED NOT NULL,
  lesson_id  INT UNSIGNED NOT NULL,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, course_id),
  FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
  FOREIGN KEY (course_id) REFERENCES courses(id) ON DELETE CASCADE,
  FOREIGN KEY (lesson_id) REFERENCES lessons(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
