<?php
/**
 * Frontend bootstrap (asset registration; renderers live in their modules).
 *
 * @package SuiteMkt3k
 */

namespace SuiteMkt3k\Frontend;

use SuiteMkt3k\Modules\Settings\Settings_Manager;
use SuiteMkt3k\Security\Nonces;
use SuiteMkt3k\Security\Rate_Limiter;
use SuiteMkt3k\Utils\Sanitizer;

defined( 'ABSPATH' ) || exit;

final class Frontend {

	/** @var array<string, mixed> */
	private static array $front_config = array();

	public static function register(): void {
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'register_assets' ), 5 );
		add_action( 'wp_enqueue_scripts', array( __CLASS__, 'maybe_enqueue_cart_badge_assets' ), 20 );
		add_action( 'wp_ajax_suitemkt3k_cart_count', array( __CLASS__, 'ajax_cart_count' ) );
		add_action( 'wp_ajax_nopriv_suitemkt3k_cart_count', array( __CLASS__, 'ajax_cart_count' ) );
	}

	public static function register_assets(): void {
		wp_register_style( 'suitemkt3k-frontend', SUITEMKT3K_URL . 'assets/frontend/css/frontend.css', array(), SUITEMKT3K_VERSION );
		wp_register_script( 'suitemkt3k-frontend', SUITEMKT3K_URL . 'assets/frontend/js/frontend.js', array(), SUITEMKT3K_VERSION, true );
	}

	/**
	 * Enqueue public CSS/JS and REST config (safe to call multiple times; config is merged).
	 *
	 * @param array<string, mixed> $extra Extra keys for SuiteMkt3kFront (e.g. currency).
	 */
	public static function enqueue_public_assets( array $extra = array() ): void {
		if ( is_admin() ) {
			return;
		}

		wp_enqueue_style( 'suitemkt3k-frontend' );
		wp_enqueue_script( 'suitemkt3k-frontend' );

		self::$front_config = array_merge(
			self::$front_config,
			array(
				'rest'  => esc_url_raw( rest_url( SUITEMKT3K_REST_NAMESPACE ) ),
				'nonce' => wp_create_nonce( 'wp_rest' ),
			),
			$extra
		);

		wp_localize_script( 'suitemkt3k-frontend', 'SuiteMkt3kFront', self::$front_config );
	}

	public static function maybe_enqueue_cart_badge_assets(): void {
		if ( is_admin() || ! class_exists( 'WooCommerce' ) ) {
			return;
		}
		if ( ! Settings_Manager::get( 'advanced', 'enable_cart_icon_count', false ) ) {
			return;
		}
		$count = 0;
		if ( function_exists( 'WC' ) && WC()->cart ) {
			$count = (int) WC()->cart->get_cart_contents_count();
		}
		self::enqueue_public_assets(
			array(
				'cartBadgeEnabled' => true,
				'cartCount'        => $count,
				'ajaxUrl'          => admin_url( 'admin-ajax.php' ),
				'publicNonce'      => wp_create_nonce( Nonces::PUBLIC_FORM ),
			)
		);
	}

	public static function ajax_cart_count(): void {
		$nonce = Sanitizer::post( 'nonce', 'text' );
		if ( ! Nonces::verify_admin( $nonce, Nonces::PUBLIC_FORM ) ) {
			wp_send_json_error( array( 'message' => 'invalid_nonce' ), 403 );
		}
		$visitor = Rate_Limiter::visitor_id();
		if ( ! Rate_Limiter::check( 'cart_count_badge', $visitor, 120, 60 ) ) {
			wp_send_json_error( array( 'message' => 'rate_limited' ), 429 );
		}
		$count = 0;
		if ( class_exists( 'WooCommerce' ) && function_exists( 'WC' ) && WC()->cart ) {
			$count = (int) WC()->cart->get_cart_contents_count();
		}
		wp_send_json_success(
			array(
				'count' => $count,
			)
		);
	}
}
