<?php
/**
 * Admin Chat Box Enqueue
 *
 * This class is used to enqueue assets
 *
 * @package ACB\Inc
 */
namespace ACB\Inc;

use \ACB\Inc\ACB_BaseController;

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');

/**
 * ACB_Enqueue Class is used to enqueue assets.
 *
 * @since 1.0.0
 */
class ACB_Enqueue extends ACB_BaseController {
	
	/**
	 * Register Instance.
	 *
	 * @since  1.0.0
	 */
	public function register() {
		add_action( 'admin_enqueue_scripts', [ $this, 'ACB_admin_enqueue' ] );
	}

	/**
	 * This ACB_admin_enqueue function is used to enqueue.
	 *
	 * @param string $screen used to show the current screen path.
	 * @since  1.0.0
	 */
	public function ACB_admin_enqueue( $screen ) {

		/**
		 * Chat Dashboard - Available to all logged-in users (read capability)
		 */
		if ( 'toplevel_page_admin_chat_box' == $screen ) {
			$this->enqueue_common_styles();
			$this->enqueue_main_dashboard_scripts();
		}

		/**
		 * Users page specific scripts - Only for administrators
		 */
		if ( 'admin-chat-box_page_admin_chat_box_users' == $screen ) {
			// Check if user has manage_options capability (administrator)
			if ( current_user_can( 'manage_options' ) ) {
				$this->enqueue_common_styles();
				$this->enqueue_users_scripts();
			} else {
				// Redirect non-administrators away from users page
				wp_redirect( admin_url( 'admin.php?page=admin_chat_box' ) );
				exit;
			}
		}

		/**
		 * Settings page specific scripts - Only for administrators
		 */
		if ( 'admin-chat-box_page_admin_chat_box_setting' == $screen ) {
			// Check if user has manage_options capability (administrator)
			if ( current_user_can( 'manage_options' ) ) {
				$this->enqueue_common_styles();
				$this->enqueue_settings_scripts();
			} else {
				// Redirect non-administrators away from settings page
				wp_redirect( admin_url( 'admin.php?page=admin_chat_box' ) );
				exit;
			}
		}
	}

	/**
	 * Enqueue common styles used across all pages
	 *
	 * @since 1.0.0
	 */
	private function enqueue_common_styles() {
		wp_enqueue_style( 'acb_main_scss_style', $this->plugin_url . 'build/style-index.css', [], '1.0.0', 'all' );
		wp_enqueue_style( 'font-awesome', $this->plugin_url . 'assets/library/all.min.css', [], '5.15.4' );
	}


	/**
	 * Enqueue main dashboard scripts with localized data
	 *
	 * @since 1.0.0
	 */
	private function enqueue_main_dashboard_scripts() {
		wp_enqueue_script( 'acb_min_js', $this->plugin_url . 'build/index.js', [ 'jquery', 'wp-element' ], '1.0.0', true );
		$config = get_option( 'firebaseConfig', [] );
		// Keep the same variable names for JavaScript compatibility
		wp_localize_script('acb_min_js', 'appLocalizer', [
			'apiUrl' => home_url( '/wp-json' ),
			'nonce' => wp_create_nonce('acb_nonce'),
			'adminAjax' => admin_url('admin-ajax.php'),
			'config' => $config,
		]);
	}

	/**
	 * Enqueue users page scripts with localized data
	 *
	 * @since 1.0.0
	 */
	private function enqueue_users_scripts() {
		wp_enqueue_script( 'acb_users_js', $this->plugin_url . 'build/users-index.js', [ 'jquery', 'wp-element' ], '1.0.0', true );
		$config = get_option( 'firebaseConfig', [] );
		// Keep the same variable names for JavaScript compatibility
		wp_localize_script('acb_users_js', 'appLocalizer', [
			'apiUrl' => home_url( '/wp-json' ),
			'nonce' => wp_create_nonce('acb_nonce'),
			'adminAjax' => admin_url('admin-ajax.php'),
			'config' => $config,
		]);

	}

	/**
	 * Enqueue settings page scripts with localized data
	 *
	 * @since 1.0.0
	 */
	private function enqueue_settings_scripts() {
		// Note: You need to enqueue the settings script first
		wp_enqueue_script( 'acb_settings_js', $this->plugin_url . 'build/settings-index.js', [ 'jquery', 'wp-element' ], '1.0.0', true );
		$config = get_option( 'firebaseConfig', [] );
		// Keep the same variable names for JavaScript compatibility
		wp_localize_script('acb_settings_js', 'appLocalizer', [
			'apiUrl' => home_url( '/wp-json' ),
			'nonce' => wp_create_nonce('acb_nonce'),
			'adminAjax' => admin_url('admin-ajax.php'),
			'config' => $config,
		]);
	}
}