CREATE TABLE IF NOT EXISTS rh_cohort_sngine_links (
  cohort_id BIGINT UNSIGNED NOT NULL,
  sngine_group_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),
  UNIQUE KEY cohort_sngine_group (sngine_group_id),
  CONSTRAINT rh_cohort_sngine_links_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_sngine_links_actor_fk FOREIGN KEY (linked_by) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohort_membership_audit (
  audit_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  cohort_id BIGINT UNSIGNED NOT NULL,
  target_user_id INT(10) UNSIGNED NOT NULL,
  actor_id INT(10) UNSIGNED NOT NULL,
  old_status ENUM('pending','active','left','removed') DEFAULT NULL,
  new_status ENUM('pending','active','left','removed') NOT NULL,
  reason VARCHAR(500) NOT NULL DEFAULT '',
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (audit_id),
  KEY cohort_membership_audit_time (cohort_id, changed_at),
  KEY cohort_membership_audit_target (target_user_id, changed_at),
  KEY cohort_membership_audit_actor (actor_id, changed_at),
  CONSTRAINT rh_cohort_membership_audit_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_membership_audit_target_fk FOREIGN KEY (target_user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_membership_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_saved_cohorts (
  user_id INT(10) UNSIGNED NOT NULL,
  cohort_id BIGINT UNSIGNED NOT NULL,
  saved_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (user_id, cohort_id),
  KEY saved_cohort_time (cohort_id, saved_at),
  CONSTRAINT rh_saved_cohorts_user_fk FOREIGN KEY (user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_saved_cohorts_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS rh_cohort_invitation_audit (
  invitation_audit_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  cohort_id BIGINT UNSIGNED NOT NULL,
  target_user_id INT(10) UNSIGNED NOT NULL,
  actor_id INT(10) UNSIGNED NOT NULL,
  action ENUM('invited','revoked','accepted') NOT NULL,
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (invitation_audit_id),
  KEY cohort_invitation_audit_time (cohort_id, changed_at),
  KEY cohort_invitation_audit_target (target_user_id, changed_at),
  CONSTRAINT rh_cohort_invitation_audit_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_invitation_audit_target_fk FOREIGN KEY (target_user_id) REFERENCES users(user_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_invitation_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_cohort_settings_audit (
  settings_audit_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  cohort_id BIGINT UNSIGNED NOT NULL,
  actor_id INT(10) UNSIGNED NOT NULL,
  before_json MEDIUMTEXT NOT NULL,
  after_json MEDIUMTEXT NOT NULL,
  reason VARCHAR(500) NOT NULL DEFAULT '',
  changed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (settings_audit_id),
  KEY cohort_settings_audit_time (cohort_id, changed_at),
  KEY cohort_settings_audit_actor (actor_id, changed_at),
  CONSTRAINT rh_cohort_settings_audit_cohort_fk FOREIGN KEY (cohort_id) REFERENCES rh_cohorts(cohort_id) ON DELETE CASCADE,
  CONSTRAINT rh_cohort_settings_audit_actor_fk FOREIGN KEY (actor_id) REFERENCES users(user_id) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO rh_modules (module_slug, display_name, state, utility_module, sort_order)
VALUES ('cohorts', 'Summits', 'disabled', 0, 125)
ON DUPLICATE KEY UPDATE display_name = VALUES(display_name), utility_module = VALUES(utility_module), sort_order = VALUES(sort_order);
