<?php
/**
 * File Path: includes/admin/tabs/class-mbs-tab-license.php
 *
 * License Tab (Parent Orchestrator)
 *
 * Thin parent that coordinates the two child classes for this tab.
 * All business logic lives in the child classes:
 *
 *   MBS_Tab_License_Ajax   — four wp_ajax_* and admin_post_* handlers
 *   MBS_Tab_License_Render — render() with all six UI states
 *
 * @package    My_Book_Showroom
 * @since      1.0.0
 */

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

class MBS_Tab_License {

	/** @var MBS_Tab_License_Ajax */
	private MBS_Tab_License_Ajax $ajax;

	public function __construct() {
		$this->ajax     = new MBS_Tab_License_Ajax();
		$this->register();
	}

	/**
	 * Register AJAX and admin-post action hooks.
	 *
	 * Called at boot-time by MBS_Settings::get_tab_objects() so hooks fire on
	 * every admin request, not only when the license tab is being rendered.
	 * Delegates to the AJAX child — the renderer has no hooks to register.
	 *
	 */
	public function register(): void {
		// S-05: All wp_ajax_* and admin_post_* hook registrations are
		// delegated to MBS_Tab_License_Ajax::register_hooks().
		// Nonce verification is performed inside each handler in that
		// class — not here. See class-mbs-tab-license-ajax.php.
		$this->ajax->register_hooks();
	}

	/** Enqueue License tab assets when this tab is active. */
	public function enqueue( string $current_tab ): void {
		if ( 'license' !== $current_tab ) {
			return;
		}

		wp_enqueue_style(
			'mbs-admin-settings-license',
			plugins_url( 'assets/css/mbs-admin-settings-license.css', MBS_DIR . 'bookshowroom.php' ),
			array( 'mbs-admin-settings' ),
			MBS_VERSION
		);

		wp_enqueue_script(
			'mbs-admin-license',
			plugins_url( 'assets/js/adminjs/admin-license.js', MBS_DIR . 'bookshowroom.php' ),
			array( 'jquery' , 'wp-i18n' ),
			MBS_VERSION,
			true
		);
		wp_set_script_translations( 'mbs-admin-license', 'my-book-showroom', MBS_DIR . 'languages' );

		wp_localize_script( 'mbs-admin-license', 'MBS_License_Ajax', array(
			'nonce'             => wp_create_nonce( 'mbs_license_ajax' ),
		) );
	}

	/**
	 * Render the License tab.
	 *
	 * Delegates to MBS_Tab_License_Render::render().
	 *
	 */
	public function render(): void {
		MBS_Tab_License_Render::render();
	}
}
