<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Branding;

/**
 * Renders the custom STRYKE admin shell (top bar, sidebar, footer).
 *
 * Replaces the WordPress admin chrome entirely with a custom UI.
 * Strategy: CSS-hide WP elements, inject custom HTML via hooks.
 * Migrated from stryke-branding (StrykeWP\Branding\AdminShell).
 *
 * Changes from original:
 *  - Uses STRYKE_WP_DIR / STRYKE_WP_VERSION instead of STRYKE_BRANDING_* constants.
 *  - STRYKE WP sidebar section updated to link to master plugin menu (stryke-wp).
 *  - Active-state map updated for new module slugs (stryke-wp-connector, etc.).
 *
 * @package StrykeWP\Modules\Branding
 */
class AdminShell {

	public function __construct(
		private readonly ThemePreference $theme_pref,
		private readonly string          $assets_dir,
	) {}

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

	public function register_hooks(): void {
		add_filter( 'admin_body_class', array( $this, 'add_body_classes' ) );
		add_action( 'admin_head', array( $this, 'inject_shell_css_override' ), 999 );
		add_action( 'in_admin_header', array( $this, 'render_shell' ), 1 );
		add_filter( 'admin_footer_text', array( $this, 'custom_footer' ), 999 );
		add_filter( 'update_footer', '__return_empty_string', 999 );
		add_filter( 'admin_title', array( $this, 'filter_admin_title' ), 10, 2 );
		add_action( 'wp_dashboard_setup', array( $this, 'setup_dashboard' ) );
		add_action( 'admin_footer-plugins.php', array( $this, 'render_plugins_page_stryke_box' ) );
	}

	// -----------------------------------------------------------------------
	// Callbacks
	// -----------------------------------------------------------------------

	/**
	 * Add custom body classes for the shell and theme.
	 */
	public function add_body_classes( string $classes ): string {
		$classes .= ' stryke-shell ' . $this->theme_pref->get_body_class();
		return $classes;
	}

	/**
	 * Critical inline CSS to immediately hide WP chrome (prevents FOUC).
	 *
	 * Only hides elements — actual layout (margins, padding) is handled
	 * by stryke-admin-shell.css which uses the shell CSS variables.
	 */
	public function inject_shell_css_override(): void {
		echo '<style id="stryke-shell-critical">
			#adminmenumain,
			#adminmenuback,
			#adminmenuwrap,
			#wpadminbar,
			#wpfooter,
			.update-nag { display: none !important; }
			html.wp-toolbar { padding-top: 0 !important; }
		</style>';
	}

	/**
	 * Render the complete custom shell (topbar + sidebar).
	 */
	public function render_shell(): void {
		$this->render_topbar();
		$this->render_sidebar();
	}

	/**
	 * Replace "WordPress" in admin page titles.
	 */
	public function filter_admin_title( string $admin_title, string $title ): string {
		return str_replace( 'WordPress', 'STRYKE WP', $admin_title );
	}

	/**
	 * Custom admin footer text.
	 */
	public function custom_footer(): string {
		return '<span><strong>STRYKE WP</strong> &mdash; &copy; ' . esc_html( (string) gmdate( 'Y' ) ) . ' <a href="https://www.strykemarketing.de/" target="_blank" rel="noopener">STRYKE Marketing</a></span>';
	}

	/**
	 * Remove default dashboard widgets and add STRYKE welcome widget.
	 */
	public function setup_dashboard(): void {
		remove_meta_box( 'dashboard_right_now', 'dashboard', 'normal' );
		remove_meta_box( 'dashboard_activity', 'dashboard', 'normal' );
		remove_meta_box( 'dashboard_primary', 'dashboard', 'normal' );
		remove_meta_box( 'dashboard_quick_press', 'dashboard', 'side' );
		remove_meta_box( 'dashboard_site_health', 'dashboard', 'normal' );
		remove_action( 'welcome_panel', 'wp_welcome_panel' );

		wp_add_dashboard_widget(
			'stryke_welcome_widget',
			'Willkommen bei STRYKE WP',
			array( $this, 'render_welcome_widget' )
		);
	}

	/**
	 * Render the welcome widget content.
	 */
	public function render_welcome_widget(): void {
		echo '<p>' . esc_html__( 'Ihr STRYKE WP Branding ist aktiv. Die Verwaltungsoberfläche wurde optimiert für eine fokussierte Arbeitsumgebung.', 'stryke-wp' ) . '</p>';
		echo '<p><a href="' . esc_url( admin_url( 'admin.php?page=stryke-wp-support' ) ) . '" class="button button-primary">Support kontaktieren</a></p>';
	}

	// -----------------------------------------------------------------------
	// Plugins Page — Separate STRYKE Box
	// -----------------------------------------------------------------------

	/**
	 * Renders a separate STRYKE-branded box at the bottom of the plugins page.
	 *
	 * Moves the STRYKE WP plugin row out of the main plugin table and into
	 * a distinct card so it visually stands apart from third-party plugins.
	 */
	public function render_plugins_page_stryke_box(): void {
		$basename = STRYKE_WP_BASENAME;
		?>
		<style>
			.stryke-plugin-box {
				margin: 32px 0 16px;
				background: var(--stryke-sidebar-bg, #141414);
				border: 1px solid var(--stryke-border, #2a2a2a);
				border-radius: 8px;
				overflow: hidden;
			}
			.stryke-plugin-box__header {
				display: flex;
				align-items: center;
				gap: 12px;
				padding: 16px 20px;
				border-bottom: 1px solid var(--stryke-border, #2a2a2a);
			}
			.stryke-plugin-box__logo {
				height: 28px;
				width: auto;
			}
			.stryke-plugin-box__title {
				font-size: 15px;
				font-weight: 700;
				color: var(--stryke-primary, #189ba7);
				margin: 0;
				letter-spacing: 0.5px;
			}
			.stryke-plugin-box__body {
				padding: 20px;
				display: flex;
				align-items: center;
				justify-content: space-between;
				flex-wrap: wrap;
				gap: 16px;
			}
			.stryke-plugin-box__info {
				flex: 1;
				min-width: 200px;
			}
			.stryke-plugin-box__name {
				font-size: 16px;
				font-weight: 600;
				color: var(--stryke-text, #f0f0f0);
				margin: 0 0 4px;
			}
			.stryke-plugin-box__desc {
				font-size: 13px;
				color: var(--stryke-text-muted, #888);
				margin: 0 0 8px;
			}
			.stryke-plugin-box__version {
				font-size: 12px;
				color: var(--stryke-text-muted, #888);
			}
			.stryke-plugin-box__actions {
				display: flex;
				gap: 8px;
				align-items: center;
			}
			.stryke-plugin-box__actions a {
				display: inline-block;
				padding: 6px 16px;
				border-radius: 6px;
				font-size: 13px;
				font-weight: 500;
				text-decoration: none;
				transition: background 0.15s, color 0.15s;
			}
			.stryke-plugin-box__actions .stryke-pb-settings {
				background: var(--stryke-primary, #189ba7);
				color: #fff;
			}
			.stryke-plugin-box__actions .stryke-pb-settings:hover {
				background: var(--stryke-primary-hover, #1db8c6);
				color: #fff;
			}
			.stryke-plugin-box__actions .stryke-pb-deactivate {
				background: transparent;
				border: 1px solid var(--stryke-border, #2a2a2a);
				color: var(--stryke-text-muted, #888);
			}
			.stryke-plugin-box__actions .stryke-pb-deactivate:hover {
				border-color: var(--stryke-error, #ff5555);
				color: var(--stryke-error, #ff5555);
			}
		</style>

		<div class="stryke-plugin-box" id="stryke-plugin-box">
			<div class="stryke-plugin-box__header">
				<?php echo $this->get_logo_img( 28 ); ?>
				<span class="stryke-plugin-box__title">STRYKE WP</span>
			</div>
			<div class="stryke-plugin-box__body">
				<div class="stryke-plugin-box__info">
					<p class="stryke-plugin-box__name"><?php esc_html_e( 'STRYKE WP — Master Plugin', 'stryke-wp' ); ?></p>
					<p class="stryke-plugin-box__desc"><?php esc_html_e( 'Hub Connector, Admin Branding, Backup und WIP-Mode in einem Paket.', 'stryke-wp' ); ?></p>
					<span class="stryke-plugin-box__version">v<?php echo esc_html( STRYKE_WP_VERSION ); ?></span>
				</div>
				<div class="stryke-plugin-box__actions">
					<a href="<?php echo esc_url( admin_url( 'admin.php?page=stryke-wp' ) ); ?>" class="stryke-pb-settings">
						<?php esc_html_e( 'Dashboard öffnen', 'stryke-wp' ); ?>
					</a>
					<?php
					$deactivate_url = wp_nonce_url(
						admin_url( 'plugins.php?action=deactivate&plugin=' . rawurlencode( $basename ) ),
						'deactivate-plugin_' . $basename
					);
					?>
					<a href="<?php echo esc_url( $deactivate_url ); ?>" class="stryke-pb-deactivate">
						<?php esc_html_e( 'Deaktivieren', 'stryke-wp' ); ?>
					</a>
				</div>
			</div>
		</div>

		<script>
		(function() {
			// Hide STRYKE WP from the main plugin table (it's in the box now).
			var row = document.querySelector('tr[data-plugin="<?php echo esc_js( $basename ); ?>"]');
			if (row) { row.style.display = 'none'; }
			// Also hide the update row if present.
			var next = row && row.nextElementSibling;
			if (next && next.classList.contains('plugin-update-tr')) { next.style.display = 'none'; }
		})();
		</script>
		<?php
	}

	// -----------------------------------------------------------------------
	// Shell Rendering
	// -----------------------------------------------------------------------

	/**
	 * Render the custom top bar.
	 */
	private function render_topbar(): void {
		$user          = wp_get_current_user();
		$display_name  = $user->display_name;
		$avatar_url    = get_avatar_url( $user->ID, array( 'size' => 64 ) );
		$profile_url   = esc_url( get_edit_profile_url() );
		$site_url      = esc_url( home_url( '/' ) );
		$logout_url    = esc_url( wp_logout_url() );
		$update_data   = wp_get_update_data();
		$update_count  = (int) ( $update_data['counts']['total'] ?? 0 );
		$breadcrumb    = $this->get_breadcrumb();
		?>
		<div class="stryke-topbar" id="stryke-topbar" x-data>
			<div class="stryke-topbar__left">
				<button class="stryke-topbar__toggle" @click="$dispatch('stryke-toggle-sidebar')" aria-label="Toggle Sidebar">
					<?php echo $this->icon( 'menu', 20 ); ?>
				</button>
				<a href="<?php echo esc_url( admin_url() ); ?>" class="stryke-topbar__logo">
					<?php echo $this->get_logo_img( 28 ); ?>
				</a>
			</div>

			<div class="stryke-topbar__center">
				<?php if ( $breadcrumb ) : ?>
					<nav class="stryke-breadcrumb" aria-label="Breadcrumb">
						<?php echo $breadcrumb; // phpcs:ignore WordPress.Security.EscapeOutput ?>
					</nav>
				<?php endif; ?>
			</div>

			<div class="stryke-topbar__right">
				<button class="stryke-topbar__theme-toggle" @click="$store.theme.toggle()"
					:aria-label="$store.theme.mode === 'dark' ? 'Switch to Light Mode' : 'Switch to Dark Mode'">
					<template x-if="$store.theme.mode === 'dark'">
						<?php echo $this->icon( 'sun', 18 ); ?>
					</template>
					<template x-if="$store.theme.mode === 'light'">
						<?php echo $this->icon( 'moon', 18 ); ?>
					</template>
				</button>

				<?php if ( $update_count > 0 && current_user_can( 'update_core' ) ) : ?>
				<div class="stryke-notifications" x-data="{ open: false }">
					<button class="stryke-topbar__icon-btn" @click="open = !open" @click.outside="open = false">
						<?php echo $this->icon( 'bell', 18 ); ?>
						<span class="stryke-badge"><?php echo esc_html( (string) $update_count ); ?></span>
					</button>
					<div class="stryke-dropdown stryke-dropdown--notifications" x-show="open" x-transition x-cloak>
						<div class="stryke-dropdown__header"><?php esc_html_e( 'Updates verfügbar', 'stryke-wp' ); ?></div>
						<a href="<?php echo esc_url( admin_url( 'update-core.php' ) ); ?>" class="stryke-dropdown__item">
							<?php echo $this->icon( 'download', 16 ); ?>
							<span><?php echo esc_html( $update_count . ' Update' . ( $update_count > 1 ? 's' : '' ) . ' verfügbar' ); ?></span>
						</a>
					</div>
				</div>
				<?php endif; ?>

				<div class="stryke-user-menu" x-data="{ open: false }">
					<button class="stryke-topbar__user" @click="open = !open" @click.outside="open = false">
						<img class="stryke-avatar" src="<?php echo esc_url( $avatar_url ); ?>" alt="" width="32" height="32" />
						<span class="stryke-topbar__username"><?php echo esc_html( $display_name ); ?></span>
						<?php echo $this->icon( 'chevron-down', 14 ); ?>
					</button>
					<div class="stryke-dropdown stryke-dropdown--user" x-show="open" x-transition x-cloak>
						<a href="<?php echo esc_url( $profile_url ); ?>" class="stryke-dropdown__item">
							<?php echo $this->icon( 'user', 16 ); ?>
							<span><?php esc_html_e( 'Profil bearbeiten', 'stryke-wp' ); ?></span>
						</a>
						<a href="<?php echo esc_url( $site_url ); ?>" class="stryke-dropdown__item" target="_blank">
							<?php echo $this->icon( 'external-link', 16 ); ?>
							<span><?php esc_html_e( 'Website ansehen', 'stryke-wp' ); ?></span>
						</a>
						<div class="stryke-dropdown__divider"></div>
						<a href="<?php echo esc_url( $logout_url ); ?>" class="stryke-dropdown__item stryke-dropdown__item--danger">
							<?php echo $this->icon( 'log-out', 16 ); ?>
							<span><?php esc_html_e( 'Abmelden', 'stryke-wp' ); ?></span>
						</a>
					</div>
				</div>
			</div>
		</div>
		<?php
	}

	/**
	 * Render the custom sidebar navigation.
	 */
	private function render_sidebar(): void {
		$items        = $this->get_sidebar_items();
		$current_slug = $this->get_current_slug();
		?>
		<nav class="stryke-sidebar" id="stryke-sidebar"
			 x-data="{ collapsed: window.innerWidth < 1024, openSubmenu: null }"
			 @stryke-toggle-sidebar.window="collapsed = !collapsed"
			 @resize.window="if(window.innerWidth < 1024) collapsed = true"
			 :class="{ 'stryke-sidebar--collapsed': collapsed }">
			<ul class="stryke-nav">
				<?php foreach ( $items as $item ) : ?>
					<?php if ( 'separator' === $item ) : ?>
						<li class="stryke-nav__separator"></li>
					<?php elseif ( isset( $item['children'] ) ) : ?>
						<li class="stryke-nav__item stryke-nav__item--parent"
							:class="{ 'stryke-nav__item--active': <?php echo $this->is_active_js( $item['slug'], $current_slug ); ?> }">
							<button class="stryke-nav__link" @click="openSubmenu = openSubmenu === '<?php echo esc_attr( $item['slug'] ); ?>' ? null : '<?php echo esc_attr( $item['slug'] ); ?>'">
								<?php echo $this->icon( $item['icon'], 20 ); ?>
								<span class="stryke-nav__label"><?php echo esc_html( $item['label'] ); ?></span>
								<span class="stryke-nav__chevron" :class="{ 'stryke-nav__chevron--open': openSubmenu === '<?php echo esc_attr( $item['slug'] ); ?>' }">
									<?php echo $this->icon( 'chevron-down', 14 ); ?>
								</span>
							</button>
							<ul class="stryke-nav__submenu" x-show="openSubmenu === '<?php echo esc_attr( $item['slug'] ); ?>'" x-transition x-cloak>
								<?php foreach ( $item['children'] as $child ) : ?>
									<li><a href="<?php echo esc_url( $child['url'] ); ?>" class="stryke-nav__sublink <?php echo $current_slug === $child['slug'] ? 'stryke-nav__sublink--active' : ''; ?>">
										<?php echo esc_html( $child['label'] ); ?>
									</a></li>
								<?php endforeach; ?>
							</ul>
						</li>
					<?php else : ?>
						<li class="stryke-nav__item <?php echo $this->is_active( $item['slug'], $current_slug ) ? 'stryke-nav__item--active' : ''; ?>">
							<a href="<?php echo esc_url( $item['url'] ); ?>" class="stryke-nav__link">
								<?php echo $this->icon( $item['icon'], 20 ); ?>
								<span class="stryke-nav__label"><?php echo esc_html( $item['label'] ); ?></span>
							</a>
						</li>
					<?php endif; ?>
				<?php endforeach; ?>
			</ul>
			<div class="stryke-sidebar__footer">
				<span class="stryke-nav__label">v<?php echo esc_html( STRYKE_WP_VERSION ); ?></span>
				<a href="https://www.strykemarketing.de" target="_blank" rel="noopener noreferrer" class="stryke-sidebar__credit">STRYKE Marketing</a>
			</div>
		</nav>
		<?php
	}

	// -----------------------------------------------------------------------
	// Sidebar Items
	// -----------------------------------------------------------------------

	/**
	 * Return the sidebar navigation items.
	 */
	private function get_sidebar_items(): array {
		$items = array(
			array( 'label' => 'Dashboard',   'icon' => 'grid',           'url' => admin_url( 'index.php' ),                     'slug' => 'dashboard' ),
			array( 'label' => 'Inhalte',     'icon' => 'file-text',      'url' => admin_url( 'edit.php' ),                      'slug' => 'edit-post',
				'children' => array(
					array( 'label' => 'Alle Beiträge', 'url' => admin_url( 'edit.php' ),                        'slug' => 'edit-post' ),
					array( 'label' => 'Erstellen',     'url' => admin_url( 'post-new.php' ),                    'slug' => 'post-new' ),
					array( 'label' => 'Kategorien',    'url' => admin_url( 'edit-tags.php?taxonomy=category' ), 'slug' => 'edit-category' ),
					array( 'label' => 'Schlagwörter',  'url' => admin_url( 'edit-tags.php?taxonomy=post_tag' ), 'slug' => 'edit-post_tag' ),
				),
			),
			array( 'label' => 'Medien',      'icon' => 'image',          'url' => admin_url( 'upload.php' ),                    'slug' => 'upload' ),
			array( 'label' => 'Seiten',      'icon' => 'layers',         'url' => admin_url( 'edit.php?post_type=page' ),       'slug' => 'edit-page',
				'children' => array(
					array( 'label' => 'Alle Seiten', 'url' => admin_url( 'edit.php?post_type=page' ),      'slug' => 'edit-page' ),
					array( 'label' => 'Erstellen',   'url' => admin_url( 'post-new.php?post_type=page' ), 'slug' => 'post-new-page' ),
				),
			),
			array( 'label' => 'Kommentare',  'icon' => 'message-circle', 'url' => admin_url( 'edit-comments.php' ),             'slug' => 'edit-comments' ),
			'separator',
			array( 'label' => 'STRYKE WP',  'icon' => 'zap',            'url' => admin_url( 'admin.php?page=stryke-wp' ),      'slug' => 'stryke-wp',
				'children' => array(
					array( 'label' => 'Dashboard',   'url' => admin_url( 'admin.php?page=stryke-wp' ),              'slug' => 'stryke-wp' ),
					array( 'label' => 'Connector',   'url' => admin_url( 'admin.php?page=stryke-wp-connector' ),    'slug' => 'stryke-wp-connector' ),
					array( 'label' => 'Backup',      'url' => admin_url( 'admin.php?page=stryke-wp-backup' ),       'slug' => 'stryke-wp-backup' ),
					array( 'label' => 'WIP Modus',   'url' => admin_url( 'admin.php?page=stryke-wp-wip_mode' ),     'slug' => 'stryke-wp-wip_mode' ),
					array( 'label' => 'Support',     'url' => admin_url( 'admin.php?page=stryke-wp-support' ),      'slug' => 'stryke-wp-support' ),
				),
			),
			'separator',
			array( 'label' => 'Einstellungen', 'icon' => 'settings',    'url' => admin_url( 'options-general.php' ),           'slug' => 'options-general',
				'children' => array(
					array( 'label' => 'Allgemein',  'url' => admin_url( 'options-general.php' ),   'slug' => 'options-general' ),
					array( 'label' => 'Permalinks', 'url' => admin_url( 'options-permalink.php' ), 'slug' => 'options-permalink' ),
				),
			),
		);

		// Superadmin extras.
		if ( $this->is_super_admin() ) {
			$items[] = 'separator';
			$items[] = array( 'label' => 'Plugins',  'icon' => 'package', 'url' => admin_url( 'plugins.php' ), 'slug' => 'plugins' );
			$items[] = array( 'label' => 'Themes',   'icon' => 'palette', 'url' => admin_url( 'themes.php' ),  'slug' => 'themes' );
			$items[] = array( 'label' => 'Benutzer', 'icon' => 'users',   'url' => admin_url( 'users.php' ),   'slug' => 'users' );
			$items[] = array( 'label' => 'Tools',    'icon' => 'wrench',  'url' => admin_url( 'tools.php' ),   'slug' => 'tools' );
		}

		return $items;
	}

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

	/**
	 * Get the current page slug for active-state detection.
	 */
	private function get_current_slug(): string {
		$screen = get_current_screen();
		if ( ! $screen ) {
			return '';
		}

		// Check for STRYKE plugin pages.
		$page = sanitize_key( wp_unslash( $_GET['page'] ?? '' ) ); // phpcs:ignore WordPress.Security.NonceVerification
		if ( str_starts_with( $page, 'stryke' ) ) {
			return $page;
		}

		return $screen->base;
	}

	/**
	 * Check if a sidebar item is active.
	 */
	private function is_active( string $item_slug, string $current_slug ): bool {
		if ( $item_slug === $current_slug ) {
			return true;
		}

		$map = array(
			'dashboard'       => array( 'dashboard' ),
			'edit-post'       => array( 'edit-post', 'post', 'post-new', 'edit-category', 'edit-post_tag' ),
			'upload'          => array( 'upload', 'media' ),
			'edit-page'       => array( 'edit-page', 'page', 'post-new-page' ),
			'edit-comments'   => array( 'edit-comments', 'comment' ),
			'stryke-wp'       => array( 'stryke-wp', 'stryke-wp-connector', 'stryke-wp-branding', 'stryke-wp-backup', 'stryke-wp-wip_mode', 'stryke-wp-support' ),
			'options-general' => array( 'options-general', 'options-permalink', 'options-writing', 'options-reading' ),
			'plugins'         => array( 'plugins', 'plugin-install' ),
			'themes'          => array( 'themes', 'theme-install' ),
			'users'           => array( 'users', 'user', 'user-new', 'profile' ),
			'tools'           => array( 'tools', 'import', 'export' ),
		);

		foreach ( $map as $parent => $children ) {
			if ( $item_slug === $parent && in_array( $current_slug, $children, true ) ) {
				return true;
			}
		}

		return false;
	}

	/**
	 * JS expression for active state (for parent items with Alpine.js).
	 */
	private function is_active_js( string $slug, string $current_slug ): string {
		return $this->is_active( $slug, $current_slug ) ? 'true' : 'false';
	}

	/**
	 * Generate a breadcrumb from the current screen.
	 */
	private function get_breadcrumb(): string {
		global $title;

		$screen = get_current_screen();
		if ( ! $screen ) {
			return '';
		}

		$parts = array( '<a href="' . esc_url( admin_url() ) . '">Dashboard</a>' );

		if ( $title && 'dashboard' !== $screen->base ) {
			$parts[] = '<span>' . esc_html( wp_strip_all_tags( $title ) ) . '</span>';
		}

		return implode( ' <span class="stryke-breadcrumb__sep">/</span> ', $parts );
	}

	/**
	 * Get the STRYKE logo as an <img> tag (PNG).
	 */
	private function get_logo_img( int $height = 28 ): string {
		$logo_file = 'img/stryke-logo.png';
		$logo_path = $this->assets_dir . $logo_file;

		if ( file_exists( $logo_path ) ) {
			$logo_url = plugins_url( 'src/Modules/Branding/assets/' . $logo_file, STRYKE_WP_DIR . '/stryke-wp.php' );
			return '<img class="stryke-logo" src="' . esc_url( $logo_url ) . '" alt="STRYKE WP" height="' . $height . '" />';
		}

		return '<span class="stryke-logo-text" style="font-size:' . $height . 'px">S</span>';
	}

	/**
	 * Render an inline SVG icon.
	 */
	private function icon( string $name, int $size = 20 ): string {
		$icons = $this->get_icons();
		$svg   = $icons[ $name ] ?? '';
		if ( ! $svg ) {
			return '';
		}
		return '<svg class="stryke-icon" width="' . $size . '" height="' . $size . '" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">' . $svg . '</svg>';
	}

	/**
	 * SVG icon paths (Feather-style line icons).
	 */
	private function get_icons(): array {
		return array(
			'menu'            => '<line x1="3" y1="12" x2="21" y2="12"/><line x1="3" y1="6" x2="21" y2="6"/><line x1="3" y1="18" x2="21" y2="18"/>',
			'grid'            => '<rect x="3" y="3" width="7" height="7"/><rect x="14" y="3" width="7" height="7"/><rect x="14" y="14" width="7" height="7"/><rect x="3" y="14" width="7" height="7"/>',
			'file-text'       => '<path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><polyline points="14 2 14 8 20 8"/><line x1="16" y1="13" x2="8" y2="13"/><line x1="16" y1="17" x2="8" y2="17"/><polyline points="10 9 9 9 8 9"/>',
			'image'           => '<rect x="3" y="3" width="18" height="18" rx="2" ry="2"/><circle cx="8.5" cy="8.5" r="1.5"/><polyline points="21 15 16 10 5 21"/>',
			'layers'          => '<polygon points="12 2 2 7 12 12 22 7 12 2"/><polyline points="2 17 12 22 22 17"/><polyline points="2 12 12 17 22 12"/>',
			'message-circle'  => '<path d="M21 11.5a8.38 8.38 0 0 1-.9 3.8 8.5 8.5 0 0 1-7.6 4.7 8.38 8.38 0 0 1-3.8-.9L3 21l1.9-5.7a8.38 8.38 0 0 1-.9-3.8 8.5 8.5 0 0 1 4.7-7.6 8.38 8.38 0 0 1 3.8-.9h.5a8.48 8.48 0 0 1 8 8v.5z"/>',
			'zap'             => '<polygon points="13 2 3 14 12 14 11 22 21 10 12 10 13 2"/>',
			'settings'        => '<circle cx="12" cy="12" r="3"/><path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06a1.65 1.65 0 0 0-.33 1.82V9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/>',
			'package'         => '<line x1="16.5" y1="9.4" x2="7.5" y2="4.21"/><path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/><polyline points="3.27 6.96 12 12.01 20.73 6.96"/><line x1="12" y1="22.08" x2="12" y2="12"/>',
			'palette'         => '<circle cx="13.5" cy="6.5" r="2.5"/><circle cx="17.5" cy="10.5" r="2.5"/><circle cx="8.5" cy="7.5" r="2.5"/><circle cx="6.5" cy="12.5" r="2.5"/><path d="M12 2C6.49 2 2 6.49 2 12s4.49 10 10 10c.55 0 1-.45 1-1v-1.67c0-.55.45-1 1-1h1.67C19.07 18.33 22 15.4 22 12 22 6.49 17.51 2 12 2z"/>',
			'users'           => '<path d="M17 21v-2a4 4 0 0 0-4-4H5a4 4 0 0 0-4 4v2"/><circle cx="9" cy="7" r="4"/><path d="M23 21v-2a4 4 0 0 0-3-3.87"/><path d="M16 3.13a4 4 0 0 1 0 7.75"/>',
			'wrench'          => '<path d="M14.7 6.3a1 1 0 0 0 0 1.4l1.6 1.6a1 1 0 0 0 1.4 0l3.77-3.77a6 6 0 0 1-7.94 7.94l-6.91 6.91a2.12 2.12 0 0 1-3-3l6.91-6.91a6 6 0 0 1 7.94-7.94l-3.76 3.76z"/>',
			'bell'            => '<path d="M18 8A6 6 0 0 0 6 8c0 7-3 9-3 9h18s-3-2-3-9"/><path d="M13.73 21a2 2 0 0 1-3.46 0"/>',
			'sun'             => '<circle cx="12" cy="12" r="5"/><line x1="12" y1="1" x2="12" y2="3"/><line x1="12" y1="21" x2="12" y2="23"/><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"/><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"/><line x1="1" y1="12" x2="3" y2="12"/><line x1="21" y1="12" x2="23" y2="12"/><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"/><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"/>',
			'moon'            => '<path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"/>',
			'chevron-down'    => '<polyline points="6 9 12 15 18 9"/>',
			'user'            => '<path d="M20 21v-2a4 4 0 0 0-4-4H8a4 4 0 0 0-4 4v2"/><circle cx="12" cy="7" r="4"/>',
			'external-link'   => '<path d="M18 13v6a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h6"/><polyline points="15 3 21 3 21 9"/><line x1="10" y1="14" x2="21" y2="3"/>',
			'log-out'         => '<path d="M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"/><polyline points="16 17 21 12 16 7"/><line x1="21" y1="12" x2="9" y2="12"/>',
			'download'        => '<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4"/><polyline points="7 10 12 15 17 10"/><line x1="12" y1="15" x2="12" y2="3"/>',
		);
	}

	private function is_super_admin(): bool {
		if ( defined( 'STRYKE_SUPERADMIN' ) && STRYKE_SUPERADMIN === true ) {
			return true;
		}
		if ( is_multisite() && is_super_admin() ) {
			return true;
		}
		return current_user_can( 'manage_options' );
	}
}
