<?php

declare( strict_types=1 );

namespace StrykeWP\Modules\Branding;

/**
 * Customizes the WordPress login page with full STRYKE branding.
 *
 * Dark design, centered card, custom logo, no WordPress branding.
 * Migrated from stryke-branding (StrykeWP\Branding\LoginPage).
 *
 * @package StrykeWP\Modules\Branding
 */
class LoginPage {

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

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

	public function register_hooks(): void {
		add_action( 'login_enqueue_scripts', array( $this, 'enqueue_assets' ) );
		add_action( 'login_head', array( $this, 'render_critical_css' ) );
		add_filter( 'login_headerurl', array( $this, 'logo_url' ) );
		add_filter( 'login_headertext', array( $this, 'logo_text' ) );
		add_filter( 'login_body_class', array( $this, 'body_class' ) );
	}

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

	/**
	 * Enqueue login page styles and inject @font-face with absolute URL (DSGVO).
	 */
	public function enqueue_assets(): void {
		wp_enqueue_style(
			'stryke-login',
			$this->assets_url . 'css/stryke-login.css',
			array(),
			STRYKE_WP_VERSION
		);

		// Inject @font-face with absolute URL so Inter is loaded locally.
		// The relative path in the CSS file would resolve incorrectly from the
		// login page context; absolute URL always works (DSGVO-compliant).
		$font_url = esc_url( STRYKE_WP_URL . 'assets/fonts/inter/' );
		$font_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')}";

		// Inject logo as background-image with absolute URL.
		$logo_path = $this->assets_dir . 'img/stryke-logo.png';
		if ( file_exists( $logo_path ) ) {
			$logo_url  = esc_url( $this->assets_url . 'img/stryke-logo.png' );
			$font_css .= "#login h1 a,.login h1 a{background-image:url('{$logo_url}') !important}";
		}

		wp_add_inline_style( 'stryke-login', $font_css );
	}

	/**
	 * Output critical inline CSS to prevent white flash.
	 */
	public function render_critical_css(): void {
		echo '<style id="stryke-login-critical">
			body.login.stryke-login {
				background: #0d0d0d !important;
				opacity: 1 !important;
			}
		</style>';
	}

	/**
	 * Logo link URL.
	 */
	public function logo_url(): string {
		return 'https://www.strykemarketing.de/';
	}

	/**
	 * Logo tooltip text.
	 */
	public function logo_text(): string {
		return 'STRYKE Hub — by STRYKE Marketing';
	}

	/**
	 * Add custom body class to login page.
	 *
	 * @param string[] $classes
	 * @return string[]
	 */
	public function body_class( array $classes ): array {
		$classes[] = 'stryke-login';
		return $classes;
	}
}
