<?php
/**
 * Header module — registry, chrome classes, tokens, assets.
 *
 * @package Kutby
 * @since   2.5.0
 */

declare(strict_types=1);

namespace Kutby\Modules;

use Kutby\Contracts\ModuleInterface;
use Kutby\Header\Layouts\CenteredHeader;
use Kutby\Header\Layouts\ClassicHeader;
use Kutby\Header\Layouts\EducationModernHeader;
use Kutby\Header\Layouts\ModernHeader;
use Kutby\Header\Services\HeaderRegistry;
use Kutby\Header\Support\HeaderContext;
use Kutby\ThemeOptions\Manager;

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

/**
 * Class HeaderModule
 */
final class HeaderModule implements ModuleInterface {

	/**
	 * {@inheritdoc}
	 */
	public function register(): void {
		$registry = HeaderRegistry::instance();
		$registry->add( 'classic', ClassicHeader::class );
		$registry->add( 'modern', ModernHeader::class );
		$registry->add( 'centered', CenteredHeader::class );
		$registry->add( 'education-modern', EducationModernHeader::class );

		/**
		 * Register additional header layouts.
		 *
		 * @since 2.6.0
		 *
		 * @param HeaderRegistry $registry Registry.
		 */
		do_action( 'kutby_header_register_layouts', $registry );

		add_filter( 'kutby_options_schema', array( $this, 'filter_layout_options' ) );
		add_filter( 'kutby_css_variables', array( $this, 'filter_css_variables' ) );
		add_filter( 'body_class', array( $this, 'body_classes' ) );

		/**
		 * Header module ready (layouts registered).
		 *
		 * @since 2.5.0
		 */
		do_action( 'kutby_header_module_ready' );
	}

	/**
	 * {@inheritdoc}
	 */
	public function boot(): void {}

	/**
	 * Header-owned body classes (sticky chrome).
	 *
	 * Sticky is owned by HeaderModule — not FrontendHooks — so layout engines
	 * keep chrome behavior inside their module boundary.
	 *
	 * @param array<int, string> $classes Classes.
	 * @return array<int, string>
	 */
	public function body_classes( array $classes ): array {
		if ( Manager::get( 'header.sticky' ) ) {
			$classes[] = 'kutby-header-sticky';
		}

		$layout = HeaderContext::from_request()->layout_id();
		if ( HeaderRegistry::instance()->has( $layout ) ) {
			$classes[] = 'kutby-header-layout-' . sanitize_html_class( $layout );
		}

		return $classes;
	}

	/**
	 * Sync layout select options from Registry labels.
	 *
	 * @param array<string, array<string, array<string, mixed>>> $schema Schema.
	 * @return array<string, array<string, array<string, mixed>>>
	 */
	public function filter_layout_options( array $schema ): array {
		$labels = HeaderRegistry::instance()->labels();
		if ( empty( $labels ) || empty( $schema['header']['layout'] ) ) {
			return $schema;
		}

		$schema['header']['layout']['options'] = $labels;
		return $schema;
	}

	/**
	 * Expose header height as design token (single height path).
	 *
	 * @param array<string, string> $tokens Tokens.
	 * @return array<string, string>
	 */
	public function filter_css_variables( array $tokens ): array {
		$ctx                             = HeaderContext::from_request();
		$tokens['--kutby-header-height'] = $ctx->height() . 'px';
		return $tokens;
	}
}
