<?php
/**
 * File Path: includes/admin/tabs/general/class-mbs-tab-general-save.php
 *
 * General Tab — Save Handler
 *
 * Owns all update_option() calls for Tab 1 (General Settings) and
 * the Post-Redirect-Get redirect after a successful save.
 *
 * Option keys written (all use the canonical mbs_setting_general_* prefix):
 *   mbs_setting_general_currency
 *   mbs_setting_general_timezone
 *   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
 *   mbs_setting_general_default_language
 *   mbs_setting_general_disclosure_enabled   (S13-12)
 *   mbs_setting_general_disclosure_text      (S13-12)
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

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

// phpcs:disable WordPress.Security.NonceVerification -- nonce verified in parent class verify_save() / check_admin_referer() before $_POST access; PHPCS cannot trace cross-method nonce verification
class MBS_Tab_General_Save {

	/**
	 * Write all General tab options from the current POST payload.
	 *
	 * Called by MBS_Tab_General::save_settings() after nonce verification
	 * and capability check. Reads directly from $_POST.
	 *
	 * Issues a Post-Redirect-Get redirect on completion and calls exit.
	 *
	 * @return void Does not return — exits after redirect.
	 */
	public function save(): void {
		// ── Currency ──────────────────────────────────────────────────────────
		$currency_dropdown = sanitize_text_field( wp_unslash( $_POST['mbs_general_currency']        ?? 'USD' ) );
		$currency_custom   = sanitize_text_field( wp_unslash( $_POST['mbs_general_currency_custom'] ?? ''    ) );
		$currency          = ( 'other' === $currency_dropdown ) ? $currency_custom : $currency_dropdown;
		if ( '' === $currency ) {
			$currency = 'USD';
		}
		update_option( 'mbs_setting_general_currency', $currency );

		// ── Timezone ──────────────────────────────────────────────────────────
		$valid_zones  = timezone_identifiers_list();
		$wp_tz_string = function_exists( 'wp_timezone_string' ) ? wp_timezone_string() : '';
		$wp_fallback  = in_array( $wp_tz_string, $valid_zones, true ) ? $wp_tz_string : 'UTC';

		$tz_raw = sanitize_text_field( wp_unslash( $_POST['mbs_general_timezone'] ?? $wp_fallback ) );
		$tz     = in_array( $tz_raw, $valid_zones, true ) ? $tz_raw : $wp_fallback;
		update_option( 'mbs_setting_general_timezone', $tz );

		// ── Display toggles ───────────────────────────────────────────────────
		update_option( 'mbs_setting_general_show_isbn',             isset( $_POST['mbs_general_show_isbn'] )             ? '1' : '0' );
		update_option( 'mbs_setting_general_show_isbn10',           isset( $_POST['mbs_general_show_isbn10'] )           ? '1' : '0' );
		update_option( 'mbs_setting_general_show_more_in_series',   isset( $_POST['mbs_general_show_more_in_series'] )   ? '1' : '0' );
		update_option( 'mbs_setting_general_show_more_by_author',   isset( $_POST['mbs_general_show_more_by_author'] )   ? '1' : '0' );
		update_option( 'mbs_setting_general_coming_soon_badge',     isset( $_POST['mbs_general_coming_soon_badge'] )     ? '1' : '0' );
		// Sprint RO-Display (4a): Series Reading Order global toggle — PRO only.
		if ( mbs_is_pro() ) {
			update_option( 'mbs_setting_show_series_reading_order', isset( $_POST['mbs_show_series_reading_order'] ) ? '1' : '0' );
		}

		// ── Default Book Language ─────────────────────────────────────────────
		$valid_lang_codes = array(
			'af', 'ar', 'zh', 'zh-tw', 'cs', 'da', 'nl', 'en', 'fi', 'fr',
			'de', 'el', 'he', 'hi', 'hu', 'id', 'it', 'ja', 'ko', 'ms',
			'no', 'fa', 'pl', 'pt', 'pt-br', 'ro', 'ru', 'sk', 'es', 'sv',
			'th', 'tr', 'uk', 'ur', 'vi',
		);
		$lang_raw = sanitize_text_field( wp_unslash( $_POST['mbs_general_default_language'] ?? 'en' ) );
		$lang     = in_array( $lang_raw, $valid_lang_codes, true ) ? $lang_raw : 'en';
		update_option( 'mbs_setting_general_default_language', $lang );

		// ── S13-12: Affiliate Disclosure ──────────────────────────────────────
		update_option(
			'mbs_setting_general_disclosure_enabled',
			isset( $_POST['mbs_general_disclosure_enabled'] ) ? '1' : '0'
		);
		update_option(
			'mbs_setting_general_disclosure_text',
			sanitize_textarea_field( wp_unslash( $_POST['mbs_general_disclosure_text'] ?? '' ) )
		);

		// ── S15-3: WooCommerce Integration toggle moved to Settings -> Advanced.

		// ── GC2-4: Scheduled Releases toggle (PRO only) ───────────────────────
		if ( mbs_is_pro() ) {
			update_option(
				'mbs_setting_general_enable_scheduled_releases',
				isset( $_POST['mbs_general_enable_scheduled_releases'] ) ? '1' : '0'
			);
		}

		// PRG redirect.
		wp_safe_redirect(
			add_query_arg(
				array( 'page' => 'my-book-showroom-settings', 'tab' => 'general', 'updated' => '1' ),
				admin_url( 'admin.php' )
			)
		);
		exit;
	}
}
