<?php
/**
 * Menu
 *
 * Registers the top-level admin menu and submenus.
 *
 * @package Relatedly\Admin
 * @since   2.0.0
 */

namespace Relatedly\Admin;

use Relatedly\Core\Options;
use Relatedly\Core\Cache;
use Relatedly\Admin\Settings\Page as SettingsPage;
use Relatedly\Admin\Settings\SaveHandler;
use Relatedly\Admin\Statistics\Page as StatsPage;
use Relatedly\Admin\Statistics\ExportHandler;
use Relatedly\Admin\Ajax\ThemePreview;
use Relatedly\Admin\Ajax\StatisticsData;
use Relatedly\Admin\Ajax\PostAnalytics;
use Relatedly\Admin\Ajax\ComparisonData;
use Relatedly\Admin\Ajax\Dismissal;
use Relatedly\Theming\ThemeRegistry;

defined( 'ABSPATH' ) || exit;

/**
 * Registers the Relatedly admin menu and sub-handlers.
 *
 * @since 2.0.0
 */
class Menu {

	/**
	 * Plugin settings repository.
	 *
	 * @since 2.0.0
	 * @var Options
	 */
	private Options $options;

	/**
	 * Cache service.
	 *
	 * @since 2.0.0
	 * @var Cache
	 */
	private Cache $cache;

	/**
	 * Theme renderer registry.
	 *
	 * @since 2.0.0
	 * @var ThemeRegistry
	 */
	private ThemeRegistry $themes;

	/**
	 * Create the admin menu service.
	 *
	 * @since 2.0.0
	 * @param Options       $options Plugin settings repository.
	 * @param Cache         $cache   Cache service.
	 * @param ThemeRegistry $themes  Theme renderer registry.
	 */
	public function __construct( Options $options, Cache $cache, ThemeRegistry $themes ) {
		$this->options = $options;
		$this->cache   = $cache;
		$this->themes  = $themes;
	}

	/**
	 * Register menu and admin sub-handler hooks.
	 *
	 * @since 2.0.0
	 */
	public function init_hooks(): void {
		add_action( 'admin_menu', [ $this, 'register_menus' ] );

		// Wire all admin sub-handlers.
		( new SaveHandler( $this->options, $this->cache ) )->init_hooks();
		( new ExportHandler( $this->cache ) )->init_hooks();
		( new ThemePreview( $this->options, $this->themes ) )->init_hooks();
		( new StatisticsData( $this->cache ) )->init_hooks();
		( new PostAnalytics( $this->cache ) )->init_hooks();
		( new ComparisonData( $this->cache ) )->init_hooks();
		( new Dismissal() )->init_hooks();
		( new Settings\Page( $this->options ) )->init_hooks();
	}

	/**
	 * Register Relatedly menu pages.
	 *
	 * @since 2.0.0
	 */
	public function register_menus(): void {
		$icon = $this->get_menu_icon();

		add_menu_page(
			__( 'Relatedly', 'relatedly' ),
			__( 'Relatedly', 'relatedly' ),
			'manage_options',
			'relatedly',
			[ $this, 'render_settings' ],
			$icon,
			30
		);

		add_submenu_page(
			'relatedly',
			__( 'Settings', 'relatedly' ),
			__( 'Settings', 'relatedly' ),
			'manage_options',
			'relatedly',
			[ $this, 'render_settings' ]
		);

		add_submenu_page(
			'relatedly',
			__( 'Statistics', 'relatedly' ),
			__( 'Statistics', 'relatedly' ),
			'manage_options',
			'relatedly-statistics',
			[ $this, 'render_statistics' ]
		);
	}

	/**
	 * Render the settings page.
	 *
	 * @since 2.0.0
	 */
	public function render_settings(): void {
		( new SettingsPage( $this->options ) )->render();
	}

	/**
	 * Render the statistics page.
	 *
	 * @since 2.0.0
	 */
	public function render_statistics(): void {
		( new StatsPage( $this->options, $this->cache ) )->render();
	}

	// -------------------------------------------------------------------------
	// Internal
	// -------------------------------------------------------------------------

	/**
	 * Get the admin menu icon URI.
	 *
	 * WordPress renders custom menu SVGs as image data on a dark admin sidebar,
	 * so the branded dark icon is not suitable here.
	 *
	 * @since 2.0.0
	 * @since 2.3.3 Uses a high-contrast admin-specific SVG.
	 * @return string Icon data URI or dashicon fallback.
	 */
	private function get_menu_icon(): string {
		return 'data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyMCAyMCI+PHBhdGggZmlsbD0iI2E3YWFhZCIgZD0iTTMgM2g2YTIgMiAwIDAgMSAyIDJ2NEg5VjVINHY4aDV2LTJoMnYyYTIgMiAwIDAgMS0yIDJIM2ExIDEgMCAwIDEtMS0xVjRhMSAxIDAgMCAxIDEtMXoiLz48cGF0aCBmaWxsPSIjYTdhYWFkIiBkPSJNMTEgN2g2YTEgMSAwIDAgMSAxIDF2OGExIDEgMCAwIDEtMSAxaC02YTIgMiAwIDAgMS0yLTJ2LTRoMnY0aDVWOWgtNVY3eiIvPjxwYXRoIGZpbGw9IiNhN2FhYWQiIGQ9Ik03LjUgOWg1YTEgMSAwIDEgMSAwIDJoLTVhMSAxIDAgMSAxIDAtMnoiLz48L3N2Zz4=';
	}
}
