<?php
/**
 * Self-hosted brand assets for admin UI (CSP-friendly; no third-party requests).
 *
 * @package WP_PowerSuite\Admin
 * @since 1.0.0
 */

namespace WP_PowerSuite\Admin;

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

final class BrandAssets {

	private const ICON_REL_PATH = 'assets/icon.svg';

	/**
	 * Default dashicon when the bundled SVG is unavailable.
	 */
	public const DEFAULT_MENU_DASHICON = 'dashicons-admin-plugins';

	/**
	 * URL to the bundled brand icon (same-origin, versioned).
	 */
	public static function icon_url(): string {
		$default = self::default_icon_url();

		/**
		 * Filter the self-hosted WP PowerSuite brand icon URL.
		 *
		 * @param string $default Plugin-relative brand icon URL, or empty when missing.
		 */
		return (string) apply_filters( 'wp_powersuite_brand_icon_url', $default );
	}

	/**
	 * Unfiltered bundled icon URL.
	 */
	public static function default_icon_url(): string {
		$url_base = defined( 'WP_POWERSUITE_PLUGIN_URL' ) ? WP_POWERSUITE_PLUGIN_URL : '';
		$dir_base = defined( 'WP_POWERSUITE_PLUGIN_DIR' ) ? WP_POWERSUITE_PLUGIN_DIR : '';

		if ( $url_base === '' || $dir_base === '' || ! is_readable( $dir_base . self::ICON_REL_PATH ) ) {
			return '';
		}

		$version = defined( 'WP_POWERSUITE_VERSION' ) ? WP_POWERSUITE_VERSION : '1.0.0';

		return $url_base . self::ICON_REL_PATH . '?ver=' . rawurlencode( $version );
	}

	/**
	 * Value for {@see add_menu_page()} icon argument: bundled SVG URL or dashicons fallback.
	 */
	public static function admin_menu_icon(): string {
		/**
		 * Filter the admin menu icon (dashicons class name or same-origin image URL).
		 *
		 * @param string|null $icon Null to use the bundled SVG / dashicons fallback.
		 */
		$filtered = apply_filters( 'wp_powersuite_admin_menu_icon', null );
		if ( is_string( $filtered ) && $filtered !== '' ) {
			return $filtered;
		}

		$url = self::icon_url();
		if ( $url !== '' ) {
			return $url;
		}

		return self::DEFAULT_MENU_DASHICON;
	}

	/**
	 * Whether the menu icon is a dashicons class (not an image URL).
	 */
	public static function is_dashicon_menu_icon( string $icon ): bool {
		return str_starts_with( $icon, 'dashicons-' );
	}

	/**
	 * System UI font stack aligned with wp-admin (no external font CDN).
	 *
	 * @return string CSS font-family value.
	 */
	public static function admin_font_family(): string {
		return '-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif';
	}
}
