<?php
/**
 * File Path: includes/metaboxes/class-mbs-metabox-onix-render.php
 *
 * ONIX Distribution Data Metabox -- Render Class
 *
 * Static renderer for the MBS_Metabox_Onix metabox. Called only when
 * mbs_is_pub() is true; the gate check lives in MBS_Metabox_Onix::render().
 *
 * Sections:
 *   A. Sales Territory (radio card group + per-region checkbox grid)
 *   B. Product Contact (ONIX List 198, 3.0/3.1 only)
 *
 * Audience Code (ONIX List 28) is owned by MBS_Metabox_Classification
 * (CLASS-6). mbs_audience_code is read here for display purposes only.
 *
 * @package MyBookShowroom
 * @since   1.2.0
 */

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

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

	/**
	 * All valid ISO 3166-1 alpha-2 codes supported in the territory grid,
	 * plus the pseudo-code ROW (Rest of World).
	 *
	 * @var string[]
	 */
	const COUNTRY_CODES = array(
		'US', 'CA', 'MX',
		'GB', 'DE', 'FR', 'ES', 'IT', 'NL',
		'AU', 'NZ', 'IN', 'JP', 'CN',
		'BR', 'ZA', 'ROW',
	);

	/**
	 * Country grid groups: region label => [ code => display name ].
	 *
	 * @var array<string, array<string, string>>
	 */
	const COUNTRY_GROUPS = array(
		'North America' => array(
			'US' => 'United States',
			'CA' => 'Canada',
			'MX' => 'Mexico',
		),
		'Europe' => array(
			'GB' => 'United Kingdom',
			'DE' => 'Germany',
			'FR' => 'France',
			'ES' => 'Spain',
			'IT' => 'Italy',
			'NL' => 'Netherlands',
		),
		'Asia-Pacific' => array(
			'AU' => 'Australia',
			'NZ' => 'New Zealand',
			'IN' => 'India',
			'JP' => 'Japan',
			'CN' => 'China',
		),
		'Latin America &amp; Other' => array(
			'BR'  => 'Brazil',
			'ZA'  => 'South Africa',
			'ROW' => 'Rest of World',
		),
	);

	/**
	 * ONIX List 198 product contact role options.
	 *
	 * @var array<string, string>
	 */
	const LIST_198_OPTIONS = array(
		'01' => 'Accessibility request contact',
		'02' => 'Promotional contact',
		'03' => 'Editorial contact',
		'04' => 'Sales contact',
		'05' => 'Rights and permissions contact',
		'06' => "Publisher's trade contact",
	);

	// -------------------------------------------------------------------------
	// Entry point
	// -------------------------------------------------------------------------

	/**
	 * Render the full metabox HTML.
	 *
	 * @param WP_Post $post Current post.
	 * @return void
	 */
	public static function render( WP_Post $post ): void {
		// --- Read stored values ------------------------------------------------
		$rights_row       = MBS_Book_Territorial_Rights_DAO::get( $post->ID );
		$rights_type      = $rights_row ? $rights_row->rights_type : 'world';
		$rights_countries = $rights_row ? $rights_row->countries   : array();
		$contact_role     = (string) get_post_meta( $post->ID, 'mbs_product_contact_role',  true );
		$contact_name     = (string) get_post_meta( $post->ID, 'mbs_product_contact_name',  true );
		$contact_email    = (string) get_post_meta( $post->ID, 'mbs_product_contact_email', true );

		wp_nonce_field( 'mbs_onix_save', 'mbs_onix_nonce' );
		?>
		<div class="mbs-onix-wrap">

			<?php /* ---- Section A: Sales Territory ---- */ ?>
			<div class="mbs-onix-section">
				<h3 class="mbs-onix-section-heading">
					<?php esc_html_e( 'Sales Territory', 'my-book-showroom' ); ?>
				</h3>

				<fieldset class="mbs-onix-territory-fieldset">
					<legend class="screen-reader-text"><?php esc_html_e( 'Territory rights', 'my-book-showroom' ); ?></legend>

					<div class="mbs-onix-radio-group">

						<div class="mbs-onix-radio-option">
							<label class="mbs-onix-radio-main">
								<input type="radio" name="mbs_territory_type" value="world"
									<?php checked( $rights_type, 'world' ); ?>>
								<span class="mbs-onix-radio-text">
									<strong class="mbs-onix-radio-title"><?php esc_html_e( 'World rights', 'my-book-showroom' ); ?></strong>
									<span class="mbs-onix-radio-desc"><?php esc_html_e( 'No country restrictions — book available everywhere', 'my-book-showroom' ); ?></span>
								</span>
							</label>
						</div>

						<div class="mbs-onix-radio-option">
							<label class="mbs-onix-radio-main">
								<input type="radio" name="mbs_territory_type" value="include"
									<?php checked( $rights_type, 'include' ); ?>>
								<span class="mbs-onix-radio-text">
									<strong class="mbs-onix-radio-title"><?php esc_html_e( 'Specific countries — included', 'my-book-showroom' ); ?></strong>
									<span class="mbs-onix-radio-desc"><?php esc_html_e( 'Book is for sale only in the selected countries', 'my-book-showroom' ); ?></span>
								</span>
							</label>
						</div>

						<div class="mbs-onix-radio-option">
							<label class="mbs-onix-radio-main">
								<input type="radio" name="mbs_territory_type" value="exclude"
									<?php checked( $rights_type, 'exclude' ); ?>>
								<span class="mbs-onix-radio-text">
									<strong class="mbs-onix-radio-title"><?php esc_html_e( 'Specific countries — excluded', 'my-book-showroom' ); ?></strong>
									<span class="mbs-onix-radio-desc"><?php esc_html_e( 'Book is NOT for sale in the selected countries', 'my-book-showroom' ); ?></span>
								</span>
							</label>
						</div>

					</div><?php /* .mbs-onix-radio-group */ ?>

					<div id="mbs-territory-grid"
						class="mbs-onix-territory-grid<?php echo ( 'world' === $rights_type ) ? ' mbs-territory-hidden' : ''; ?>"
						aria-hidden="<?php echo ( 'world' === $rights_type ) ? 'true' : 'false'; ?>">

						<p class="mbs-territory-hint description">
							<?php
							if ( 'exclude' === $rights_type ) {
								esc_html_e( 'Check countries where this book is NOT for sale.', 'my-book-showroom' );
							} else {
								esc_html_e( 'Check countries where this book is for sale.', 'my-book-showroom' );
							}
							?>
						</p>

						<?php foreach ( self::COUNTRY_GROUPS as $region => $countries_in_group ) : ?>
							<div class="mbs-territory-group">
								<div class="mbs-territory-group-header">
									<strong class="mbs-territory-region-label"><?php echo esc_html( html_entity_decode( $region ) ); ?></strong>
									<button type="button" class="button-link mbs-territory-toggle" data-action="select"
										aria-label="<?php esc_attr_e( 'Select all in region', 'my-book-showroom' ); ?>">
										<?php esc_html_e( 'Select all', 'my-book-showroom' ); ?>
									</button>
									<span class="mbs-territory-sep"> / </span>
									<button type="button" class="button-link mbs-territory-toggle" data-action="clear"
										aria-label="<?php esc_attr_e( 'Clear all in region', 'my-book-showroom' ); ?>">
										<?php esc_html_e( 'Clear all', 'my-book-showroom' ); ?>
									</button>
								</div>
								<div class="mbs-territory-checkboxes">
									<?php foreach ( $countries_in_group as $code => $name ) : ?>
										<label class="mbs-territory-check-label">
											<input type="checkbox"
												name="mbs_territory_countries[]"
												value="<?php echo esc_attr( $code ); ?>"
												<?php checked( in_array( $code, $rights_countries, true ) ); ?>>
											<?php echo esc_html( $name ); ?>
										</label>
									<?php endforeach; ?>
								</div>
							</div>
						<?php endforeach; ?>

					</div><?php /* #mbs-territory-grid */ ?>

				</fieldset>
			</div><?php /* .mbs-onix-section -- Sales Territory */ ?>

			<?php /* ---- Section B: Product Contact ---- */ ?>
			<div class="mbs-onix-section">
				<h3 class="mbs-onix-section-heading">
					<?php esc_html_e( 'Product Contact', 'my-book-showroom' ); ?>
					<span class="mbs-onix-badge">ONIX 3.0 / 3.1</span>
				</h3>
				<p class="mbs-onix-section-desc description">
					<?php esc_html_e( 'Exported in the ONIX 3.0/3.1 ProductContact composite within PublishingDetail.', 'my-book-showroom' ); ?>
				</p>

				<div class="mbs-onix-fields">

					<div class="mbs-onix-field-row">
						<label for="mbs_product_contact_role" class="mbs-onix-field-label">
							<?php esc_html_e( 'Role', 'my-book-showroom' ); ?>
						</label>
						<div class="mbs-onix-field-control">
							<select id="mbs_product_contact_role" name="mbs_product_contact_role">
								<option value=""><?php esc_html_e( '--- select ---', 'my-book-showroom' ); ?></option>
								<?php foreach ( self::LIST_198_OPTIONS as $code => $label ) : ?>
									<option value="<?php echo esc_attr( $code ); ?>"
										<?php selected( $contact_role, $code ); ?>>
										<?php echo esc_html( $code . ' — ' . $label ); ?>
									</option>
								<?php endforeach; ?>
							</select>
							<span class="description"><?php esc_html_e( 'ONIX List 198', 'my-book-showroom' ); ?></span>
						</div>
					</div>

					<div class="mbs-onix-field-row">
						<label for="mbs_product_contact_name" class="mbs-onix-field-label">
							<?php esc_html_e( 'Name', 'my-book-showroom' ); ?>
						</label>
						<div class="mbs-onix-field-control">
							<input type="text" id="mbs_product_contact_name" name="mbs_product_contact_name"
								value="<?php echo esc_attr( $contact_name ); ?>"
								maxlength="200" class="regular-text">
						</div>
					</div>

					<div class="mbs-onix-field-row">
						<label for="mbs_product_contact_email" class="mbs-onix-field-label">
							<?php esc_html_e( 'Email', 'my-book-showroom' ); ?>
						</label>
						<div class="mbs-onix-field-control">
							<input type="email" id="mbs_product_contact_email" name="mbs_product_contact_email"
								value="<?php echo esc_attr( $contact_email ); ?>"
								maxlength="200" class="regular-text"
							       autocomplete="off">
						</div>
					</div>

				</div><?php /* .mbs-onix-fields */ ?>
			</div><?php /* .mbs-onix-section -- Product Contact */ ?>

		</div><?php /* .mbs-onix-wrap */
	}
}
