<?php
/**
 * TutorLMS Integration
 *
 * Integrates PressPrimer Quiz with TutorLMS for seamless quiz
 * experiences within courses.
 *
 * @package PressPrimer_Quiz
 * @subpackage Integrations
 * @since 1.0.0
 */

// Prevent direct access.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * TutorLMS Integration Class
 *
 * Handles all TutorLMS integration functionality including:
 * - Meta boxes for lesson post type
 * - Quiz display in lesson content
 * - Completion tracking on quiz pass
 * - Access control respecting TutorLMS enrollment
 *
 * @since 1.0.0
 */
class PressPrimer_Quiz_TutorLMS {

	/**
	 * Meta key for storing the PPQ quiz ID on lessons
	 *
	 * @since 1.0.0
	 * @var string
	 */
	const META_KEY_QUIZ_ID = '_ppq_tutorlms_quiz_id';

	/**
	 * Meta key for storing require pass setting
	 *
	 * @since 1.0.0
	 * @var string
	 */
	const META_KEY_REQUIRE_PASS = '_ppq_tutorlms_require_pass';

	/**
	 * Meta key for storing topic-level PPQ quiz ID
	 *
	 * @since 1.0.0
	 * @var string
	 */
	const META_KEY_TOPIC_QUIZ_ID = '_ppq_tutorlms_topic_quiz_id';

	/**
	 * Supported post types for integration
	 *
	 * @since 1.0.0
	 * @var array
	 */
	private $supported_post_types = [ 'lesson' ];

	/**
	 * Whether the lesson quiz has already been rendered via the_content filter.
	 *
	 * Prevents double-rendering since Tutor LMS calls the_content() via
	 * tutor_load_template() outside the standard WordPress loop.
	 *
	 * @since 2.1.0
	 * @var bool
	 */
	private $lesson_quiz_rendered = false;

	/**
	 * Initialize the integration
	 *
	 * @since 1.0.0
	 */
	public function init() {
		// Only initialize if TutorLMS is active.
		if ( ! defined( 'TUTOR_VERSION' ) ) {
			return;
		}

		// Admin hooks.
		add_action( 'add_meta_boxes', [ $this, 'register_meta_boxes' ] );
		add_action( 'save_post', [ $this, 'save_meta_box' ], 10, 2 );

		// AJAX handler for classic editor quiz search.
		add_action( 'wp_ajax_pressprimer_quiz_search_quizzes_tutorlms', [ $this, 'ajax_search_quizzes' ] );

		// Gutenberg support.
		add_action( 'init', [ $this, 'register_meta_fields' ] );
		add_action( 'enqueue_block_editor_assets', [ $this, 'enqueue_block_editor_assets' ] );

		// Course builder support.
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_course_builder_assets' ] );

		// Map TutorLMS Instructor role to PPQ teacher capabilities.
		$this->map_instructor_capabilities();
		add_filter( 'pressprimer_quiz_user_has_teacher_capability', [ $this, 'check_instructor_capability' ], 10, 2 );

		// Frontend hooks — append quiz to lesson content via the_content filter.
		// Tutor LMS calls the_content() inside tutor_load_template() which does
		// NOT use the WordPress loop, so in_the_loop()/is_main_query() are false.
		add_filter( 'the_content', [ $this, 'append_quiz_to_lesson_content' ], 20 );

		// Ensure the Overview tab is shown for empty lessons that have a quiz mapped.
		// Without this, Tutor LMS hides the tab (and never calls the_content) when
		// the lesson post_content is empty and the user is not an admin.
		add_filter( 'tutor_has_lesson_content', [ $this, 'force_lesson_content_for_quiz' ], 10, 2 );
		add_action( 'tutor_course/single/enrolled/after/lesson_list', [ $this, 'display_topic_quizzes' ] );
		add_filter( 'tutor_course/single/enrolled/topic_contents', [ $this, 'inject_topic_quiz_content' ], 10, 2 );

		// Hide Tutor's mark complete button when quiz is attached.
		add_filter( 'tutor_lesson/single/complete_form', [ $this, 'maybe_hide_complete_button' ] );

		// Completion tracking.
		add_action( 'pressprimer_quiz_quiz_passed', [ $this, 'handle_quiz_passed' ], 10, 2 );

		// REST API endpoints.
		add_action( 'rest_api_init', [ $this, 'register_rest_routes' ] );
	}

	/**
	 * Register meta boxes for TutorLMS post types
	 *
	 * Only registers for Classic Editor - Gutenberg uses the sidebar panel.
	 *
	 * @since 1.0.0
	 */
	public function register_meta_boxes() {
		// Don't register metabox if using block editor.
		$screen = get_current_screen();
		if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) {
			return;
		}

		foreach ( $this->supported_post_types as $post_type ) {
			add_meta_box(
				'ppq_tutorlms_quiz',
				__( 'PressPrimer Quiz', 'pressprimer-quiz' ),
				[ $this, 'render_meta_box' ],
				$post_type,
				'side',
				'high'
			);
		}
	}

	/**
	 * Render the meta box
	 *
	 * @since 1.0.0
	 *
	 * @param WP_Post $post Current post object.
	 */
	public function render_meta_box( $post ) {
		wp_nonce_field( 'pressprimer_quiz_tutorlms_meta_box', 'pressprimer_quiz_tutorlms_nonce' );

		$quiz_id      = get_post_meta( $post->ID, self::META_KEY_QUIZ_ID, true );
		$require_pass = get_post_meta( $post->ID, self::META_KEY_REQUIRE_PASS, true );

		// Get quiz display label if one is selected.
		$quiz_display = '';
		if ( $quiz_id ) {
			$quiz         = PressPrimer_Quiz_Quiz::get( $quiz_id );
			$quiz_display = $quiz ? sprintf( '%d - %s', $quiz->id, $quiz->title ) : '';
		}
		?>
		<div class="ppq-tutorlms-meta-box">
			<p>
				<label for="ppq_quiz_search">
					<?php esc_html_e( 'Select Quiz:', 'pressprimer-quiz' ); ?>
				</label>
			</p>
			<div class="ppq-quiz-selector">
				<input
					type="text"
					id="ppq_quiz_search"
					class="ppq-quiz-search widefat"
					placeholder="<?php esc_attr_e( 'Click to browse or type to search...', 'pressprimer-quiz' ); ?>"
					value="<?php echo esc_attr( $quiz_display ); ?>"
					autocomplete="off"
					<?php echo esc_attr( $quiz_id ? 'readonly' : '' ); ?>
				/>
				<input
					type="hidden"
					id="ppq_quiz_id"
					name="ppq_quiz_id"
					value="<?php echo esc_attr( $quiz_id ); ?>"
				/>
				<div id="ppq_quiz_results" class="ppq-quiz-results" style="display: none;"></div>
				<?php if ( $quiz_id ) : ?>
					<button type="button" class="ppq-remove-quiz button-link" aria-label="<?php esc_attr_e( 'Remove quiz', 'pressprimer-quiz' ); ?>" title="<?php esc_attr_e( 'Remove quiz', 'pressprimer-quiz' ); ?>">
						<span class="dashicons dashicons-no-alt"></span>
					</button>
				<?php endif; ?>
			</div>

			<p style="margin-top: 12px;">
				<label>
					<input
						type="checkbox"
						name="ppq_require_pass"
						value="1"
						<?php checked( $require_pass, '1' ); ?>
					/>
					<?php esc_html_e( 'Require passing score to complete lesson', 'pressprimer-quiz' ); ?>
				</label>
			</p>
			<p class="description">
				<?php esc_html_e( 'When enabled, students must pass this quiz to mark the lesson complete.', 'pressprimer-quiz' ); ?>
			</p>
		</div>
		<?php
		$this->enqueue_meta_box_assets();
	}

	/**
	 * Enqueue meta box assets (styles and scripts)
	 *
	 * @since 1.0.0
	 */
	private function enqueue_meta_box_assets() {
		// Ensure admin scripts are enqueued (they may not be on LMS post type edit screens).
		wp_enqueue_style(
			'ppq-admin',
			PRESSPRIMER_QUIZ_PLUGIN_URL . 'assets/css/admin.css',
			[],
			PRESSPRIMER_QUIZ_VERSION
		);

		wp_enqueue_script(
			'ppq-admin',
			PRESSPRIMER_QUIZ_PLUGIN_URL . 'assets/js/admin.js',
			[ 'jquery' ],
			PRESSPRIMER_QUIZ_VERSION,
			true
		);

		// Localize script with nonces and translatable strings.
		wp_localize_script(
			'ppq-admin',
			'pressPrimerQuizTutorLMSMetaBox',
			[
				'nonce'   => wp_create_nonce( 'pressprimer_quiz_tutorlms_search' ),
				'strings' => [
					'noQuizzesFound' => __( 'No quizzes found', 'pressprimer-quiz' ),
					'removeQuiz'     => __( 'Remove quiz', 'pressprimer-quiz' ),
				],
			]
		);

		// Inline CSS for meta box styling.
		$inline_css = '
			.ppq-tutorlms-meta-box .ppq-quiz-selector {
				position: relative;
				display: flex;
				align-items: center;
				gap: 4px;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-search {
				flex: 1;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-search[readonly] {
				background: #f0f6fc;
				cursor: default;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-results {
				position: absolute;
				top: 100%;
				left: 0;
				right: 30px;
				background: #fff;
				border: 1px solid #ddd;
				border-top: none;
				max-height: 250px;
				overflow-y: auto;
				z-index: 1000;
				box-shadow: 0 2px 4px rgba(0,0,0,0.1);
			}
			.ppq-tutorlms-meta-box .ppq-quiz-result-item {
				padding: 8px 12px;
				cursor: pointer;
				border-bottom: 1px solid #f0f0f0;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-result-item:hover {
				background: #f0f0f0;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-result-item:last-child {
				border-bottom: none;
			}
			.ppq-tutorlms-meta-box .ppq-remove-quiz {
				color: #d63638;
				text-decoration: none;
				padding: 4px;
				border: none;
				background: none;
				cursor: pointer;
				display: flex;
				align-items: center;
			}
			.ppq-tutorlms-meta-box .ppq-remove-quiz:hover {
				color: #b32d2e;
			}
			.ppq-tutorlms-meta-box .ppq-no-results {
				padding: 12px;
				color: #666;
				font-style: italic;
			}
			.ppq-tutorlms-meta-box .ppq-quiz-result-item .ppq-quiz-id {
				color: #666;
				font-weight: 600;
				margin-right: 4px;
			}
		';
		wp_add_inline_style( 'ppq-admin', $inline_css );

		// Inline JavaScript for meta box functionality.
		$inline_script = 'jQuery(document).ready(function($) {' .
			'var config = window.pressPrimerQuizTutorLMSMetaBox || {};' .
			'var searchTimeout;' .
			'var $search = $("#ppq_quiz_search");' .
			'var $results = $("#ppq_quiz_results");' .
			'var $quizId = $("#ppq_quiz_id");' .
			'var $removeBtn = $(".ppq-remove-quiz");' .
			'function formatQuiz(quiz) {' .
				'return \'<div class="ppq-quiz-result-item" data-id="\' + quiz.id + \'" data-title="\' + $("<div/>").text(quiz.title).html() + \'">\' +' .
					'\'<span class="ppq-quiz-id">\' + quiz.id + \'</span> - \' + $("<div/>").text(quiz.title).html() +' .
					'\'</div>\';' .
			'}' .
			'$search.on("focus", function() {' .
				'if ($quizId.val()) { return; }' .
				'$.ajax({' .
					'url: ajaxurl,' .
					'type: "POST",' .
					'data: {' .
						'action: "pressprimer_quiz_search_quizzes_tutorlms",' .
						'nonce: config.nonce,' .
						'recent: 1' .
					'},' .
					'success: function(response) {' .
						'var html = "";' .
						'if (response.success && response.data.quizzes.length > 0) {' .
							'response.data.quizzes.forEach(function(quiz) {' .
								'html += formatQuiz(quiz);' .
							'});' .
						'} else {' .
							'html = \'<div class="ppq-no-results">\' + strings.noQuizzesFound + \'</div>\';' .
						'}' .
						'$results.html(html).show();' .
					'}' .
				'});' .
			'});' .
			'$search.on("input", function() {' .
				'var query = $(this).val();' .
				'clearTimeout(searchTimeout);' .
				'if (query.length < 2) {' .
					'$search.trigger("focus");' .
					'return;' .
				'}' .
				'searchTimeout = setTimeout(function() {' .
					'$.ajax({' .
						'url: ajaxurl,' .
						'type: "POST",' .
						'data: {' .
							'action: "pressprimer_quiz_search_quizzes_tutorlms",' .
							'nonce: config.nonce,' .
							'search: query' .
						'},' .
						'success: function(response) {' .
							'if (response.success && response.data.quizzes.length > 0) {' .
								'var html = "";' .
								'response.data.quizzes.forEach(function(quiz) {' .
									'html += formatQuiz(quiz);' .
								'});' .
								'$results.html(html).show();' .
							'} else {' .
								'$results.html(\'<div class="ppq-no-results">\' + config.strings.noQuizzesFound + \'</div>\').show();' .
							'}' .
						'}' .
					'});' .
				'}, 300);' .
			'});' .
			'$results.on("click", ".ppq-quiz-result-item", function() {' .
				'var $item = $(this);' .
				'var id = $item.data("id");' .
				'var title = $item.data("title");' .
				'var display = id + " - " + title;' .
				'$quizId.val(id);' .
				'$search.val(display).attr("readonly", true);' .
				'$results.hide();' .
				'if (!$removeBtn.length) {' .
					'$search.after(\'<button type="button" class="ppq-remove-quiz button-link" aria-label="\' + config.strings.removeQuiz + \'" title="\' + config.strings.removeQuiz + \'"><span class="dashicons dashicons-no-alt"></span></button>\');' .
					'$removeBtn = $(".ppq-remove-quiz");' .
				'}' .
			'});' .
			'$(document).on("click", ".ppq-remove-quiz", function() {' .
				'$quizId.val("");' .
				'$search.val("").removeAttr("readonly");' .
				'$(this).remove();' .
				'$removeBtn = $();' .
			'});' .
			'$(document).on("click", function(e) {' .
				'if (!$(e.target).closest(".ppq-quiz-selector").length) {' .
					'$results.hide();' .
				'}' .
			'});' .
		'});';
		wp_add_inline_script( 'ppq-admin', $inline_script );
	}

	/**
	 * Save meta box data
	 *
	 * @since 1.0.0
	 *
	 * @param int     $post_id Post ID.
	 * @param WP_Post $post    Post object.
	 */
	public function save_meta_box( $post_id, $post ) {
		// Verify nonce.
		if ( ! isset( $_POST['pressprimer_quiz_tutorlms_nonce'] ) ||
			! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['pressprimer_quiz_tutorlms_nonce'] ) ), 'pressprimer_quiz_tutorlms_meta_box' ) ) {
			return;
		}

		// Check autosave.
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		// Check post type.
		if ( ! in_array( $post->post_type, $this->supported_post_types, true ) ) {
			return;
		}

		// Check permissions.
		if ( ! PressPrimer_Quiz_Helpers::current_user_can_edit_post( $post_id ) ) {
			return;
		}

		// Save quiz ID.
		$quiz_id = isset( $_POST['ppq_quiz_id'] ) ? absint( wp_unslash( $_POST['ppq_quiz_id'] ) ) : 0;
		if ( $quiz_id ) {
			update_post_meta( $post_id, self::META_KEY_QUIZ_ID, $quiz_id );
		} else {
			delete_post_meta( $post_id, self::META_KEY_QUIZ_ID );
		}

		// Save require pass setting.
		$require_pass = isset( $_POST['ppq_require_pass'] ) ? '1' : '';
		update_post_meta( $post_id, self::META_KEY_REQUIRE_PASS, $require_pass );
	}

	/**
	 * Register meta fields for Gutenberg
	 *
	 * @since 1.0.0
	 */
	public function register_meta_fields() {
		foreach ( $this->supported_post_types as $post_type ) {
			register_post_meta(
				$post_type,
				self::META_KEY_QUIZ_ID,
				[
					'show_in_rest'  => true,
					'single'        => true,
					'type'          => 'integer',
					'auth_callback' => function () {
						return current_user_can( 'edit_posts' );
					},
				]
			);

			register_post_meta(
				$post_type,
				self::META_KEY_REQUIRE_PASS,
				[
					'show_in_rest'  => true,
					'single'        => true,
					'type'          => 'string',
					'auth_callback' => function () {
						return current_user_can( 'edit_posts' );
					},
				]
			);
		}
	}

	/**
	 * Enqueue block editor assets
	 *
	 * @since 1.0.0
	 */
	public function enqueue_block_editor_assets() {
		$screen = get_current_screen();

		if ( ! $screen || ! in_array( $screen->post_type, $this->supported_post_types, true ) ) {
			return;
		}

		wp_enqueue_script(
			'ppq-tutorlms-editor',
			PRESSPRIMER_QUIZ_PLUGIN_URL . 'assets/js/tutorlms-editor.js',
			[ 'wp-plugins', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-components', 'wp-data', 'wp-api-fetch', 'wp-i18n' ],
			PRESSPRIMER_QUIZ_VERSION,
			true
		);

		wp_localize_script(
			'ppq-tutorlms-editor',
			'pressprimerQuizTutorLMS',
			[
				'metaKeyQuizId'      => self::META_KEY_QUIZ_ID,
				'metaKeyRequirePass' => self::META_KEY_REQUIRE_PASS,
				'postType'           => $screen->post_type,
				'restNonce'          => wp_create_nonce( 'wp_rest' ),
				'strings'            => [
					'panelTitle'        => __( 'PressPrimer Quiz', 'pressprimer-quiz' ),
					'selectQuiz'        => __( 'Select Quiz', 'pressprimer-quiz' ),
					'searchPlaceholder' => __( 'Click to browse or type to search...', 'pressprimer-quiz' ),
					'noQuiz'            => __( 'No quiz selected', 'pressprimer-quiz' ),
					'requirePassLabel'  => __( 'Require passing score to complete lesson', 'pressprimer-quiz' ),
					'requirePassHelp'   => __( 'When enabled, students must pass this quiz to mark the lesson complete.', 'pressprimer-quiz' ),
				],
			]
		);
	}

	/**
	 * Enqueue course builder assets
	 *
	 * @since 1.0.0
	 *
	 * @param string $hook Current admin page hook.
	 */
	public function enqueue_course_builder_assets( $hook ) {
		// Only load on TutorLMS course builder page.
		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		if ( 'tutor_page_create-course' !== $hook && ! isset( $_GET['page'] ) ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : '';
		if ( 'create-course' !== $page ) {
			return;
		}

		// phpcs:ignore WordPress.Security.NonceVerification.Recommended
		$course_id = isset( $_GET['course_id'] ) ? absint( wp_unslash( $_GET['course_id'] ) ) : 0;

		// Get existing lesson quiz associations for this course.
		$lesson_quizzes = $this->get_lesson_quizzes_for_course( $course_id );

		wp_enqueue_script(
			'ppq-tutorlms-course-builder',
			PRESSPRIMER_QUIZ_PLUGIN_URL . 'assets/js/tutorlms-course-builder.js',
			[ 'wp-element' ],
			PRESSPRIMER_QUIZ_VERSION,
			true
		);

		wp_localize_script(
			'ppq-tutorlms-course-builder',
			'pressprimerQuizTutorCourseBuilder',
			[
				'courseId'      => $course_id,
				'restUrl'       => rest_url(),
				'restNonce'     => wp_create_nonce( 'wp_rest' ),
				'adminUrl'      => admin_url(),
				'lessonQuizzes' => $lesson_quizzes,
				'strings'       => [
					'searchPlaceholder' => __( 'Search quizzes...', 'pressprimer-quiz' ),
					'noQuizzes'         => __( 'No quizzes found', 'pressprimer-quiz' ),
					'error'             => __( 'Error loading quizzes', 'pressprimer-quiz' ),
				],
			]
		);
	}

	/**
	 * Get lesson quiz associations for a course
	 *
	 * @since 1.0.0
	 *
	 * @param int $course_id Course ID.
	 * @return array Associative array of lesson_id => quiz data.
	 */
	private function get_lesson_quizzes_for_course( $course_id ) {
		if ( ! $course_id ) {
			return [];
		}

		$lesson_quizzes = [];

		// Get all lessons for this course (lessons are children of topics, which are children of courses).
		$topics = get_posts(
			[
				'post_type'      => 'topics',
				'post_parent'    => $course_id,
				'posts_per_page' => -1,
				'post_status'    => 'any',
			]
		);

		foreach ( $topics as $topic ) {
			$lessons = get_posts(
				[
					'post_type'      => 'lesson',
					'post_parent'    => $topic->ID,
					'posts_per_page' => -1,
					'post_status'    => 'any',
				]
			);

			foreach ( $lessons as $lesson ) {
				$quiz_id = get_post_meta( $lesson->ID, self::META_KEY_QUIZ_ID, true );
				if ( $quiz_id ) {
					$quiz = PressPrimer_Quiz_Quiz::get( $quiz_id );
					if ( $quiz ) {
						$lesson_quizzes[ $lesson->ID ] = [
							'id'    => $quiz->id,
							'title' => $quiz->title,
						];
					}
				}
			}
		}

		return $lesson_quizzes;
	}

	/**
	 * AJAX handler for searching quizzes
	 *
	 * @since 1.0.0
	 */
	public function ajax_search_quizzes() {
		// Verify nonce.
		if ( ! check_ajax_referer( 'pressprimer_quiz_tutorlms_search', 'nonce', false ) ) {
			wp_send_json_error( [ 'message' => __( 'Security check failed.', 'pressprimer-quiz' ) ] );
		}

		// Check permissions.
		if ( ! current_user_can( 'edit_posts' ) ) {
			wp_send_json_error( [ 'message' => __( 'Permission denied.', 'pressprimer-quiz' ) ] );
		}

		global $wpdb;
		$table = $wpdb->prefix . 'ppq_quizzes';

		// Users with manage_all see every published quiz; teacher-scoped users
		// see only their own. Same rule applies to both recent and search.
		$can_see_all = current_user_can( 'pressprimer_quiz_manage_all' );

		$recent = isset( $_POST['recent'] ) && rest_sanitize_boolean( wp_unslash( $_POST['recent'] ) );

		if ( $recent ) {
			if ( $can_see_all ) {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- AJAX search results, not suitable for caching
				$quizzes = $wpdb->get_results(
					"SELECT id, title FROM {$table} WHERE status = 'published' ORDER BY id DESC LIMIT 50" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				);
			} else {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- AJAX search results, not suitable for caching
				$quizzes = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT id, title FROM {$table} WHERE status = 'published' AND owner_id = %d ORDER BY id DESC LIMIT 50", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
						get_current_user_id()
					)
				);
			}

			wp_send_json_success( [ 'quizzes' => $quizzes ] );
			return;
		}

		$search = isset( $_POST['search'] ) ? sanitize_text_field( wp_unslash( $_POST['search'] ) ) : '';

		if ( strlen( $search ) < 2 ) {
			wp_send_json_success( [ 'quizzes' => [] ] );
		}

		if ( $can_see_all ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- AJAX search results, not suitable for caching
			$quizzes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT id, title FROM {$table} WHERE title LIKE %s AND status = 'published' ORDER BY title ASC LIMIT 20", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					'%' . $wpdb->esc_like( $search ) . '%'
				)
			);
		} else {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- AJAX search results, not suitable for caching
			$quizzes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT id, title FROM {$table} WHERE title LIKE %s AND status = 'published' AND owner_id = %d ORDER BY title ASC LIMIT 20", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					'%' . $wpdb->esc_like( $search ) . '%',
					get_current_user_id()
				)
			);
		}

		wp_send_json_success( [ 'quizzes' => $quizzes ] );
	}

	/**
	 * Append quiz to lesson content via the_content filter.
	 *
	 * Tutor LMS renders lesson text via the_content() inside its own template
	 * loader (tutor_load_template) which bypasses the WordPress loop. This means
	 * in_the_loop() and is_main_query() return false. Additionally, Tutor LMS
	 * spotlight mode loads lesson content via AJAX (tutor_render_lesson_content),
	 * where is_singular() returns false entirely. We guard with a post_type
	 * check on the current post instead, which works in both contexts.
	 *
	 * @since 2.1.0
	 *
	 * @param string $content The post content.
	 * @return string Modified content with quiz appended.
	 */
	public function append_quiz_to_lesson_content( $content ) {
		// Only for Tutor LMS lesson posts. We check $post->post_type instead of
		// is_singular('lesson') because Tutor's spotlight mode loads lessons via
		// AJAX where the main WP query is not a singular lesson query.
		$post = get_post();
		if ( ! $post || 'lesson' !== $post->post_type ) {
			return $content;
		}

		// Prevent double-rendering (the_content can fire multiple times).
		if ( $this->lesson_quiz_rendered ) {
			return $content;
		}

		$post_id = get_the_ID();
		$quiz_id = get_post_meta( $post_id, self::META_KEY_QUIZ_ID, true );

		if ( ! $quiz_id ) {
			return $content;
		}

		// Mark as rendered before processing.
		$this->lesson_quiz_rendered = true;

		// Check if user is enrolled in the course.
		$course_id = $this->get_course_id_for_lesson( $post_id );
		$user_id   = get_current_user_id();

		if ( $course_id && ! $this->is_user_enrolled( $user_id, $course_id ) ) {
			$content .= '<div class="ppq-tutorlms-access-denied">';
			$content .= '<p>' . esc_html__( 'Enroll in this course to access the quiz.', 'pressprimer-quiz' ) . '</p>';
			$content .= '</div>';
			return $content;
		}

		// Add context data for navigation.
		$context_data = [
			'tutorlms_lesson_id' => $post_id,
			'tutorlms_course_id' => $course_id,
		];

		// Render quiz shortcode with context.
		$quiz_shortcode = sprintf(
			'[pressprimer_quiz id="%d" context="%s"]',
			absint( $quiz_id ),
			esc_attr( base64_encode( wp_json_encode( $context_data ) ) )
		);

		$content .= '<div class="ppq-tutorlms-quiz-wrapper">';
		$content .= do_shortcode( $quiz_shortcode );
		$content .= '</div>';

		return $content;
	}

	/**
	 * Force Tutor LMS to show the Overview tab when a quiz is mapped.
	 *
	 * Tutor's Lesson::has_lesson_content() returns false for non-admin users
	 * when the lesson post_content is empty. This causes the Overview tab
	 * (and its the_content() call) to be hidden, preventing our quiz from
	 * rendering. We override it to true when a PPQ quiz is attached.
	 *
	 * @since 2.1.0
	 *
	 * @param bool $has_content Whether Tutor thinks the lesson has content.
	 * @param int  $lesson_id   Lesson post ID.
	 * @return bool True if a quiz is mapped, original value otherwise.
	 */
	public function force_lesson_content_for_quiz( $has_content, $lesson_id ) {
		if ( $has_content ) {
			return $has_content;
		}

		$quiz_id = get_post_meta( $lesson_id, self::META_KEY_QUIZ_ID, true );

		return ! empty( $quiz_id );
	}

	/**
	 * Display topic quizzes on course page
	 *
	 * This adds PPQ quizzes to the topic content list on the course page.
	 *
	 * @since 1.0.0
	 *
	 * @param int $course_id Course ID.
	 */
	public function display_topic_quizzes( $course_id ) {
		// Get all topics for this course.
		$topics = get_posts(
			[
				'post_type'      => 'topics',
				'post_parent'    => $course_id,
				'posts_per_page' => -1,
				'post_status'    => 'publish',
				'orderby'        => 'menu_order',
				'order'          => 'ASC',
			]
		);

		$has_quizzes = false;
		foreach ( $topics as $topic ) {
			$quiz_id = get_post_meta( $topic->ID, self::META_KEY_TOPIC_QUIZ_ID, true );
			if ( $quiz_id ) {
				$has_quizzes = true;
				break;
			}
		}

		if ( ! $has_quizzes ) {
			return;
		}

		// Enqueue frontend styles for topic quizzes.
		wp_add_inline_style(
			'tutor-frontend',
			'
			.ppq-topic-quiz-item {
				display: flex;
				align-items: center;
				padding: 12px 16px;
				background: #f8f9fa;
				border-left: 3px solid #7b68ee;
				margin: 8px 0;
				border-radius: 4px;
			}
			.ppq-topic-quiz-item .ppq-quiz-icon {
				margin-right: 12px;
				color: #7b68ee;
			}
			.ppq-topic-quiz-item .ppq-quiz-title {
				flex: 1;
				font-weight: 500;
			}
			.ppq-topic-quiz-item .ppq-quiz-link {
				color: #7b68ee;
				text-decoration: none;
			}
			.ppq-topic-quiz-item .ppq-quiz-link:hover {
				text-decoration: underline;
			}
		'
		);
	}

	/**
	 * Inject topic quiz content into the topic content list
	 *
	 * @since 1.0.0
	 *
	 * @param array $contents Topic contents array.
	 * @param int   $topic_id Topic post ID.
	 * @return array Modified contents array.
	 */
	public function inject_topic_quiz_content( $contents, $topic_id ) {
		$quiz_id = get_post_meta( $topic_id, self::META_KEY_TOPIC_QUIZ_ID, true );

		if ( ! $quiz_id ) {
			return $contents;
		}

		$quiz = PressPrimer_Quiz_Quiz::get( $quiz_id );
		if ( ! $quiz ) {
			return $contents;
		}

		// Create a pseudo-content item for the PPQ quiz.
		$quiz_content = (object) [
			'ID'           => 'ppq-quiz-' . $quiz_id,
			'post_title'   => $quiz->title,
			'post_type'    => 'ppq_quiz',
			'post_status'  => 'publish',
			'ppq_quiz_id'  => $quiz_id,
			'ppq_topic_id' => $topic_id,
			'is_ppq_quiz'  => true,
		];

		// Add to the end of the contents.
		$contents[] = $quiz_content;

		return $contents;
	}

	/**
	 * Maybe hide the complete button when quiz is attached
	 *
	 * @since 1.0.0
	 *
	 * @param string $form Complete form HTML.
	 * @return string Modified form HTML.
	 */
	public function maybe_hide_complete_button( $form ) {
		$post_id = get_the_ID();
		$quiz_id = get_post_meta( $post_id, self::META_KEY_QUIZ_ID, true );

		if ( $quiz_id ) {
			// Only hide the complete button if "Require passing score" is enabled.
			$require_pass = get_post_meta( $post_id, self::META_KEY_REQUIRE_PASS, true );

			if ( '1' === $require_pass ) {
				// If the lesson is already marked complete, don't hide the button.
				if ( function_exists( 'tutor_utils' ) ) {
					$completed = tutor_utils()->is_completed_lesson( $post_id, get_current_user_id() );
					if ( false !== $completed ) {
						return $form;
					}
				}

				// If the user has already passed this quiz, don't hide the button.
				$user_id = get_current_user_id();
				if ( $user_id && $this->user_has_passed_quiz( (int) $quiz_id, $user_id ) ) {
					return $form;
				}

				// Return empty to hide the complete button - user must pass quiz.
				return '';
			}
		}

		return $form;
	}

	/**
	 * Check if a user has passed a specific quiz
	 *
	 * @since 2.1.0
	 *
	 * @param int $quiz_id PPQ Quiz ID.
	 * @param int $user_id User ID.
	 * @return bool True if user has at least one passing submitted attempt.
	 */
	private function user_has_passed_quiz( int $quiz_id, int $user_id ): bool {
		global $wpdb;
		$table = $wpdb->prefix . 'ppq_attempts';

		// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- One-off check, not suitable for persistent caching.
		$passed = $wpdb->get_var(
			$wpdb->prepare(
				"SELECT COUNT(*) FROM {$table} WHERE quiz_id = %d AND user_id = %d AND status = 'submitted' AND passed = 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is constructed from $wpdb->prefix.
				$quiz_id,
				$user_id
			)
		);

		return $passed > 0;
	}

	/**
	 * Handle quiz passed event
	 *
	 * @since 1.0.0
	 *
	 * @param PressPrimer_Quiz_Attempt $attempt Attempt object.
	 * @param PressPrimer_Quiz_Quiz    $quiz    Quiz object.
	 */
	public function handle_quiz_passed( $attempt, $quiz ) {
		// Only for logged-in users.
		if ( ! $attempt->user_id ) {
			return;
		}

		// Find TutorLMS lessons that use this quiz.
		$lessons = $this->get_lessons_for_quiz( $quiz->id );

		foreach ( $lessons as $lesson ) {
			$require_pass = get_post_meta( $lesson->ID, self::META_KEY_REQUIRE_PASS, true );

			if ( '1' === $require_pass ) {
				$this->mark_lesson_complete( $lesson->ID, $attempt->user_id );
			}
		}
	}

	/**
	 * Get lessons that use a specific quiz
	 *
	 * @since 1.0.0
	 *
	 * @param int $quiz_id Quiz ID.
	 * @return array Array of post objects.
	 */
	private function get_lessons_for_quiz( $quiz_id ) {
		$args = [
			'post_type'      => 'lesson',
			'posts_per_page' => -1,
			'meta_query'     => [
				[
					'key'   => self::META_KEY_QUIZ_ID,
					'value' => $quiz_id,
				],
			],
		];

		return get_posts( $args );
	}

	/**
	 * Mark a TutorLMS lesson as complete
	 *
	 * @since 1.0.0
	 *
	 * @param int $lesson_id Lesson post ID.
	 * @param int $user_id   User ID.
	 */
	private function mark_lesson_complete( $lesson_id, $user_id ) {
		// Use TutorLMS LessonModel class (TutorLMS 2.x+).
		if ( class_exists( '\Tutor\Models\LessonModel' ) ) {
			\Tutor\Models\LessonModel::mark_lesson_complete( $lesson_id, $user_id );

			/**
			 * Fires after PPQ marks a TutorLMS lesson complete.
			 *
			 * @since 1.0.0
			 *
			 * @param int $lesson_id Lesson post ID.
			 * @param int $user_id   User ID.
			 */
			do_action( 'pressprimer_quiz_tutorlms_lesson_completed', $lesson_id, $user_id );

			// Trigger auto-course-completion if all lessons are now complete.
			// TutorLMS only checks this on course page load, so we trigger it here
			// to complete the course immediately after the final lesson finishes.
			$this->maybe_auto_complete_course( $lesson_id, $user_id );
		}
	}

	/**
	 * Trigger TutorLMS auto-course-completion if all content is done
	 *
	 * TutorLMS normally only checks for auto-completion on course page load.
	 * This method triggers the check immediately after a lesson is programmatically
	 * marked complete, so the course completes without requiring a page visit.
	 *
	 * @since 2.1.0
	 *
	 * @param int $lesson_id Lesson post ID.
	 * @param int $user_id   User ID.
	 */
	private function maybe_auto_complete_course( $lesson_id, $user_id ) {
		if ( ! class_exists( '\Tutor\Models\CourseModel' ) ) {
			return;
		}

		$course_id = $this->get_course_id_for_lesson( $lesson_id );
		if ( ! $course_id ) {
			return;
		}

		if ( \Tutor\Models\CourseModel::can_autocomplete_course( $course_id, $user_id ) ) {
			\Tutor\Models\CourseModel::mark_course_as_completed( $course_id, $user_id );
		}
	}

	/**
	 * Get course ID for a lesson
	 *
	 * @since 1.0.0
	 *
	 * @param int $lesson_id Lesson post ID.
	 * @return int|null Course ID or null.
	 */
	private function get_course_id_for_lesson( $lesson_id ) {
		// TutorLMS stores course ID in post meta.
		$course_id = get_post_meta( $lesson_id, '_tutor_course_id_for_lesson', true );

		if ( $course_id ) {
			return (int) $course_id;
		}

		// Try TutorLMS function.
		if ( function_exists( 'tutor_utils' ) ) {
			$utils = tutor_utils();
			if ( method_exists( $utils, 'get_course_id_by_lesson' ) ) {
				return $utils->get_course_id_by_lesson( $lesson_id );
			}
		}

		return null;
	}

	/**
	 * Check if user has access to a course
	 *
	 * Checks enrollment, public course status, admin/instructor status,
	 * and completed course enrollment records.
	 *
	 * @since 1.0.0
	 *
	 * @param int $user_id   User ID.
	 * @param int $course_id Course ID.
	 * @return bool True if user has access.
	 */
	private function is_user_enrolled( $user_id, $course_id ) {
		// Public courses are accessible to everyone (no enrollment required).
		$is_public = get_post_meta( $course_id, '_tutor_is_public_course', true );
		if ( 'yes' === $is_public ) {
			return true;
		}

		// Admins and course instructors always have access.
		if ( current_user_can( 'manage_options' ) ) {
			return true;
		}

		if ( function_exists( 'tutor_utils' ) ) {
			$utils = tutor_utils();

			// Check if user is an instructor for this course.
			if ( method_exists( $utils, 'is_instructor_of_this_course' ) ) {
				if ( $utils->is_instructor_of_this_course( $user_id, $course_id ) ) {
					return true;
				}
			}

			// Check enrollment (includes completed courses — enrollment record
			// keeps post_status='completed' even after course completion).
			if ( method_exists( $utils, 'is_enrolled' ) ) {
				if ( $utils->is_enrolled( $course_id, $user_id ) ) {
					return true;
				}
			}
		}

		if ( ! $user_id ) {
			return false;
		}

		return true; // Default to allowing if we can't check.
	}

	/**
	 * Map TutorLMS Instructor role to PPQ teacher capabilities
	 *
	 * Grants pressprimer_quiz_manage_own and related capabilities to the
	 * tutor_instructor role so instructors can create and manage their own
	 * quizzes, questions, and banks.
	 *
	 * @since 2.1.0
	 */
	private function map_instructor_capabilities() {
		$instructor = get_role( 'tutor_instructor' );

		if ( ! $instructor ) {
			return;
		}

		// Only add capabilities if the role doesn't already have them.
		if ( ! $instructor->has_cap( 'pressprimer_quiz_manage_own' ) ) {
			$instructor->add_cap( 'pressprimer_quiz_manage_own' );
			$instructor->add_cap( 'pressprimer_quiz_view_results_own' );
			$instructor->add_cap( 'pressprimer_quiz_take_quiz' );
		}
	}

	/**
	 * Check if user is a TutorLMS Instructor
	 *
	 * @since 2.1.0
	 *
	 * @param bool $has_capability Whether user has teacher capability.
	 * @param int  $user_id        User ID.
	 * @return bool Modified capability.
	 */
	public function check_instructor_capability( $has_capability, $user_id ) {
		if ( $has_capability ) {
			return $has_capability;
		}

		// Check if user is a TutorLMS Instructor.
		$user = get_userdata( $user_id );
		if ( $user && in_array( 'tutor_instructor', (array) $user->roles, true ) ) {
			return true;
		}

		return $has_capability;
	}

	/**
	 * Register REST routes
	 *
	 * @since 1.0.0
	 */
	public function register_rest_routes() {
		register_rest_route(
			'ppq/v1',
			'/tutorlms/quizzes/search',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'rest_search_quizzes' ],
				'permission_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
				'args'                => [
					'search' => [
						'required'          => false,
						'sanitize_callback' => 'sanitize_text_field',
					],
					'recent' => [
						'required'          => false,
						'sanitize_callback' => 'absint',
					],
				],
			]
		);

		register_rest_route(
			'ppq/v1',
			'/tutorlms/status',
			[
				'methods'             => 'GET',
				'callback'            => [ $this, 'rest_get_status' ],
				'permission_callback' => function () {
					return current_user_can( 'manage_options' );
				},
			]
		);

		register_rest_route(
			'ppq/v1',
			'/tutorlms/lesson-quiz',
			[
				'methods'             => 'POST',
				'callback'            => [ $this, 'rest_save_lesson_quiz' ],
				'permission_callback' => function () {
					return current_user_can( 'edit_posts' );
				},
				'args'                => [
					'course_id' => [
						'required'          => false,
						'sanitize_callback' => 'absint',
					],
					'lesson_id' => [
						'required'          => true,
						'sanitize_callback' => 'sanitize_text_field',
					],
					'quiz_id'   => [
						'required'          => true,
						'sanitize_callback' => 'absint',
					],
				],
			]
		);
	}

	/**
	 * REST endpoint: Search quizzes
	 *
	 * @since 1.0.0
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response Response object.
	 */
	public function rest_search_quizzes( $request ) {
		global $wpdb;

		$table  = $wpdb->prefix . 'ppq_quizzes';
		$recent = $request->get_param( 'recent' );

		// Users with manage_all see every published quiz; teacher-scoped users
		// see only their own. Same rule applies to both recent and search.
		$can_see_all = current_user_can( 'pressprimer_quiz_manage_all' );

		if ( $recent ) {
			if ( $can_see_all ) {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- REST search results, not suitable for caching
				$quizzes = $wpdb->get_results(
					"SELECT id, title FROM {$table} WHERE status = 'published' ORDER BY id DESC LIMIT 50" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
				);
			} else {
				// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- REST search results, not suitable for caching
				$quizzes = $wpdb->get_results(
					$wpdb->prepare(
						"SELECT id, title FROM {$table} WHERE status = 'published' AND owner_id = %d ORDER BY id DESC LIMIT 50", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
						get_current_user_id()
					)
				);
			}

			return new WP_REST_Response(
				[
					'success' => true,
					'quizzes' => $quizzes,
				]
			);
		}

		$search = $request->get_param( 'search' );

		if ( empty( $search ) || strlen( $search ) < 2 ) {
			return new WP_REST_Response(
				[
					'success' => true,
					'quizzes' => [],
				]
			);
		}

		if ( $can_see_all ) {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- REST search results, not suitable for caching
			$quizzes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT id, title FROM {$table} WHERE title LIKE %s AND status = 'published' ORDER BY title ASC LIMIT 20", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					'%' . $wpdb->esc_like( $search ) . '%'
				)
			);
		} else {
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- REST search results, not suitable for caching
			$quizzes = $wpdb->get_results(
				$wpdb->prepare(
					"SELECT id, title FROM {$table} WHERE title LIKE %s AND status = 'published' AND owner_id = %d ORDER BY title ASC LIMIT 20", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
					'%' . $wpdb->esc_like( $search ) . '%',
					get_current_user_id()
				)
			);
		}

		return new WP_REST_Response(
			[
				'success' => true,
				'quizzes' => $quizzes,
			]
		);
	}

	/**
	 * REST endpoint: Get TutorLMS integration status
	 *
	 * @since 1.0.0
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response Response object.
	 */
	public function rest_get_status( $request ) {
		$status = [
			'active'      => defined( 'TUTOR_VERSION' ),
			'version'     => defined( 'TUTOR_VERSION' ) ? TUTOR_VERSION : null,
			'integration' => 'working',
		];

		// Count how many TutorLMS lessons have PPQ quizzes attached.
		if ( $status['active'] ) {
			global $wpdb;
			// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching -- Status count query, not suitable for caching
			$count                      = $wpdb->get_var(
				$wpdb->prepare(
					"SELECT COUNT(*) FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value != ''", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- WordPress core table name
					self::META_KEY_QUIZ_ID
				)
			);
			$status['attached_quizzes'] = (int) $count;
		}

		return new WP_REST_Response(
			[
				'success' => true,
				'status'  => $status,
			]
		);
	}

	/**
	 * REST endpoint: Save lesson quiz association
	 *
	 * @since 1.0.0
	 *
	 * @param WP_REST_Request $request Request object.
	 * @return WP_REST_Response Response object.
	 */
	public function rest_save_lesson_quiz( $request ) {
		$lesson_id = $request->get_param( 'lesson_id' );
		$quiz_id   = $request->get_param( 'quiz_id' );

		// Handle temporary lesson IDs (lesson-0, lesson-1, etc.).
		$is_temp_id = strpos( $lesson_id, 'lesson-' ) === 0;

		if ( $is_temp_id ) {
			return new WP_REST_Response(
				[
					'success' => false,
					'message' => __( 'Please save the lesson first before attaching a quiz.', 'pressprimer-quiz' ),
				],
				400
			);
		}

		$lesson_id = absint( $lesson_id );

		// Validate lesson exists.
		$lesson = get_post( $lesson_id );
		if ( ! $lesson || 'lesson' !== $lesson->post_type ) {
			return new WP_REST_Response(
				[
					'success' => false,
					'message' => __( 'Invalid lesson.', 'pressprimer-quiz' ),
				],
				400
			);
		}

		// Check user can edit this lesson.
		// Note: Tutor LMS registers custom capabilities (edit_tutor_lesson) that may not
		// be in the database if Tutor wasn't properly activated. We check manage_options
		// (admin) or the Tutor-specific edit_tutor_lesson capability, plus verify the
		// lesson author matches for non-admins.
		$can_edit = current_user_can( 'manage_options' )
			|| current_user_can( 'edit_tutor_lesson' )
			|| ( (int) $lesson->post_author === get_current_user_id() && current_user_can( 'edit_posts' ) );

		if ( ! $can_edit ) {
			return new WP_REST_Response(
				[
					'success' => false,
					'message' => __( 'Permission denied.', 'pressprimer-quiz' ),
				],
				403
			);
		}

		// Save or remove the quiz association.
		if ( $quiz_id ) {
			// Validate quiz exists.
			$quiz = PressPrimer_Quiz_Quiz::get( $quiz_id );
			if ( ! $quiz ) {
				return new WP_REST_Response(
					[
						'success' => false,
						'message' => __( 'Quiz not found.', 'pressprimer-quiz' ),
					],
					400
				);
			}

			update_post_meta( $lesson_id, self::META_KEY_QUIZ_ID, $quiz_id );
			// Default to requiring pass when attaching a quiz via REST API.
			update_post_meta( $lesson_id, self::META_KEY_REQUIRE_PASS, '1' );
		} else {
			delete_post_meta( $lesson_id, self::META_KEY_QUIZ_ID );
			delete_post_meta( $lesson_id, self::META_KEY_REQUIRE_PASS );
		}

		return new WP_REST_Response(
			[
				'success'   => true,
				'lesson_id' => $lesson_id,
				'quiz_id'   => $quiz_id,
			]
		);
	}
}
