<?php

declare( strict_types=1 );

namespace StrykeWP\Core;

/**
 * Asset management service for STRYKE WP.
 *
 * Centralises registration and conditional enqueueing of CSS/JS assets.
 * Only loads assets on plugin-owned admin pages to avoid frontend impact.
 *
 * @package StrykeWP\Core
 */
class AssetManager {

	private string $base_url;
	private string $version;

	public function __construct( string $base_url, string $version ) {
		$this->base_url = rtrim( $base_url, '/' );
		$this->version  = $version;
	}

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

	public function register_hooks(): void {
		add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_admin_assets' ] );
	}

	// ---------------------------------------------------------------------------
	// Admin Assets
	// ---------------------------------------------------------------------------

	/**
	 * Enqueues admin CSS/JS only on STRYKE WP admin pages.
	 *
	 * WordPress passes the current admin page hook as $hook_suffix.
	 * Our pages are registered under the 'stryke-wp' slug, so the top-level
	 * hook is 'toplevel_page_stryke-wp' and subpages are 'stryke-wp_page_*'.
	 *
	 * @param string $hook_suffix Current admin page hook suffix.
	 */
	public function enqueue_admin_assets( string $hook_suffix ): void {
		if ( ! $this->is_stryke_page( $hook_suffix ) ) {
			return;
		}

		// Main admin stylesheet (dark theme + design system).
		wp_enqueue_style(
			'stryke-wp-admin',
			$this->asset_url( 'css/admin.css' ),
			[],
			$this->version
		);

		// Main admin script.
		wp_enqueue_script(
			'stryke-wp-admin',
			$this->asset_url( 'js/admin.js' ),
			[],
			$this->version,
			true  // Load in footer.
		);

		// Pass PHP data to JS.
		wp_localize_script(
			'stryke-wp-admin',
			'strykeWP',
			[
				'ajaxUrl' => admin_url( 'admin-ajax.php' ),
				'nonce'   => wp_create_nonce( 'stryke_wp_admin' ),
				'version' => $this->version,
				'i18n'    => [
					'enabled'  => __( 'Aktiv', 'stryke-wp' ),
					'disabled' => __( 'Inaktiv', 'stryke-wp' ),
					'saving'   => __( 'Wird gespeichert…', 'stryke-wp' ),
					'error'    => __( 'Fehler beim Speichern.', 'stryke-wp' ),
				],
			]
		);
	}

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

	/**
	 * Checks if the current admin page belongs to STRYKE WP.
	 *
	 * @param string $hook_suffix WP admin page hook.
	 */
	public function is_stryke_page( string $hook_suffix ): bool {
		return str_contains( $hook_suffix, 'stryke-wp' );
	}

	/**
	 * Builds a full URL for an asset relative to the assets/ directory.
	 *
	 * @param string $path Relative path, e.g. 'css/admin.css'.
	 */
	public function asset_url( string $path ): string {
		return $this->base_url . '/assets/' . ltrim( $path, '/' );
	}

	/**
	 * Returns the base plugin URL.
	 */
	public function base_url(): string {
		return $this->base_url;
	}
}
