<?php
/**
 * File Path: includes/taxonomies/class-mbs-taxonomy-series.php
 *
 * Series Taxonomy
 *
 * @package MyBookShowroom
 * @since   1.0.0
 */

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

class MBS_Taxonomy_Series {
	use MBS_Taxonomy_Layout_Field_Trait;

	/**
	 * Return the taxonomy constant for the layout field trait.
	 *
	 * @return string
	 */
	protected static function layout_taxonomy_key(): string {
		return MBS_TAX_SERIES;
	}

	public static function register(): void {
		$labels = array(
			'name'              => __( 'Series', 'my-book-showroom' ),
			'singular_name'     => __( 'Series', 'my-book-showroom' ),
			'menu_name'         => __( 'Series', 'my-book-showroom' ),
			'all_items'         => __( 'All Series', 'my-book-showroom' ),
			'add_new_item'      => __( 'Add New Series', 'my-book-showroom' ),
			'new_item_name'     => __( 'New Series Name', 'my-book-showroom' ),
			// Screen title on the native WordPress term-edit page (term.php).
			// Without this, 'Edit Category' appears (hierarchical default).
			'edit_item'         => __( 'Edit Series Slug', 'my-book-showroom' ),
			'update_item'       => __( 'Update Series', 'my-book-showroom' ),
			'search_items'      => __( 'Search Series', 'my-book-showroom' ),
			'not_found'         => __( 'No series found.', 'my-book-showroom' ),
			'no_terms'          => __( 'No series', 'my-book-showroom' ),
		);

		register_taxonomy(
			MBS_TAX_SERIES,
			MBS_POST_TYPE,
			array(
				'labels'            => $labels,
				'hierarchical'      => false,
				'public'            => true,
				'show_ui'           => true,
				'show_in_rest'      => true,
				'show_admin_column' => true,
				// MENU-01: Prevent menu.php from auto-attaching to edit.php?post_type=mbs_book.
				// MBS_Admin::add_menu() registers the Series submenu manually.
				'show_in_menu'          => false,
				'show_in_quick_edit'    => false,
				// RF-013: Count all non-trashed statuses, not just published (latent bug fix).
				'update_count_callback' => array( __CLASS__, 'update_term_count_all' ),
			)
		);

		// PLG-055: Suppress the default tag-style widget WordPress auto-registers for
		// non-hierarchical taxonomies on the book editor. MBS_Book_Header_Core renders
		// a single-select <select> dropdown instead, enforcing the 0:1 cardinality rule.
		add_action( 'add_meta_boxes', array( __CLASS__, 'remove_default_meta_box' ), 99 );
		if ( is_admin() ) {
			add_action( MBS_TAX_SERIES . '_edit_form_fields', array( __CLASS__, 'render_settings_notice' ),  5, 2 );
			add_action( MBS_TAX_SERIES . '_edit_form_fields', array( __CLASS__, 'render_layout_field' ), 20, 2 );
			add_action( 'edited_' . MBS_TAX_SERIES,           array( __CLASS__, 'save_layout_field' ) );
			add_action( 'admin_enqueue_scripts',               array( __CLASS__, 'enqueue_layout_scripts' ) );
		}
	}

	/**
	 * Render a navigation notice on the native WordPress Edit Term screen.
	 *
	 * Points editors to Settings → Series for the full rich editing UI.
	 * Delegates HTML rendering to MBS_Taxonomy_Settings_Notice::render().
	 *
	 * @param WP_Term $term Current term object.
	 * @return void
	 * @since 1.0.0
	 */
	public static function render_settings_notice( WP_Term $term ): void {
		MBS_Taxonomy_Settings_Notice::render(
			$term,
			'series',
			/* translators: Button label linking to the series front-end page. */
			__( 'View Series →', 'my-book-showroom' ),
			/* translators: %s: link to My Book Showroom → Settings → Series */
			__( 'To edit the description, banner, reading order, and other details, go to %s. This screen manages the slug only.', 'my-book-showroom' ),
			__( 'My Book Showroom → Settings → Series', 'my-book-showroom' )
		);
	}
	public static function remove_default_meta_box(): void {
		remove_meta_box( 'tagsdiv-' . MBS_TAX_SERIES, MBS_POST_TYPE, 'side' );
	}
	/**
	 * Term count callback: count all non-trashed post statuses.
	 *
	 * WordPress core's _update_post_term_count() only counts published posts.
	 * This callback counts every non-trashed status so term counts stay accurate
	 * during active editing (drafts, private, pending, future).
	 *
	 * Registered via 'update_count_callback' on all four MBS taxonomies
	 * (RF-013: fixes the latent bug where genre/series/universe showed 0 for
	 * non-published books).
	 *
	 * @param int[]       $terms    Array of term taxonomy IDs to update.
	 * @param WP_Taxonomy $taxonomy Taxonomy object (unused; required by WP).
	 * @return void
	 */
	public static function update_term_count_all( array $terms, WP_Taxonomy $taxonomy ): void {
		foreach ( $terms as $ttid ) {
			MBS_Taxonomy_Count_DAO::update_term_book_count( (int) $ttid );
		}
	}

}
