CREATE TABLE IF NOT EXISTS rh_courses (
  course_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  instructor_id INT(10) UNSIGNED NOT NULL,
  instructor_context ENUM('personal','business') NOT NULL DEFAULT 'personal',
  business_page_id INT(10) UNSIGNED DEFAULT NULL,
  slug VARCHAR(80) CHARACTER SET ascii NOT NULL,
  title VARCHAR(180) NOT NULL,
  subtitle VARCHAR(240) NOT NULL DEFAULT '',
  description MEDIUMTEXT NOT NULL,
  cover_image_url VARCHAR(2048) NOT NULL DEFAULT '',
  trailer_video_url VARCHAR(2048) NOT NULL DEFAULT '',
  root_ids_json TEXT NOT NULL,
  delivery_type ENUM('prerecorded','live') NOT NULL DEFAULT 'prerecorded',
  meeting_url VARCHAR(2048) NOT NULL DEFAULT '',
  level ENUM('beginner','intermediate','advanced') NOT NULL DEFAULT 'beginner',
  language_code VARCHAR(12) CHARACTER SET ascii NOT NULL DEFAULT 'en',
  price_minor INT UNSIGNED NOT NULL DEFAULT 0,
  currency CHAR(3) CHARACTER SET ascii NOT NULL DEFAULT 'USD',
  for_kids TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  adult_only TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  enrollment_count INT UNSIGNED NOT NULL DEFAULT 0,
  completion_count INT UNSIGNED NOT NULL DEFAULT 0,
  average_rating DECIMAL(2,1) DEFAULT NULL,
  review_count INT UNSIGNED NOT NULL DEFAULT 0,
  total_duration_minutes INT UNSIGNED NOT NULL DEFAULT 0,
  lesson_count INT UNSIGNED NOT NULL DEFAULT 0,
  status ENUM('draft','published','archived') NOT NULL DEFAULT 'draft',
  review_status ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  review_note VARCHAR(2000) NOT NULL DEFAULT '',
  version INT UNSIGNED NOT NULL DEFAULT 1,
  published_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  deleted_at DATETIME DEFAULT NULL,
  PRIMARY KEY (course_id),
  UNIQUE KEY course_slug (slug),
  KEY course_discover (status, review_status, adult_only, for_kids, published_at),
  KEY course_instructor (instructor_id, status, updated_at),
  KEY course_business (business_page_id, status),
  CONSTRAINT rh_courses_instructor_fk FOREIGN KEY (instructor_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_modules (
  module_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  title VARCHAR(180) NOT NULL,
  description TEXT NOT NULL,
  position INT UNSIGNED NOT NULL DEFAULT 0,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (module_id),
  KEY module_course_order (course_id, position, module_id),
  CONSTRAINT rh_course_modules_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_lessons (
  lesson_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  module_id BIGINT UNSIGNED DEFAULT NULL,
  title VARCHAR(180) NOT NULL,
  description TEXT NOT NULL,
  content_type ENUM('video','article','quiz','download') NOT NULL DEFAULT 'video',
  video_url VARCHAR(2048) NOT NULL DEFAULT '',
  video_duration_seconds INT UNSIGNED NOT NULL DEFAULT 0,
  article_html MEDIUMTEXT NOT NULL,
  download_url VARCHAR(2048) NOT NULL DEFAULT '',
  captions_url VARCHAR(2048) NOT NULL DEFAULT '',
  is_preview TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  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,
  deleted_at DATETIME DEFAULT NULL,
  PRIMARY KEY (lesson_id),
  KEY lesson_course_order (course_id, position, lesson_id),
  KEY lesson_module_order (module_id, position, lesson_id),
  CONSTRAINT rh_course_lessons_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_lessons_module_fk FOREIGN KEY (module_id) REFERENCES rh_course_modules(module_id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_media (
  media_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  lesson_id BIGINT UNSIGNED DEFAULT NULL,
  uploader_id INT(10) UNSIGNED NOT NULL,
  media_kind ENUM('cover','trailer','lesson_video','lesson_download','captions') NOT NULL,
  source_url VARCHAR(2048) NOT NULL,
  playback_url VARCHAR(2048) NOT NULL DEFAULT '',
  processing_state ENUM('pending','processing','ready','failed','quarantined') NOT NULL DEFAULT 'pending',
  mime_type VARCHAR(120) CHARACTER SET ascii NOT NULL DEFAULT '',
  size_bytes BIGINT UNSIGNED DEFAULT NULL,
  duration_seconds INT UNSIGNED DEFAULT NULL,
  checksum_sha256 CHAR(64) CHARACTER SET ascii NOT NULL DEFAULT '',
  failure_reason VARCHAR(500) NOT NULL DEFAULT '',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (media_id),
  KEY media_course_state (course_id, processing_state),
  KEY media_lesson (lesson_id, media_kind),
  CONSTRAINT rh_course_media_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_media_lesson_fk FOREIGN KEY (lesson_id) REFERENCES rh_course_lessons(lesson_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_media_uploader_fk FOREIGN KEY (uploader_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_enrollments (
  enrollment_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT(10) UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  status ENUM('active','completed','cancelled','refunded','removed') NOT NULL DEFAULT 'active',
  last_lesson_id BIGINT UNSIGNED DEFAULT NULL,
  progress_percent TINYINT UNSIGNED NOT NULL DEFAULT 0,
  price_paid_minor INT UNSIGNED NOT NULL DEFAULT 0,
  currency CHAR(3) CHARACTER SET ascii NOT NULL DEFAULT 'USD',
  enrolled_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  last_accessed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  completed_at DATETIME DEFAULT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (enrollment_id),
  UNIQUE KEY enrollment_user_course (user_id, course_id),
  KEY enrollment_course_status (course_id, status, enrolled_at),
  KEY enrollment_user_status (user_id, status, last_accessed_at),
  CONSTRAINT rh_course_enrollments_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_enrollments_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_enrollments_lesson_fk FOREIGN KEY (last_lesson_id) REFERENCES rh_course_lessons(lesson_id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_lesson_completions (
  completion_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT(10) UNSIGNED NOT NULL,
  lesson_id BIGINT UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  watch_seconds INT UNSIGNED NOT NULL DEFAULT 0,
  completed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (completion_id),
  UNIQUE KEY completion_user_lesson (user_id, lesson_id),
  KEY completion_user_course (user_id, course_id, completed_at),
  KEY completion_course_lesson (course_id, lesson_id),
  CONSTRAINT rh_lesson_completions_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_lesson_completions_lesson_fk FOREIGN KEY (lesson_id) REFERENCES rh_course_lessons(lesson_id) ON DELETE CASCADE,
  CONSTRAINT rh_lesson_completions_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_activities (
  activity_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  after_lesson_id BIGINT UNSIGNED DEFAULT NULL,
  activity_type ENUM('multiple_choice','long_answer','journal','action_step') NOT NULL,
  prompt TEXT NOT NULL,
  options_json TEXT NOT NULL,
  correct_indices_json 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 (activity_id),
  KEY activity_course_order (course_id, after_lesson_id, position, activity_id),
  CONSTRAINT rh_course_activities_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_activities_lesson_fk FOREIGN KEY (after_lesson_id) REFERENCES rh_course_lessons(lesson_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_activity_responses (
  response_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  user_id INT(10) UNSIGNED NOT NULL,
  activity_id BIGINT UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  selected_indices_json TEXT NOT NULL,
  response_text MEDIUMTEXT NOT NULL,
  is_correct TINYINT(1) UNSIGNED DEFAULT NULL,
  completed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (response_id),
  UNIQUE KEY response_user_activity (user_id, activity_id),
  KEY response_user_course (user_id, course_id, completed_at),
  KEY response_course_activity (course_id, activity_id),
  CONSTRAINT rh_course_responses_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_responses_activity_fk FOREIGN KEY (activity_id) REFERENCES rh_course_activities(activity_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_responses_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_saved_courses (
  user_id INT(10) UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  saved_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, course_id),
  KEY saved_course_time (course_id, saved_at),
  CONSTRAINT rh_saved_courses_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_saved_courses_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_reviews (
  review_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  user_id INT(10) UNSIGNED NOT NULL,
  rating TINYINT UNSIGNED NOT NULL,
  review_text TEXT NOT NULL,
  status ENUM('published','hidden','removed') NOT NULL DEFAULT 'published',
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (review_id),
  UNIQUE KEY review_user_course (user_id, course_id),
  KEY review_course_status (course_id, status, created_at),
  CONSTRAINT rh_course_reviews_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_reviews_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_review_audit (
  review_audit_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  old_status ENUM('pending','approved','rejected') DEFAULT NULL,
  new_status ENUM('pending','approved','rejected') NOT NULL,
  actor_id INT(10) UNSIGNED NOT NULL,
  note VARCHAR(2000) NOT NULL DEFAULT '',
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (review_audit_id),
  KEY review_audit_course_time (course_id, changed_at),
  KEY review_audit_actor_time (actor_id, changed_at),
  CONSTRAINT rh_course_review_audit_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_review_audit_actor_fk FOREIGN KEY (actor_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_teaching_permissions (
  user_id INT(10) UNSIGNED NOT NULL,
  override_state ENUM('automatic','allowed','blocked') NOT NULL DEFAULT 'automatic',
  reason VARCHAR(500) NOT NULL DEFAULT '',
  updated_by INT(10) UNSIGNED DEFAULT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id),
  KEY teaching_override (override_state, updated_at),
  CONSTRAINT rh_teaching_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_teaching_updater_fk FOREIGN KEY (updated_by) REFERENCES users(user_id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_teaching_permission_audit (
  audit_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  target_user_id INT(10) UNSIGNED NOT NULL,
  old_state ENUM('automatic','allowed','blocked') DEFAULT NULL,
  new_state ENUM('automatic','allowed','blocked') NOT NULL,
  actor_id INT(10) UNSIGNED NOT NULL,
  reason VARCHAR(500) NOT NULL DEFAULT '',
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (audit_id),
  KEY teaching_audit_target_time (target_user_id, changed_at),
  KEY teaching_audit_actor_time (actor_id, changed_at),
  CONSTRAINT rh_teaching_audit_target_fk FOREIGN KEY (target_user_id) REFERENCES users(user_id) ON DELETE RESTRICT,
  CONSTRAINT rh_teaching_audit_actor_fk FOREIGN KEY (actor_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_orders (
  order_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  idempotency_key CHAR(36) CHARACTER SET ascii NOT NULL,
  buyer_id INT(10) UNSIGNED NOT NULL,
  instructor_id INT(10) UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  amount_minor INT UNSIGNED NOT NULL,
  platform_fee_minor INT UNSIGNED NOT NULL DEFAULT 0,
  instructor_net_minor INT UNSIGNED NOT NULL DEFAULT 0,
  currency CHAR(3) CHARACTER SET ascii NOT NULL,
  status ENUM('pending','paid','failed','cancelled','refunded','partially_refunded') NOT NULL DEFAULT 'pending',
  payment_method VARCHAR(40) CHARACTER SET ascii NOT NULL DEFAULT '',
  provider_reference VARCHAR(191) CHARACTER SET ascii DEFAULT NULL,
  sngine_transaction_id BIGINT UNSIGNED DEFAULT NULL,
  paid_at DATETIME DEFAULT NULL,
  refunded_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (order_id),
  UNIQUE KEY order_idempotency (idempotency_key),
  UNIQUE KEY order_provider (provider_reference),
  KEY order_buyer_time (buyer_id, created_at),
  KEY order_course_status (course_id, status, created_at),
  KEY order_instructor_status (instructor_id, status, created_at),
  CONSTRAINT rh_course_orders_buyer_fk FOREIGN KEY (buyer_id) REFERENCES users(user_id) ON DELETE RESTRICT,
  CONSTRAINT rh_course_orders_instructor_fk FOREIGN KEY (instructor_id) REFERENCES users(user_id) ON DELETE RESTRICT,
  CONSTRAINT rh_course_orders_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_course_post_links (
  share_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  course_id BIGINT UNSIGNED NOT NULL,
  post_id INT(10) UNSIGNED DEFAULT NULL,
  posted_by INT(10) UNSIGNED NOT NULL,
  consent_token CHAR(36) CHARACTER SET ascii NOT NULL,
  audience ENUM('friends','public') NOT NULL DEFAULT 'friends',
  status ENUM('pending','published','failed') NOT NULL DEFAULT 'pending',
  failure_code VARCHAR(80) CHARACTER SET ascii NOT NULL DEFAULT '',
  consented_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  published_at DATETIME DEFAULT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (share_id),
  UNIQUE KEY course_share_consent (posted_by, consent_token),
  KEY course_share_history (course_id, status, consented_at),
  KEY course_share_post (post_id),
  CONSTRAINT rh_course_post_links_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_course_post_links_actor_fk FOREIGN KEY (posted_by) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohorts (
  cohort_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  owner_id INT(10) UNSIGNED NOT NULL,
  owner_context ENUM('personal','business') NOT NULL DEFAULT 'personal',
  business_page_id INT(10) UNSIGNED DEFAULT NULL,
  slug VARCHAR(80) CHARACTER SET ascii NOT NULL,
  name VARCHAR(180) NOT NULL,
  tagline VARCHAR(240) NOT NULL DEFAULT '',
  description MEDIUMTEXT NOT NULL,
  cover_image_url VARCHAR(2048) NOT NULL DEFAULT '',
  root_ids_json TEXT NOT NULL,
  pricing_type ENUM('free','one_time','monthly') NOT NULL DEFAULT 'free',
  price_minor INT UNSIGNED NOT NULL DEFAULT 0,
  currency CHAR(3) CHARACTER SET ascii NOT NULL DEFAULT 'USD',
  visibility ENUM('public','invite_only','private') NOT NULL DEFAULT 'public',
  join_policy ENUM('open','approval_required') NOT NULL DEFAULT 'open',
  adult_only TINYINT(1) UNSIGNED NOT NULL DEFAULT 0,
  member_count INT UNSIGNED NOT NULL DEFAULT 0,
  post_count INT UNSIGNED NOT NULL DEFAULT 0,
  starts_at DATETIME DEFAULT NULL,
  ends_at DATETIME DEFAULT NULL,
  status ENUM('draft','published','archived') NOT NULL DEFAULT 'draft',
  published_at DATETIME DEFAULT NULL,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  deleted_at DATETIME DEFAULT NULL,
  PRIMARY KEY (cohort_id),
  UNIQUE KEY cohort_slug (slug),
  KEY cohort_discover (status, visibility, adult_only, member_count),
  KEY cohort_owner (owner_id, status, updated_at),
  CONSTRAINT rh_cohorts_owner_fk FOREIGN KEY (owner_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohort_memberships (
  membership_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  cohort_id BIGINT UNSIGNED NOT NULL,
  user_id INT(10) UNSIGNED NOT NULL,
  status ENUM('pending','active','left','removed') NOT NULL DEFAULT 'active',
  membership_role ENUM('owner','moderator','member') NOT NULL DEFAULT 'member',
  joined_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  left_at DATETIME DEFAULT NULL,
  updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (membership_id),
  UNIQUE KEY membership_cohort_user (cohort_id, user_id),
  KEY membership_user_status (user_id, status, joined_at),
  KEY membership_cohort_status (cohort_id, status, membership_role),
  CONSTRAINT rh_cohort_memberships_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_memberships_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohort_courses (
  cohort_id BIGINT UNSIGNED NOT NULL,
  course_id BIGINT UNSIGNED NOT NULL,
  position INT UNSIGNED NOT NULL DEFAULT 0,
  added_by INT(10) UNSIGNED NOT NULL,
  added_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (cohort_id, course_id),
  KEY cohort_course_order (cohort_id, position, course_id),
  KEY course_cohort (course_id, cohort_id),
  CONSTRAINT rh_cohort_courses_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_courses_course_fk FOREIGN KEY (course_id) REFERENCES rh_courses(course_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_courses_actor_fk FOREIGN KEY (added_by) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohort_post_links (
  cohort_id BIGINT UNSIGNED NOT NULL,
  post_id INT(10) UNSIGNED NOT NULL,
  linked_by INT(10) UNSIGNED NOT NULL,
  linked_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (cohort_id, post_id),
  KEY cohort_post_time (cohort_id, linked_at),
  KEY cohort_post_actor (linked_by, linked_at),
  CONSTRAINT rh_cohort_post_links_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_post_links_actor_fk FOREIGN KEY (linked_by) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
