<?php
/**
 * File Path: includes/admin/tabs/class-mbs-tab-general-render.php
 *
 * General Tab -- Render Class
 *
 * Delegates to child panel render methods:
 *   MBS_Tab_General_Currency::render_fields() -- Regional & Currency Settings card
 *   MBS_Tab_General_Display::render_fields()  -- Display Options card
 *
 * Layout: two-column grid -- primary (frequently-changed) left, sidebar (configure-once) right.
 * Collapses to single column below 782px (WP tablet breakpoint).
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Admin/Tabs
 * @since      1.0.0
 */

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

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

	/**
	 * Render the General Settings tab.
	 *
	 * @return void
	 */
	public static function render(): void {
		$current_currency = get_option( 'mbs_setting_general_currency', 'USD' );

		// S-04: Show save confirmation after PRG redirect.
		if ( isset( $_GET['updated'] ) && '1' === $_GET['updated'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			add_settings_error( 'mbs_general', 'mbs_general_updated', __( 'Settings saved.', 'my-book-showroom' ), 'updated' );
		}
		settings_errors( 'mbs_general' );

		$disclosure_enabled = get_option( 'mbs_setting_general_disclosure_enabled', '0' );
		$disclosure_text    = get_option( 'mbs_setting_general_disclosure_text', '' );
		?>
		<form method="post" action="<?php echo esc_url( admin_url( 'admin.php?page=my-book-showroom-settings&tab=general' ) ); ?>">
		<div class="mbs-tab-general">

			<?php /* UX-01: Top save button — must precede <h2> so the float
			          establishes before the heading and they sit on the same line. */ ?>
			<div class="mbs-save-top"><?php submit_button( null, 'primary', 'submit', true ); ?></div>
			<h2><?php esc_html_e( 'General Settings', 'my-book-showroom' ); ?></h2>
			<p class="mbs-settings-subtitle">
				<?php esc_html_e( 'Configure currency, timezone, and core display options for your book showroom.', 'my-book-showroom' ); ?>
			</p>

			<div class="mbs-general-layout">

				<div class="mbs-settings-card">
					<h3><?php esc_html_e( 'Regional &amp; Currency Settings', 'my-book-showroom' ); ?></h3>
					<?php ( new MBS_Tab_General_Currency() )->render_fields(); ?>
				</div>

				<div class="mbs-settings-card">
					<h3><?php esc_html_e( 'Display Options', 'my-book-showroom' ); ?></h3>
					<?php ( new MBS_Tab_General_Display() )->render_fields(); ?>
				</div>

				<?php /* S13-12: Affiliate Disclosure card */ ?>
				<div class="mbs-settings-card">
					<h3><?php esc_html_e( 'Affiliate Disclosure', 'my-book-showroom' ); ?></h3>
					<table class="form-table"><tbody>
						<tr>
							<th scope="row"><?php esc_html_e( 'Enable Disclosure', 'my-book-showroom' ); ?></th>
							<td>
								<label>
									<input type="checkbox" name="mbs_general_disclosure_enabled" value="1"
										<?php checked( '1', $disclosure_enabled ); ?>>
									<?php esc_html_e( 'Show affiliate disclosure notice on book pages', 'my-book-showroom' ); ?>
								</label>
							</td>
						</tr>
						<tr>
							<th scope="row">
								<label for="mbs_general_disclosure_text"><?php esc_html_e( 'Disclosure Text', 'my-book-showroom' ); ?></label>
							</th>
							<td>
								<textarea id="mbs_general_disclosure_text" name="mbs_general_disclosure_text"
								          rows="5" class="large-text"
								          placeholder="<?php esc_attr_e( 'This page contains affiliate links from various programs. As an author/publisher, I may earn a commission from qualifying purchases made through these links at no extra cost to you. This helps support my work.', 'my-book-showroom' ); ?>"><?php echo esc_textarea( $disclosure_text ); ?></textarea>
								<p class="description">
									<?php esc_html_e( "Displayed below buy links. Example: 'This page contains affiliate links. I may earn a commission from qualifying purchases at no extra cost to you.'", 'my-book-showroom' ); ?>
								</p>
							</td>
						</tr>
					</tbody></table>
				</div>

				<?php /* WooCommerce Integration moved to Settings -> Advanced */ ?>

				<!-- GC2-4: Scheduled Releases -- PRO-only front-end gate -->
				<div class="mbs-settings-card">
					<h3><?php esc_html_e( 'Scheduled Releases', 'my-book-showroom' ); ?><?php mbs_help_tip(
					__( 'When enabled, books with a future MBS Publication Date (set in the book editor under Publication) are hidden from the front-end catalog until that date arrives. This uses the MBS Publication Date field — not the WordPress post publish date. A book can be WordPress-published (visible in the admin) while still hidden on the front end because its MBS Publication Date is in the future.', 'my-book-showroom' ),
					__( 'About Scheduled Releases', 'my-book-showroom' ),
					'mbs-help-tip--wide'
				); ?></h3>
					<?php if ( mbs_is_pro() ) : ?>
					<label>
						<input type="checkbox"
						       name="mbs_setting_general_enable_scheduled_releases"
						       value="1"
						       <?php checked( get_option( 'mbs_setting_general_enable_scheduled_releases' ), '1' ); ?>>
						<?php esc_html_e( 'Enable scheduled releases', 'my-book-showroom' ); ?>
					</label>
					<p class="description">
						<?php esc_html_e( 'When enabled, books with a future publication date are hidden from the front-end catalog until their release date/time arrives.', 'my-book-showroom' ); ?>
					</p>
					<?php else : ?>
					<!-- MBS: enable_scheduled_releases requires PRO license -->
					<?php endif; ?>
				</div>

			</div><!-- .mbs-general-layout -->

			<!-- Hidden field so Formatting tab can read the current currency -->
			<input type="hidden" id="mbs_current_currency" name="mbs_current_currency" value="<?php echo esc_attr( $current_currency ); ?>">

			<!-- CSRF nonce field -->
			<?php wp_nonce_field( 'mbs_general_save', 'mbs_general_nonce' ); ?>

			<p class="mbs-settings-actions">
				<?php submit_button( null, 'primary', 'submit', false ); ?>
			</p>

		</div>
		</form>
		<?php
	}
}
