<?php
/**
 * File Path: templates/admin/collection-card.php
 *
 * Admin Template — Single Collection Card
 *
 * Renders the collapsible <details>/<summary> card for one collection term on
 * the Settings -> Collections tab. Wraps the card in its own <form> so each
 * collection saves independently via the PRG pattern in MBS_Tab_Collections.
 *
 * Expected variables (passed via mbs_get_template_part() $args):
 *   WP_Term  $collection       The collection taxonomy term.
 *   int      $term_id          Collection term ID.
 *   int      $book_count       Number of books assigned to this collection.
 *   string   $description      Collection description (plain text).
 *   string   $collection_type  Current type slug (key from MBS_Taxonomy_Collection::get_types()).
 *   bool     $is_open          Whether this card should start expanded.
 *   string   $edit_url         URL to the WordPress term-edit screen for this collection.
 *   array    $book_ids         Ordered array of book post IDs.
 *   array    $book_display_map Keyed by book ID; each entry has 'title', 'position', '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).
 *   int      $banner_id        Collection 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;
}
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- template-scope variables injected by mbs_get_template_part(); not global vars

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

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

			<div class="mbs-entity-card-nav-links">
				<a class="mbs-collection-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( $collection );
				if ( ! is_wp_error( $_mbs_view_url ) ) :
				?>
				<a class="mbs-collection-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 Collection →', 'my-book-showroom' ); ?>
				</a>
				<?php endif; ?>
			</div>

			<?php wp_nonce_field( 'mbs_collection_save', 'mbs_collection_nonce' ); ?>
			<input type="hidden" name="mbs_save_collection_id" value="<?php echo esc_attr( $term_id ); ?>">

			<div class="mbs-settings-field-cell mbs-collection-field-wide">
				<label for="mbs-collection-description-<?php echo esc_attr( $term_id ); ?>">
					<?php esc_html_e( 'Description', 'my-book-showroom' ); ?>
				</label>
				<textarea id="mbs-collection-description-<?php echo esc_attr( $term_id ); ?>"
				          name="mbs_collection_description[<?php echo esc_attr( $term_id ); ?>]"
				          class="mbs-collection-description-field"
				          rows="3"
				          placeholder="<?php esc_attr_e( 'Short collection description…', 'my-book-showroom' ); ?>"><?php echo esc_textarea( $description ); ?></textarea>
			</div>
			<!-- Collection Banner Image (F-BNR-C) -->
			<div class="mbs-settings-field-cell mbs-collection-field-banner">
				<label><?php esc_html_e( 'Collection Banner Image', 'my-book-showroom' ); ?></label>
				<?php if ( mbs_is_pro() || mbs_is_pub() ) : ?>
				<div class="mbs-collection-banner-wrap">
					<img id="mbs-collection-banner-preview-<?php echo esc_attr( $term_id ); ?>"
					     class="mbs-collection-banner-img"
					     src="<?php echo esc_url( $banner_url ); ?>"
					     alt=""
					     <?php echo $banner_url ? '' : 'hidden'; ?>>
					<input type="hidden"
					       id="mbs-collection-banner-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_collection_banner_id[<?php echo esc_attr( $term_id ); ?>]"
					       value="<?php echo esc_attr( $banner_id ?: '' ); ?>">
					<div class="mbs-collection-banner-controls">
						<button type="button"
						        class="button mbs-collection-banner-select"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-collection-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-collection-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-collection-banner-remove"
						        data-term-id="<?php echo esc_attr( $term_id ); ?>"
						        data-preview="mbs-collection-banner-preview-<?php echo esc_attr( $term_id ); ?>"
						        data-input="mbs-collection-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-collection-banner-hint-<?php echo esc_attr( $term_id ); ?>"
					   class="description mbs-collection-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 collection 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 collection archive page. Recommended: 1200 x 320 px (4:1 ratio).', 'my-book-showroom' ); ?>
				</p>
				<?php endif; ?>
			</div>

			<div class="mbs-collection-type-cell">
				<label for="mbs-collection-type-<?php echo esc_attr( $term_id ); ?>">
					<?php esc_html_e( 'Collection Type', 'my-book-showroom' ); ?>
				</label>
				<select id="mbs-collection-type-<?php echo esc_attr( $term_id ); ?>"
				        name="mbs_collection_type_select[<?php echo esc_attr( $term_id ); ?>]"
				        class="mbs-collection-type-select">
					<?php foreach ( MBS_Taxonomy_Collection::get_types() as $value => $label ) : ?>
						<option value="<?php echo esc_attr( $value ); ?>"<?php selected( $collection_type, $value ); ?>>
							<?php echo esc_html( $label ); ?>
						</option>
					<?php endforeach; ?>
				</select>
				<p class="description">
					<?php esc_html_e( 'Describes the kind of grouping (e.g. Box Set, Anthology, Bundle). Optional.', 'my-book-showroom' ); ?>
				</p>
			</div>

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

			<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 collection yet. Assign books to this collection 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 collection.', 'my-book-showroom' ); ?>
					<?php else : ?>
						<?php esc_html_e( 'Showing the default order (by collection position). 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-collection-reading-order-list-<?php echo esc_attr( $term_id ); ?>"
					    class="mbs-collection-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-collection-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-collection-book-thumb mbs-book-order-thumb"
									     src="<?php echo esc_url( $book['thumb'] ); ?>"
									     alt="">
								<?php else : ?>
									<span class="mbs-collection-book-thumb-placeholder mbs-book-order-thumb-placeholder" aria-hidden="true"></span>
								<?php endif; ?>
								<span class="mbs-collection-book-title mbs-book-order-title">
									<?php echo esc_html( $book['title'] ); ?>
								</span>
								<?php if ( null !== $book['position'] ) : ?>
									<span class="mbs-collection-book-position mbs-book-order-badge">
										<?php echo esc_html( '#' . $book['position'] ); ?>
									</span>
								<?php endif; ?>
							</li>
						<?php endforeach; ?>
					</ul>

					<input type="hidden"
					       id="mbs-collection-reading-order-input-<?php echo esc_attr( $term_id ); ?>"
					       name="mbs_collection_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>
