<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
 * Frontend enqueue for Navi AI Product Chat Bot.
 *
 * @package NAVIAIPR
 */

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

/**
 * Frontend assets class.
 */
class NAVIAIPR_Frontend
{

	/**
	 * Plugin options.
	 *
	 * @var array
	 */
	private $naviaipr_opts;

	/**
	 * Constructor.
	 *
	 * @param array $opts_arr Plugin options.
	 */
	public function __construct($opts_arr)
	{
		$this->naviaipr_opts = $opts_arr;
		add_action('wp_enqueue_scripts', array($this, 'naviaipr_public_enqueue_assets'));
	}




	/**
	 * Enqueue frontend styles and scripts.
	 */
	public function naviaipr_public_enqueue_assets()
	{
		if (is_admin()) {
			return;
		}

		$opts = $this->naviaipr_opts;
		$key_words_arr = explode(',', $opts['greeting_msg']);
		wp_enqueue_style('naviaipr-public-css', plugins_url('/css/navi-ai-product-chat-bot-public.css', __FILE__), array(), NAVIAIPR_VERSION);
		wp_enqueue_script('naviaipr-public-js', plugins_url('/js/navi-ai-product-chat-bot-public.js', __FILE__), array(), NAVIAIPR_VERSION, true);
		wp_register_style(
			'naviaipr-inline-css',
			false,
			array(),
			md5(wp_json_encode($opts))
		);
		wp_enqueue_style('naviaipr-inline-css');
		wp_add_inline_style('naviaipr-inline-css', wp_strip_all_tags(naviaipr_generate_dynamic_css($opts)));
		wp_localize_script(
			'naviaipr-public-js',
			'NaviaiprChat',
			array(
				'api_nonce' => wp_create_nonce('wp_rest'),
				'rest_url' => esc_url_raw(rest_url('naviaipr/v1/query')),
				'max_results' => intval($opts['max_results']),
				'bubble_image' => esc_url_raw($opts['bubble_image']),
				'chat_layout' => esc_js($opts['chat_layout']),
				'greeting_msg' => wp_kses_post(
					sprintf(
						'Hi there! 👋<br>I can help you find products. Try:<br>• "<span>%1$s</span>" - Search by name<br>• "<span>%2$s</span>" or "<span>$%2$s</span>" - Products at exact price<br>• "<span>%3$s</span>" - Protein at 60<br>• "<span>%4$s</span>" - Price range filter<br>What are you looking for?',
						esc_html(trim($key_words_arr[0] ?? '')),
						esc_html(trim($key_words_arr[1] ?? '')),
						esc_html(trim($key_words_arr[2] ?? '')),
						esc_html(trim($key_words_arr[3] ?? ''))
					)
				),
			)
		);
	}
}
