<?php
/**
 * File Path: includes/admin/tabs/general/class-mbs-tab-general-display.php
 *
 * General Tab — Display, Visibility & Pagination Field Group
 *
 * Renders five display toggles inside the Display Options card. The parent
 * (MBS_Tab_General) owns the surrounding .mbs-settings-card wrapper; this
 * class outputs a .mbs-display-options-grid containing five
 * .mbs-display-option-cell blocks arranged in a 2-column responsive grid.
 *
 * Option keys read:
 *   mbs_setting_general_show_isbn
 *   mbs_setting_general_show_isbn10
 *   mbs_setting_general_show_more_in_series
 *   mbs_setting_general_show_more_by_author
 *   mbs_setting_general_coming_soon_badge   (PRO only)
 *
 * SET-01: mbs_setting_general_default_language moved to MBS_Tab_General_Currency
 *         (Regional & Currency Settings card) — language is a regional setting.
 * All checkbox IDs/names are preserved from the pre-Phase-4b markup so the
 * save handler (class-mbs-tab-general-save.php) requires no changes.
 *
 * Phase 4b (2026-04-19):
 *   · Layout changed from stacked <tr> rows to a 2-column responsive grid
 *     per client structural redesign. Stacks to 1 column below 782px.
 *   · Each cell now includes a helper description line below the label
 *     explaining what the toggle does (exceeds client spec — client
 *     approved UX improvement 2026-04-19).
 *   · Method renamed render_rows() → render_fields() to reflect output is
 *     no longer <tr> rows for an outer <table>.
 *   · D-6 comment retained: items_per_page remains in the Formatting tab.
 *
 * Sprint 13+: new visibility/display fields (reading progress, custom badges,
 * bulk ISBN toggles, etc.) append as additional cells in render_fields().
 * Do not add them to any other file.
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class MBS_Tab_General_Display { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound -- mbs_/MBS_ prefix is correct; scanner not configured with plugin prefix

	/**
	 * Render a single display-option cell (checkbox + label + helper text).
	 *
	 * Private helper to keep render_fields() uncluttered. Each cell shares
	 * the same structure; only the IDs, option key, default, label, and
	 * helper text vary.
	 *
	 * @param string $field_id    DOM id + form name for the checkbox.
	 * @param string $option_key  wp_options key to read via get_option().
	 * @param string $default     Default '1'/'0' if option is unset.
	 * @param string $label       Visible label text (already translated).
	 * @param string $description Helper text below the label (already translated).
	 * @param bool   $is_pro_only When true, the checkbox is disabled for FREE users
	 *                            and a PRO badge is rendered next to the label.
	 */
	private function render_cell(
		string $field_id,
		string $option_key,
		string $default,
		string $label,
		string $description,
		bool $is_pro_only = false
	): void {
		$is_disabled = $is_pro_only && ! mbs_is_pro();
		?>
		<div class="mbs-display-option-cell">
			<input type="checkbox"
			       id="<?php echo esc_attr( $field_id ); ?>"
			       name="<?php echo esc_attr( $field_id ); ?>"
			       value="1"
			       <?php checked( get_option( $option_key, $default ), '1' ); ?>
			       <?php disabled( $is_disabled, true ); ?>>
			<label for="<?php echo esc_attr( $field_id ); ?>">
				<?php echo esc_html( $label ); ?>
				<?php if ( $is_pro_only && ! mbs_is_pro() ) : ?>
					<span class="pro-badge"><?php esc_html_e( 'PRO', 'my-book-showroom' ); ?></span>
				<?php endif; ?>
			</label>
			<p class="description"><?php echo esc_html( $description ); ?></p>
		</div>
		<?php
	}

	/**
	 * Render the display toggle cells.
	 *
	 * Checkbox cells appear in a .mbs-display-options-grid.
	 * SET-01: Default Language field moved to MBS_Tab_General_Currency.
	 */
	public function render_fields(): void {
		?>
		<div class="mbs-display-options-grid">

			<?php
			$this->render_cell(
				'mbs_general_show_isbn',
				'mbs_setting_general_show_isbn',
				'1',
				__( 'Show ISBN-13', 'my-book-showroom' ),
				__( 'Display the 13-digit ISBN on single-book pages and archives.', 'my-book-showroom' )
			);

			$this->render_cell(
				'mbs_general_show_isbn10',
				'mbs_setting_general_show_isbn10',
				'0',
				__( 'Show ISBN-10', 'my-book-showroom' ),
				__( 'Display the legacy 10-digit ISBN alongside ISBN-13 when available.', 'my-book-showroom' )
			);

			$this->render_cell(
				'mbs_general_show_more_in_series',
				'mbs_setting_general_show_more_in_series',
				'1',
				__( 'Show "More in Series"', 'my-book-showroom' ),
				__( 'Show related titles from the same series on single-book pages.', 'my-book-showroom' )
			);

			$this->render_cell(
				'mbs_general_show_more_by_author',
				'mbs_setting_general_show_more_by_author',
				'1',
				__( 'Show "More by Author"', 'my-book-showroom' ),
				__( 'Show other books by the same author on single-book pages.', 'my-book-showroom' )
			);

			$this->render_cell(
				'mbs_general_coming_soon_badge',
				'mbs_setting_general_coming_soon_badge',
				'0',
				__( 'Show "Coming Soon" Badge', 'my-book-showroom' ),
				__( 'Display a badge on books flagged as upcoming releases. Requires the Coming Soon feature (arriving in a future release).', 'my-book-showroom' ),
				true
			);

			// Sprint RO-Display (4a): Series Reading Order global toggle.
			$this->render_cell(
				'mbs_show_series_reading_order',
				'mbs_setting_show_series_reading_order',
				'0',
				__( 'Show Series Reading Order', 'my-book-showroom' ),
				__( 'When enabled, single-book pages display a Reading Order row below the Series Order row in the More in This Series section. Authors configure reading order positions in Book Details.', 'my-book-showroom' ),
				true // PRO-only — RO-Display gate fix.
			);
			?>

		</div>

		<?php /* D-6: items_per_page removed from General tab. Setting is now in Formatting tab
		          (mbs_setting_formatting_items_per_page). MBS_Book_Query reads the Formatting key. */ ?>
		<?php
	}
}
