<?php
/**
 * Frontend integration.
 *
 * @package Noravo
 */

declare(strict_types=1);

namespace Noravo\Frontend;

use Noravo\Assets\AssetManager;
use Noravo\Settings\SettingsRepository;

/**
 * Loads frontend assets and renders the notification mount point.
 */
final class Frontend {
	private SettingsRepository $settings;

	private AssetManager $assets;

	/**
	 * @param SettingsRepository $settings Plugin settings store.
	 * @param AssetManager       $assets   Frontend asset loader.
	 */
	public function __construct(SettingsRepository $settings, AssetManager $assets) {
		$this->settings = $settings;
		$this->assets   = $assets;
	}

	/** Registers frontend enqueue and footer render hooks. */
	public function register(): void {
		add_action('wp_enqueue_scripts', array($this, 'enqueue'));
		add_action('wp_footer', array($this, 'render_root'));
	}

	/** Enqueues frontend assets when notifications are enabled. */
	public function enqueue(): void {
		if (! $this->settings->is_enabled()) {
			return;
		}

		$this->assets->enqueue_frontend();
	}

	/** Outputs the DOM root element for client-side notifications. */
	public function render_root(): void {
		if (! $this->settings->is_enabled()) {
			return;
		}

		$settings = $this->settings->all();
		$styles   = sprintf(
			'--noravo-toast-width:%1$dpx;--noravo-toast-radius:%2$dpx;--noravo-toast-text:%3$s;--noravo-toast-background:%4$s;',
			absint($settings['toast_width']),
			absint($settings['border_radius']),
			esc_attr($settings['text_color']),
			esc_attr($settings['background_color'])
		);

		printf(
			'<div id="noravo-root" class="noravo-root noravo-%1$s noravo-animation-%2$s" style="%3$s" aria-live="polite" aria-atomic="true"></div>',
			esc_attr($settings['position']),
			esc_attr($settings['animation']),
			esc_attr($styles)
		);
	}
}
