<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Branding;

/**
 * Manages CSS and JavaScript enqueuing for the STRYKE admin shell.
 *
 * Migrated from stryke-branding (StrykeWP\Branding\AdminStyles).
 * Assets are loaded from stryke-wp/src/Modules/Branding/assets/.
 *
 * @package StrykeWP\Modules\Branding
 */
class AdminStyles {

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

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

	public function register_hooks(): void {
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue' ) );
		add_action( 'admin_enqueue_scripts', array( $this, 'dequeue_wp_defaults' ), 999 );
		add_filter( 'script_loader_tag', array( $this, 'add_defer_attribute' ), 10, 2 );
	}

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

	/**
	 * Enqueue all STRYKE admin assets.
	 */
	public function enqueue(): void {
		$url      = $this->assets_url;
		$ver      = STRYKE_WP_VERSION;
		$font_url = esc_url( STRYKE_WP_URL . 'assets/fonts/inter/' );

		// Inject @font-face with absolute URL (DSGVO-compliant local hosting).
		// Done via inline style so the path is always absolute regardless of
		// where the CSS file is served from.
		$font_face_css = "
@font-face {
	font-family: 'Inter';
	font-style: normal;
	font-weight: 400;
	font-display: swap;
	src: url('{$font_url}Inter-Regular.woff2') format('woff2');
}
@font-face {
	font-family: 'Inter';
	font-style: normal;
	font-weight: 500;
	font-display: swap;
	src: url('{$font_url}Inter-Medium.woff2') format('woff2');
}
@font-face {
	font-family: 'Inter';
	font-style: normal;
	font-weight: 600;
	font-display: swap;
	src: url('{$font_url}Inter-SemiBold.woff2') format('woff2');
}";

		// CSS.
		wp_enqueue_style( 'stryke-admin-shell', $url . 'css/stryke-admin-shell.css', array(), $ver );
		wp_add_inline_style( 'stryke-admin-shell', $font_face_css );
		wp_enqueue_style( 'stryke-admin-overrides', $url . 'css/stryke-admin-overrides.css', array( 'stryke-admin-shell' ), $ver );
		wp_enqueue_style( 'stryke-admin-pages', $url . 'css/stryke-admin-pages.css', array( 'stryke-admin-overrides' ), $ver );

		// Gutenberg compat (only on block editor screens).
		$screen = get_current_screen();
		if ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() ) {
			wp_enqueue_style( 'stryke-editor-compat', $url . 'css/stryke-editor-compat.css', array( 'stryke-admin-overrides' ), $ver );
		}

		// Alpine.js (local, GDPR compliant). Loaded in header with defer.
		// File must be placed at: src/Modules/Branding/assets/js/alpine.min.js
		// Download from: https://cdn.jsdelivr.net/npm/alpinejs@3.14.8/dist/cdn.min.js
		wp_enqueue_script( 'stryke-alpine', $url . 'js/alpine.min.js', array(), '3.14.8', false );

		// STRYKE Branding Admin JS. Must load before Alpine.js initializes.
		wp_enqueue_script( 'stryke-admin-js', $url . 'js/stryke-admin.js', array(), $ver, false );

		// Pass data to JS.
		wp_localize_script(
			'stryke-admin-js',
			'strykeAdmin',
			array(
				'ajaxUrl'   => admin_url( 'admin-ajax.php' ),
				'nonce'     => wp_create_nonce( 'stryke_admin_nonce' ),
				'themeMode' => $this->theme_pref->get_current_mode(),
			)
		);
	}

	/**
	 * Remove default WordPress admin styles that conflict.
	 */
	public function dequeue_wp_defaults(): void {
		// Remove legacy Google Fonts (GDPR).
		wp_dequeue_style( 'google-font-inter' );
		wp_deregister_style( 'google-font-inter' );

		// Remove old STRYKE branding styles if the legacy plugin is still active.
		wp_dequeue_style( 'stryke-admin-styles' );
	}

	/**
	 * Add defer attribute to Alpine.js and admin script for proper loading order.
	 */
	public function add_defer_attribute( string $tag, string $handle ): string {
		if ( in_array( $handle, array( 'stryke-alpine', 'stryke-admin-js' ), true ) ) {
			if ( str_contains( $tag, 'defer' ) ) {
				return $tag;
			}
			return str_replace( ' src=', ' defer src=', $tag );
		}
		return $tag;
	}
}
