<?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars
/**
 * File Path: templates/admin/universe-card.php
 *
 * Admin Template -- Single Universe Card
 *
 * Renders the collapsible <details>/<summary> card for one universe term on
 * the Settings -> Universes tab. Wraps the card in its own <form> so each
 * universe saves independently via the PRG pattern in MBS_Tab_Universes.
 *
 * Expected variables (passed via mbs_get_template_part() $args):
 *   WP_Term  $universe         The universe taxonomy term.
 *   int      $term_id          Universe term ID.
 *   int      $book_count       Number of books assigned to this universe.
 *   string   $description      Universe description (plain text).
 *   bool     $is_open          Whether this card should start expanded.
 *   string   $edit_url         URL to the WordPress term-edit screen for this universe.
 *   array    $book_ids         Ordered array of book post IDs.
 *   array    $book_display_map Keyed by book ID; each entry has 'title', 'thumb'.
 *   bool     $has_persisted    Whether a custom reading order has been saved.
 *   string   $meta_line        Pre-formatted summary line for the card header.
 *   string   $term_layout          Saved per-term layout slug ('' = use global default).
   string   $universe_display_mode Per-universe display mode ('' | 'order' | 'reading_order' | 'both').
 *   int      $banner_id        Universe banner attachment ID (0 when not set).
 *   string   $banner_url       URL of the banner preview image ('' when not set).
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

$universe_name_lower = function_exists( 'mb_strtolower' )
	? mb_strtolower( $universe->name )
	: strtolower( $universe->name );
?>
<form method="post" action="" class="mbs-universe-card-form mbs-entity-card-form" data-name="<?php echo esc_attr( $universe_name_lower ); ?>">
	<details class="mbs-settings-card mbs-universe-card mbs-entity-card"
	         data-term-id="<?php echo esc_attr( $term_id ); ?>"<?php echo $is_open ? ' open' : ''; ?>>
		<summary class="mbs-universe-card-summary mbs-entity-card-summary">
			<span class="mbs-universe-card-heading mbs-entity-card-heading">
				<span class="mbs-universe-card-name mbs-entity-card-name"><?php echo esc_html( $universe->name ); ?></span>
				<span class="mbs-universe-card-meta mbs-entity-card-meta"><?php echo esc_html( $meta_line ); ?></span>
			</span>
		</summary>

		<div class="mbs-universe-card-body mbs-entity-card-body">

			<div class="mbs-entity-card-nav-links">
				<a class="mbs-universe-card-edit-term mbs-entity-card-edit-link" href="<?php echo esc_url( $edit_url ); ?>">
					<?php esc_html_e( 'Edit slug in WordPress →', 'my-book-showroom' ); ?>
				</a>
				<?php
				$_mbs_view_url = get_term_link( $universe );
				if ( ! is_wp_error( $_mbs_view_url ) ) :
				?>
				<a class="mbs-universe-card-view-term mbs-entity-card-view-link button button-secondary button-small"
				   href="<?php echo esc_url( $_mbs_view_url ); ?>"
				   target="_blank"
				   rel="noopener noreferrer">
					<?php esc_html_e( 'View Universe →', 'my-book-showroom' ); ?>
				</a>
				<?php endif; ?>
			</div>

			<?php wp_nonce_field( 'mbs_universe_save', 'mbs_universe_nonce' ); ?>
			<input type="hidden" name="mbs_save_universe_id" value="<?php echo esc_attr( $term_id ); ?>">

			<div class="mbs-settings-field-cell mbs-universe-field-wide">
				<label for="mbs-universe-description-<?php echo esc_attr( $term_id ); ?>">
					<?php esc_html_e( 'Description', 'my-book-showroom' ); ?>
				</label>
				<textarea id="mbs-universe-description-<?php echo esc_attr( $term_id ); ?>"
				          name="mbs_universe_description[<?php echo esc_attr( $term_id ); ?>]"
				          class="mbs-universe-description-field"
				          rows="3"
				          placeholder="<?php esc_attr_e( 'Short universe description…', 'my-book-showroom' ); ?>"><?php echo esc_textarea( $description ); ?></textarea>
			</div>
			<!-- Universe Banner Image (F-BNR-U) -->
			<div class="mbs-settings-field-cell mbs-universe-field-banner">
				<label><?php esc_html_e( 'Universe Banner Image', 'my-book-showroom' ); ?></label>
				<?php if ( mbs_is_pro() || mbs_is_pub() ) : ?>
				<div class="mbs-universe-banner-wrap">
					<img id="mbs-universe-banner-preview-<?php echo esc_attr( $term_id ); ?>"
					     class="mbs-universe-banner-img"
					     src="<?php echo esc_url( $banner_url ); ?>"
					     alt=""
					     <?php echo $banner_url ? '' : 'hidden'; ?>>
					<input type="hidden"
					       id="mbs-universe-banner-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_universe_banner_id[<?php echo esc_attr( $term_id ); ?>]"
					       value="<?php echo esc_attr( $banner_id ?: '' ); ?>">
					<div class="mbs-universe-banner-controls">
						<button type="button"
						        class="button mbs-universe-banner-select"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-universe-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-universe-banner-input-<?php echo esc_attr( $term_id ); ?>">
							<?php echo $banner_url
								? esc_html__( 'Change', 'my-book-showroom' )
								: esc_html__( 'Add Banner', 'my-book-showroom' ); ?>
						</button>
						<button type="button"
						        class="button mbs-universe-banner-remove"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-universe-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-universe-banner-input-<?php echo esc_attr( $term_id ); ?>"
						        <?php echo $banner_url ? '' : 'hidden'; ?>>
							<?php esc_html_e( 'Remove', 'my-book-showroom' ); ?>
						</button>
					</div>
					<p id="mbs-universe-banner-hint-<?php echo esc_attr( $term_id ); ?>"
					   class="description mbs-universe-banner-hint"
					   hidden>
						<?php esc_html_e( 'Image selected — click Save below to persist it.', 'my-book-showroom' ); ?>
					</p>
					<p class="description">
						<?php esc_html_e( 'Displayed above the book grid on the universe archive page. Recommended: 1200 x 320 px (4:1 ratio). Taller images are cropped to 320 px on desktop, 180 px on mobile.', 'my-book-showroom' ); ?>
					</p>
				</div>
				<?php else : ?>
				<p class="description">
					<?php esc_html_e( 'Upgrade to PRO to display a full-width banner image above the book grid on the universe archive page. Recommended: 1200 x 320 px (4:1 ratio).', 'my-book-showroom' ); ?>
				</p>
				<?php endif; ?>
			</div>

<?php mbs_get_template_part( 'admin/partials/layout-picker', array(
				'term_id'     => $term_id,
				'term_layout' => $term_layout,
				'desc_text'   => __( 'Layout for this universe\'s book grid. Grid is used when nothing is selected.', 'my-book-showroom' ),
			) ); ?>

			<!-- Sprint RO-Display 4d: per-universe archive display mode -->
			<div class="mbs-universe-display-mode-cell">
				<?php $_mbs_u_ro_enabled = '1' === get_option( 'mbs_setting_show_universe_reading_order', '0' ); ?>
				<label for="mbs-universe-display-mode-<?php echo esc_attr( $term_id ); ?>">
					<strong><?php esc_html_e( 'Archive Display Mode', 'my-book-showroom' ); ?></strong>
				</label>
				<select id="mbs-universe-display-mode-<?php echo esc_attr( $term_id ); ?>"
				        name="mbs_universe_archive_display_mode[<?php echo esc_attr( $term_id ); ?>]">
					<option value="" <?php selected( '', $universe_display_mode ); ?>><?php esc_html_e( 'Use Global Setting', 'my-book-showroom' ); ?></option>
					<option value="order" <?php selected( 'order', $universe_display_mode ); ?>><?php esc_html_e( 'Series Order only', 'my-book-showroom' ); ?></option>
					<option value="reading_order" <?php selected( 'reading_order', $universe_display_mode ); ?>><?php esc_html_e( 'Reading Order only', 'my-book-showroom' ); ?></option>
					<option value="both" <?php selected( 'both', $universe_display_mode ); ?>><?php esc_html_e( 'Both (Series Order + Reading Order)', 'my-book-showroom' ); ?></option>
				</select>
				<?php if ( ! $_mbs_u_ro_enabled ) : ?>
				<p class="description">
					<?php esc_html_e( 'Enable Universe Reading Order in Settings -> Advanced to use Reading Order and Both options.', 'my-book-showroom' ); ?>
				</p>
				<?php endif; ?>
			</div>

			<section class="mbs-entity-reading-order-section">
				<h4><?php esc_html_e( 'Reading Order', 'my-book-showroom' ); ?></h4>
				<p class="description">
					<?php if ( empty( $book_display_map ) ) : ?>
						<?php esc_html_e( 'No books are assigned to this universe yet. Assign books to this universe on the book editor, then return here to set the reading order.', 'my-book-showroom' ); ?>
					<?php elseif ( $has_persisted ) : ?>
						<?php esc_html_e( 'Drag to reorder the books in the recommended reading sequence for this universe.', 'my-book-showroom' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'Showing the default order (alphabetical by title). Drag to customize and click Save to lock in a custom reading order.', 'my-book-showroom' ); ?>
					<?php endif; ?>
				</p>

				<?php if ( ! empty( $book_display_map ) ) : ?>
					<ul id="mbs-universe-reading-order-list-<?php echo esc_attr( $term_id ); ?>"
					    class="mbs-universe-book-list mbs-book-order-list">
						<?php foreach ( $book_display_map as $book_id => $book ) : ?>
							<li data-book-id="<?php echo esc_attr( $book_id ); ?>"
							    class="mbs-universe-book-item mbs-book-order-item">
								<?php MBS_Sortable_UI::drag_handle( true ); ?>
								<?php MBS_Sortable_UI::reorder_buttons(
									__( 'Move up', 'my-book-showroom' ),
									__( 'Move down', 'my-book-showroom' )
								); ?>
								<?php if ( $book['thumb'] ) : ?>
									<img class="mbs-universe-book-thumb mbs-book-order-thumb"
									     src="<?php echo esc_url( $book['thumb'] ); ?>"
									     alt="">
								<?php else : ?>
									<span class="mbs-universe-book-thumb-placeholder mbs-book-order-thumb-placeholder" aria-hidden="true"></span>
								<?php endif; ?>
								<span class="mbs-universe-book-title mbs-book-order-title">
									<?php echo esc_html( $book['title'] ); ?>
								</span>
							</li>
						<?php endforeach; ?>
					</ul>

					<input type="hidden"
					       id="mbs-universe-reading-order-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_universe_reading_order[<?php echo esc_attr( $term_id ); ?>]"
					       value="<?php echo esc_attr( wp_json_encode( $book_ids ) ); ?>">

				<?php endif; ?>
			</section>

			<p class="mbs-settings-actions">
				<?php submit_button( __( 'Save', 'my-book-showroom' ), 'primary', 'submit', false ); ?>
			</p>

		</div>
	</details>
</form>
