CREATE TABLE IF NOT EXISTS rh_course_flashcards (
  card_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  checkpoint_lesson_id BIGINT UNSIGNED NOT NULL,
  term VARCHAR(500) NOT NULL,
  definition TEXT NOT NULL,
  position INT UNSIGNED NOT NULL DEFAULT 0,
  version INT UNSIGNED NOT NULL DEFAULT 1,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (card_id),
  KEY flashcard_course_order (course_id, position, card_id),
  KEY flashcard_checkpoint_order (checkpoint_lesson_id, position, card_id),
  CONSTRAINT rh_course_flashcards_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_flashcards_lesson_fk FOREIGN KEY (checkpoint_lesson_id) REFERENCES rh_course_lessons(lesson_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
