<?php
/**
 * Assets Manager class for managing plugin assets
 *
 * This class handles all asset management for the Ask Any plugin,
 * including styles, scripts, and localization.
 *
 * @package Askany
 * @since 1.0.0
 */

namespace Askany;

use ask\askany\wpnts_nts_fs;

/**
 * Assets Manager class for managing plugin assets
 *
 * @package Askany
 * @since 1.0.0
 */
class AssetsManager {
	/**
	 * Plugin URL.
	 *
	 * @var string
	 */
	private $plugin_url;
	/**
	 * Plugin version.
	 *
	 * @var string
	 */
	private $version;

	/**
	 * Constructor for AssetsManager.
	 *
	 * Initializes the plugin URL and version.
	 */
	public function __construct() {
		$this->plugin_url = ASKANY_PLUGIN_URL;
		$this->version = ASKANY_VERSION;
	}

	/**
	 * Initialize the AssetsManager.
	 *
	 * This method hooks into WordPress to enqueue frontend styles and scripts.
	 */
	public function init() {
		add_action('wp_enqueue_scripts', array( $this, 'enqueue_frontend_assets' ));
	}

	/**
	 * Enqueue frontend assets.
	 *
	 * This method enqueues the necessary styles and scripts for the frontend.
	 * It also localizes the script with necessary data for AJAX requests.
	 *
	 * @return void
	 */
	public function enqueue_frontend_assets() {
		// Enqueue CSS.
		wp_enqueue_style(
			'askany-frontend',
			$this->plugin_url . 'dist/css/frontend.min.css',
			array(),
			$this->version
		);
		wp_enqueue_style(
			'askany-frontend-news',
			$this->plugin_url . 'dist/css/frontend-news.min.css',
			array(),
			$this->version
		);

		// Enqueue encryption script first (dependency for frontend script)
		wp_enqueue_script(
			'askany-encryption',
			$this->plugin_url . 'dist/js/encryption.min.js',
			array(),
			$this->version,
			true
		);

		// Enqueue markdown parser
		wp_enqueue_script(
			'askany-markdown',
			$this->plugin_url . 'dist/js/markdown-parser.min.js',
			array(),
			$this->version,
			true
		);

		// Enqueue SSE client script
		wp_enqueue_script(
			'askany-sse-client',
			$this->plugin_url . 'dist/js/sse-client.min.js',
			array(),
			$this->version,
			true
		);

		// Enqueue main frontend JavaScript.
		wp_enqueue_script(
			'askany-frontend',
			$this->plugin_url . 'dist/js/frontend.min.js',
			array( 'jquery', 'askany-encryption', 'askany-markdown', 'askany-sse-client' ),
			$this->version,
			true
		);

		// Localize script.
		$current_user = wp_get_current_user();
		$assistant_avatar = get_option('askany_assistant_avatar', '');
		
		// Get user avatar HTML if logged in
		$user_avatar_html = '';
		if ( is_user_logged_in() ) {
			$user_avatar_html = get_avatar( $current_user->ID, 32, '', '', array( 'class' => 'avatar-image' ) );
		}
		
		wp_localize_script(
			'askany-frontend',
			'askanyData',
			array(
				'ajaxUrl' => admin_url('admin-ajax.php'),
				'nonce' => wp_create_nonce('askany_chat_nonce'),
				'apiEndpoint' => rest_url('askany/v1/chat'),
				'isLoggedIn' => is_user_logged_in(),
				'currentUserId' => get_current_user_id(),
				'currentUserName' => $current_user->display_name ?: $current_user->user_login,
				'currentUserLogin' => $current_user->user_login,
				'userAvatar' => $user_avatar_html,
				'assistantAvatar' => esc_url( $assistant_avatar ),
				'encryptionEnabled' => true, // Flag to indicate encryption is available
				'typingAnimationEnabled' => get_option('askany_typing_animation_enabled', false),
				'typingSpeed'            => get_option( 'askany_typing_speed', 'regular' ),
				'reportIssueEnabled' => get_option('askany_report_issue_enabled', true),
				'featureRequestEnabled' => get_option('askany_feature_request_enabled', true),
				'reportIssueMessage' => get_option('askany_report_issue_message', __('Hey 👋 Found a bug? Please describe the issue below. Also, please share your name and email so we can follow up with you.', 'askany')),
				'featureRequestMessage' => get_option('askany_feature_request_message', __('What feature would you like to see next? Share your name, email and feature details so we can consider it for future updates.', 'askany')),
				'i18n' => array(
					'sending' => __('Sending...', 'askany'),
					'error' => __('Error occurred. Please try again.', 'askany'),
					'emptyMessage' => __('Please enter a message.', 'askany'),
				),
			)
		);
	}
}
