<?php

declare( strict_types=1 );

namespace StrykeWP\Admin;

use StrykeWP\Modules\ModuleRegistry;

/**
 * Registers the unified STRYKE WP admin menu.
 *
 * Creates a single top-level menu entry "STRYKE WP" with subpages for
 * each module. Replaces the individual plugin menus that would otherwise
 * create separate top-level items.
 *
 * @package StrykeWP\Admin
 */
class MenuManager {

	public function __construct(
		private readonly ModuleRegistry $registry,
		private readonly Dashboard      $dashboard,
	) {}

	// ---------------------------------------------------------------------------
	// Hook Registration
	// ---------------------------------------------------------------------------

	public function register_hooks(): void {
		add_action( 'admin_menu', [ $this, 'register_menu' ] );
		add_filter( 'admin_body_class', [ $this, 'add_body_class' ] );
	}

	// ---------------------------------------------------------------------------
	// Menu Registration
	// ---------------------------------------------------------------------------

	/**
	 * Registers the top-level "STRYKE WP" menu and all module subpages.
	 */
	public function register_menu(): void {
		// Top-level menu — dashboard overview.
		add_menu_page(
			__( 'STRYKE Hub', 'stryke-wp' ),
			'STRYKE Hub',
			'manage_options',
			'stryke-wp',
			[ $this->dashboard, 'render_overview' ],
			$this->get_menu_icon(),
			3  // Position: just below Dashboard.
		);

		// Rename the first auto-generated subpage to "Dashboard".
		add_submenu_page(
			'stryke-wp',
			__( 'Dashboard', 'stryke-wp' ),
			__( 'Dashboard', 'stryke-wp' ),
			'manage_options',
			'stryke-wp',
			[ $this->dashboard, 'render_overview' ]
		);

		// Subpage for each registered module.
		foreach ( $this->registry->all() as $module ) {
			add_submenu_page(
				'stryke-wp',
				/* translators: %s: module name */
				sprintf( __( '%s — STRYKE Hub', 'stryke-wp' ), $module->get_name() ),
				$module->get_name(),
				'manage_options',
				'stryke-wp-' . $module->get_id(),
				[ $this->dashboard, 'render_module_page' ]
			);
		}
	}

	// ---------------------------------------------------------------------------
	// Body Class
	// ---------------------------------------------------------------------------

	/**
	 * Adds 'stryke-wp-page' to the admin body class on STRYKE WP screens.
	 * Used by CSS to apply our custom theme overrides.
	 *
	 * @param string $classes Existing body classes.
	 * @return string Modified body classes.
	 */
	public function add_body_class( string $classes ): string {
		$screen = get_current_screen();
		if ( $screen && str_contains( $screen->base, 'stryke-wp' ) ) {
			$classes .= ' stryke-wp-page';
		}
		return $classes;
	}

	// ---------------------------------------------------------------------------
	// Helpers
	// ---------------------------------------------------------------------------

	/**
	 * Returns the SVG data URI for the STRYKE WP menu icon.
	 *
	 * Using an inline SVG ensures the icon matches our #189ba7 brand colour
	 * without relying on Dashicons.
	 */
	private function get_menu_icon(): string {
		// Lightning bolt / S-shaped icon in brand cyan.
		$svg = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20">'
			. '<path fill="#189ba7" d="M11 2L4 11h6l-1 7 7-9h-6z"/>'
			. '</svg>';
		return 'data:image/svg+xml;base64,' . base64_encode( $svg );
	}
}
