<?php
/**
 * App handler for SureCookie plugin.
 *
 * @package    SureCookie
 * @subpackage SureCookie/Core
 * @since      0.0.1
 */

namespace SureCookie\Core;

use SureCookie\Inc\Functions\Get;
use SureCookie\Inc\Functions\Sanitize;
use SureCookie\Inc\Functions\Settings;
use SureCookie\Inc\Functions\Validate;
use SureCookie\Inc\Traits\GetInstance;
use SureCookie\Inc\Utils\LocalizeData;

defined( 'ABSPATH' ) || exit;

/**
 * Frontend handler for SureCookie plugin.
 *
 * This class handles the registration and rendering of the admin menu.
 *
 * @since 0.0.1
 */
class Frontend {
	use GetInstance;

	/**
	 * Constructor.
	 *
	 * @since 0.0.1
	 */
	public function __construct() {
		add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_public_assets' ] );
		// Use wp_body_open to put the banner root at the very top of the body for better accessibility.
		// Fallback to wp_footer if the theme doesn't support wp_body_open.
		if ( function_exists( 'wp_body_open' ) ) {
			add_action( 'wp_body_open', [ $this, 'add_public_root' ], 1 );
		} else {
			add_action( 'wp_footer', [ $this, 'add_public_root' ] );
		}
	}

	/**
	 * Add public root div to footer.
	 *
	 * @since 0.0.1
	 * @return void
	 */
	public function add_public_root(): void {
		echo '<div id="surecookie-public-root"></div>';
	}

	/**
	 * Enqueue public assets.
	 *
	 * @since 0.0.1
	 * @return void
	 */
	public function enqueue_public_assets(): void {
		// Early bail if in admin area or the banner is not enabled.
		if ( is_admin() || ! Settings::get( 'banner_enabled' ) ) {
			return;
		}

		// Check if script file exists before enqueuing.
		$base_directory = plugin_dir_path( SURECOOKIE_FILE ) . 'build/';
		$assets_file    = $base_directory . 'public.asset.php';
		if ( ! file_exists( $assets_file ) ) {
			return;
		}
		$assets_info = include $assets_file;

		wp_enqueue_style(
			'surecookie-public',
			SURECOOKIE_URL . 'assets/css/' . Get::shared_style_css(),
			[],
			SURECOOKIE_VERSION
		);

		// Add palette CSS variables from stored option.
		$palette_css = Sanitize::stylesheet( Get::palette_root_css() );
		if ( ! empty( $palette_css ) ) {
			wp_add_inline_style( 'surecookie-public', wp_strip_all_tags( $palette_css ) );
		}

		// Add custom CSS if available.
		$custom_css = Sanitize::stylesheet( (string) Settings::get( 'custom_css' ) );
		if ( Validate::not_empty( $custom_css ) ) {
			wp_add_inline_style( 'surecookie-public', "/* SureCookie Custom CSS - Public */\n" . wp_strip_all_tags( $custom_css ) );
		}

		/**
		 * Enqueue or register additional frontend scripts before the main public script.
		 *
		 * @param array<string, mixed> $assets_info Build asset metadata for surecookie-public.
		 */
		do_action( 'surecookie_public_enqueue_scripts', $assets_info );

		$public_dependencies = apply_filters( 'surecookie_public_script_dependencies', $assets_info['dependencies'] ?? [] );
		$public_dependencies = is_array( $public_dependencies ) ? $public_dependencies : ( $assets_info['dependencies'] ?? [] );

		// Enqueue the JavaScript file.
		wp_enqueue_script(
			'surecookie-public',
			SURECOOKIE_URL . 'build/public.js',
			$public_dependencies,
			$assets_info['version'] ?? SURECOOKIE_VERSION,
			true
		);
		wp_set_script_translations( 'surecookie-public', 'surecookie', SURECOOKIE_DIR . 'languages' );

		// Localize frontend data using utility.
		LocalizeData::localize_frontend_script( 'surecookie-public' );
	}
}
