<?php
/**
 * File Path: includes/taxonomies/class-mbs-taxonomy-imprint.php
 *
 * Imprint Taxonomy
 *
 * Flat (non-hierarchical) taxonomy for publisher imprint names. Each imprint
 * stores its parent publisher ID in term_meta (mbs_publisher_id). Book
 * assignment is via MBS_Metabox_Publisher_Imprint (Phase 4). Phase 4 wires
 * the custom term form fields for publisher association.
 *
 * @package    My_Book_Showroom
 * @subpackage Includes/Taxonomies
 * @since      1.1.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_Taxonomy_Imprint {

	public static function register(): void {
		$labels = array(
			'name'          => __( 'Imprints', 'my-book-showroom' ),
			'singular_name' => __( 'Imprint', 'my-book-showroom' ),
			'menu_name'     => __( 'Imprints', 'my-book-showroom' ),
			'search_items'  => __( 'Search Imprints', 'my-book-showroom' ),
			'all_items'     => __( 'All Imprints', 'my-book-showroom' ),
			'edit_item'     => __( 'Edit Imprint', 'my-book-showroom' ),
			'update_item'   => __( 'Update Imprint', 'my-book-showroom' ),
			'add_new_item'  => __( 'Add New Imprint', 'my-book-showroom' ),
			'new_item_name' => __( 'New Imprint Name', 'my-book-showroom' ),
			'not_found'     => __( 'No imprints found.', 'my-book-showroom' ),
		);

		register_taxonomy(
			MBS_TAX_IMPRINT,
			MBS_POST_TYPE,
			array(
				'labels'            => $labels,
				'hierarchical'      => false,
				'public'            => false,
				'show_ui'           => true,
				'show_in_rest'      => false,
				'show_admin_column' => false,
				'show_in_menu'      => false,
				'show_in_quick_edit' => false,
			)
		);

		self::register_term_meta_hooks();
	}

	/**
	 * Register term_meta field hooks for mbs_publisher_id.
	 * Called from register() so hooks are set on 'init'.
	 */
	public static function register_term_meta_hooks(): void {
		add_action( MBS_TAX_IMPRINT . '_add_form_fields',  array( __CLASS__, 'add_form_fields' ) );
		add_action( MBS_TAX_IMPRINT . '_edit_form_fields', array( __CLASS__, 'edit_form_fields' ), 10, 2 );
		add_action( 'created_' . MBS_TAX_IMPRINT,         array( __CLASS__, 'save_term_meta' ) );
		add_action( 'edited_'  . MBS_TAX_IMPRINT,         array( __CLASS__, 'save_term_meta' ) );
	}

	/**
	 * Publisher select field on the Add New Imprint form.
	 */
	public static function add_form_fields(): void {
		?>
		<div class="form-field">
			<label for="mbs_publisher_id">
				<?php esc_html_e( 'Publisher', 'my-book-showroom' ); ?>
			</label>
			<?php self::publisher_select( 0 ); ?>
			<p><?php esc_html_e( 'Associate this imprint with a publisher.', 'my-book-showroom' ); ?></p>
		</div>
		<?php
	}

	/**
	 * Publisher select field on the Edit Imprint form.
	 */
	public static function edit_form_fields( WP_Term $term ): void {
		$saved = (int) get_term_meta( $term->term_id, 'mbs_publisher_id', true );
		?>
		<tr class="form-field">
			<th scope="row">
				<label for="mbs_publisher_id">
					<?php esc_html_e( 'Publisher', 'my-book-showroom' ); ?>
				</label>
			</th>
			<td>
				<?php self::publisher_select( $saved ); ?>
				<p class="description"><?php esc_html_e( 'Associate this imprint with a publisher.', 'my-book-showroom' ); ?></p>
			</td>
		</tr>
		<?php
	}

	/**
	 * Save mbs_publisher_id term_meta.
	 */
	public static function save_term_meta( int $term_id ): void {
		if ( isset( $_POST['mbs_publisher_id'] ) ) {
			$pub_id = absint( wp_unslash( $_POST['mbs_publisher_id'] ) );
			if ( $pub_id > 0 ) {
				update_term_meta( $term_id, 'mbs_publisher_id', $pub_id );
			} else {
				delete_term_meta( $term_id, 'mbs_publisher_id' );
			}
		}
	}

	/**
	 * Render a publisher <select> populated from mbs_publisher terms.
	 */
	private static function publisher_select( int $selected_id ): void {
		$terms = get_terms( array(
			'taxonomy'   => MBS_TAX_PUBLISHER,
			'hide_empty' => false,
		) );
		?>
		<select id="mbs_publisher_id" name="mbs_publisher_id">
			<option value="0"><?php esc_html_e( '-- None --', 'my-book-showroom' ); ?></option>
			<?php if ( ! is_wp_error( $terms ) && ! empty( $terms ) ) : ?>
				<?php foreach ( $terms as $term ) : ?>
					<option value="<?php echo esc_attr( $term->term_id ); ?>"
						<?php selected( $selected_id, $term->term_id ); ?>>
						<?php echo esc_html( $term->name ); ?>
					</option>
				<?php endforeach; ?>
			<?php endif; ?>
		</select>
		<?php
	}
}
