<?php
/**
 * Admin Menu Manager Service
 *
 * Handles WordPress admin menu registration and page rendering.
 *
 * @package AIPT\Core\Services
 * @since 2.4.0
 */

namespace AIPT\Core\Services;

use AIPT\Core\Init;

/**
 * Manages admin menu registration and rendering
 *
 * @internal Used by Init
 */
class AdminMenuManager {

	/**
	 * Init instance
	 *
	 * @var Init
	 */
	private $init;

	/**
	 * Cached menu icon
	 *
	 * @var string|null
	 */
	private $icon_cache = null;

	/**
	 * Constructor
	 *
	 * @param Init $init Init instance.
	 */
	public function __construct( Init $init ) {
		$this->init = $init;
	}

	/**
	 * Register admin menu pages
	 *
	 * Creates main plugin menu and all submenu pages.
	 */
	public function registerMenus(): void {
		$icon_base64 = $this->getCachedIcon();

		add_menu_page(
			__( 'AI Product Tools', 'ai-product-tools' ),
			__( 'AI Product Tools', 'ai-product-tools' ),
			'manage_options',
			'ai-product-tools',
			array( $this, 'renderToolkitPage' ),
			$icon_base64,
			56
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Toolkit', 'ai-product-tools' ),
			__( 'Toolkit', 'ai-product-tools' ),
			'manage_options',
			'ai-product-tools',
			array( $this, 'renderToolkitPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'AI Automation Jobs', 'ai-product-tools' ),
			__( 'AI Automation Jobs', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-automation-jobs',
			array( $this, 'renderAutomationJobsPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'AI Store Index', 'ai-product-tools' ),
			__( 'AI Store Index', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-store-index',
			array( $this, 'renderStoreIndexPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'AI Shop Assistant', 'ai-product-tools' ),
			__( 'AI Shop Assistant', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-chatbot',
			array( $this, 'renderChatbotPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Standard Bulk Generator', 'ai-product-tools' ),
			__( 'Standard Bulk Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-standard-bulk-generator',
			array( $this, 'renderStandardBulkGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Advanced Bulk Generator', 'ai-product-tools' ),
			__( 'Advanced Bulk Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-advanced-bulk-generator',
			array( $this, 'renderBulkGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Bulk FAQ Generator', 'ai-product-tools' ),
			__( 'Bulk FAQ Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-faq-generator',
			array( $this, 'renderFaqGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Bulk Taxonomy Generator', 'ai-product-tools' ),
			__( 'Bulk Taxonomy Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-taxonomy-generator',
			array( $this, 'renderTaxonomyGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Bulk Title Generator', 'ai-product-tools' ),
			__( 'Bulk Title Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-title-generator',
			array( $this, 'renderTitleGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Bulk SEO Meta Generator', 'ai-product-tools' ),
			__( 'Bulk SEO Meta Generator', 'ai-product-tools' ),
			'manage_woocommerce',
			'aipt-seo-meta-generator',
			array( $this, 'renderSeoMetaGeneratorPage' )
		);

		add_submenu_page(
			'ai-product-tools',
			__( 'Settings', 'ai-product-tools' ),
			__( 'Settings', 'ai-product-tools' ),
			'manage_options',
			'aipt-settings',
			array( $this, 'renderSettingsPage' )
		);
	}

	/**
	 * Get cached SVG icon
	 *
	 * Loads and caches the plugin menu icon.
	 *
	 * @return string Base64-encoded SVG or dashicon fallback.
	 */
	private function getCachedIcon(): string {
		if ( $this->icon_cache === null ) {
			require_once ABSPATH . 'wp-admin/includes/file.php';
			WP_Filesystem();
			global $wp_filesystem;

			$icon_svg = '';
			if ( $wp_filesystem ) {
				$icon_svg = $wp_filesystem->get_contents( AIPT_PLUGIN_DIR . 'dist/img/aipt-icon.svg' );
			}

			$this->icon_cache = $icon_svg ? 'data:image/svg+xml;base64,' . base64_encode( $icon_svg ) : 'dashicons-admin-generic';
		}
		return $this->icon_cache;
	}

	/**
	 * Render Toolkit page
	 */
	public function renderToolkitPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-body-container" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Settings page
	 */
	public function renderSettingsPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-settings-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Advanced Bulk Generator page
	 */
	public function renderBulkGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-body-container" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Standard Bulk Generator page
	 */
	public function renderStandardBulkGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-body-container" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render FAQ Generator page
	 */
	public function renderFaqGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-faq-generator-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Title Generator page
	 */
	public function renderTitleGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-body-container" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Automation Jobs page
	 */
	public function renderAutomationJobsPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-automation-jobs-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render AI Store Index page
	 */
	public function renderStoreIndexPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-store-index-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render AI Shop Assistant page
	 */
	public function renderChatbotPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-chatbot-admin-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Bulk SEO Meta Generator page
	 */
	public function renderSeoMetaGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-seo-meta-generator-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}

	/**
	 * Render Taxonomy Generator page
	 */
	public function renderTaxonomyGeneratorPage(): void {
		echo '<div class="wrap">';
		echo '<div id="aipt-taxonomy-generator-root" class="aipt-ui-shell"></div>';
		echo '</div>';
	}
}
