<?php
/**
 * Activity Guard – Real Time Notifier to Slack Enqueue.
 *
 * This class is used to enqueue ass assets.
 *
 * @package WPNTS\Inc
 */
namespace WPNTS\Inc;

use WPNTS\Inc\wpnts_nts_fs; // phpcs:ignore
use WPNTS\Inc\BaseController; // phpcs:ignore
use WPNTS\Inc\Database\DB; // phpcs:ignore

defined('ABSPATH') || die('Hey, what are you doing here? You silly human!');
/**
 * Enqueue Class is used to enqueue ass assets.
 *
 * @since 1.0.0
 */
class Enqueue extends BaseController {
	/**
	 * Register Instance.
	 *
	 * @since  1.0.0
	 */
	public function register() {
		add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue' ] );
		add_action( 'wp_enqueue_scripts', [ $this, 'frondend_enqueue' ] );
	}
	/**
	 * This admin_enqueue function is used to enqueue.
	 *
	 * @param string $screen used to show the current screen path.
	 * @since  1.0.0
	 */
	public function admin_enqueue( $screen ) {

		 /**
		 * Banner content & notices.
		 */
		$pages = [ 'toplevel_page_notifier-to-slack', 'edit.php', 'plugins.php', 'index.php' ];

		if ( ! in_array($screen, $pages) ) {
			wp_enqueue_style( 'wpnts_noice_prevent_css_style', WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/prevent-notice.css',[], time(),'all' );
			return;
		}

		if ( 'edit.php' !== $screen || 'product' === get_current_screen()->post_type || in_array($screen, $pages) ) {
			wp_enqueue_style( 'wpnts_noice_css_style', WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/notice.css',[], time(),'all' );
		}

		/**
		 * WPNTS admin-all screen loaded file.
		 */
		if ( 'toplevel_page_notifier-to-slack' === $screen || 'notifier_page_notifier-to-slack_setting' === $screen || 'notifier_page_notifier-to-slack_authors' === $screen ) {

			remove_all_actions( 'admin_notices' );
			remove_all_actions( 'all_admin_notices' );

			// Enqueue Freemius Checkout script.
			wp_enqueue_script('freemius_checkout', '//checkout.freemius.com/checkout.min.js', [ 'jquery' ], '1.0.0', true);

			wp_enqueue_style( 'wpnts_main_scss_style', WP_NOTIFIER_TO_SLACK_DIR_URL . 'build/index.css',[], time(),'all' );
			/**
			 * Admin global CSS.
			 */
			wp_enqueue_style(
				'wpnts-admin-css',
				WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/admin.css',
				'',
				time(),
				'all'
			);

			wp_enqueue_script(
				'wpnts-admin-js',
				WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/admin.js',
				[ 'jquery' ],
				time(),
				true
			);

			/**
			 * Main Script enqueue here.
			 */

			$wpnts_db_instance = new DB();
			$dashboard_data = $wpnts_db_instance->get_all();
			$get_all_activity_logs = $wpnts_db_instance->get_all_activity_logs();
			$formflow_status = $wpnts_db_instance->formflow_status();
			$woocommercewoocommerce_status = $wpnts_db_instance->woocommercewoocommerce_status();
			$cf7_status = $wpnts_db_instance->cf7_status();
			$is_pro_active = $wpnts_db_instance->is_pro_active();
			$debug_mode_status = $wpnts_db_instance->debug_mode_status();
			$maintenannotice_mode_status = $wpnts_db_instance->maintenannotice_mode_status();
			$global_settings = $wpnts_db_instance->global_settings();
			$get_current_user_id = $wpnts_db_instance->get_current_user_id();
			$visitor_data = $wpnts_db_instance->visitor_data();
			$notice_settings = $wpnts_db_instance->notice_settings();
			$admin_list = $wpnts_db_instance->admin_list();
			$system_info = $wpnts_db_instance->system_info();

			$get_exclusion_option = $wpnts_db_instance->get_exclusion_option();
			$get_core_file_directories = $wpnts_db_instance->get_core_file_directories();
			$get_installed_plugins = $wpnts_db_instance->get_installed_plugins();
			$get_installed_themes = $wpnts_db_instance->get_installed_themes();

			wp_enqueue_script( 'wpnts_min_js', $this->plugin_url . 'build/index.js',[ 'jquery', 'wp-element', 'wp-util' ], time(),true );
			wp_localize_script('wpnts_min_js', 'appLocalizer', [
				'nonce' => wp_create_nonce( 'wp_rest'),
				'wpntsUrl' => home_url( '/wp-json' ),
				'homeUrl' => esc_url(admin_url()),
				'admin_ajax' => esc_url( admin_url( 'admin-ajax.php' ) ),
				'dashboard_data'  => $dashboard_data,
				'isPro'  => $is_pro_active,
				'formflow_status'  => $formflow_status,
				'isWooactive'  => $woocommercewoocommerce_status,
				'cf7_status'  => $cf7_status,
				'debugMode'  => $debug_mode_status,
				'maintenannoticeMode'  => $maintenannotice_mode_status,
				'globalhook'  => $global_settings,
				'get_current_user_id'  => $get_current_user_id,
				'visitor_data'  => $visitor_data,
				'notice_settings'  => $notice_settings,
				'admin_list'  => $admin_list,
				'system_info'  => $system_info,

				'get_exclusion_option'  => $get_exclusion_option,
				'get_core_file_directories'  => $get_core_file_directories,
				'get_installed_plugins'  => $get_installed_plugins,
				'get_installed_themes'  => $get_installed_themes,

				'get_all_activity_logs'  => $get_all_activity_logs,
				'ran_setup_wizard' => wp_validate_boolean( get_option( 'notifier_ran_setup_wizard', false ) ),
			] );
			wp_enqueue_script('wpnts_min_js');

		}
	}

	/**
	 * WPNTS public-all screen loaded file.
	 */
	public function frondend_enqueue() {
			// Only load visitor tracking if enabled in Analytics settings
			$analytics_settings = get_option('wpnts_analytics_settings', '{}');
			$settings = json_decode($analytics_settings, true);
			
			$visitor_tracking_value = isset($settings['visitors_traffic_list']) ? $settings['visitors_traffic_list'] : true;

			// Convert to boolean: true, 1, "1" = enabled; false, 0, "", null = disabled
			$visitor_tracking_enabled = filter_var($visitor_tracking_value, FILTER_VALIDATE_BOOLEAN);

			if ($visitor_tracking_enabled) {
				/**
				 * Lightweight visitor tracking script
				 */
				wp_enqueue_script( 
					'wpnts_visitor_tracking', 
					WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/visitor.js',
					[], 
					WP_NOTIFIER_TO_SLACK_VERSION,
					true 
				);
				
				wp_localize_script('wpnts_visitor_tracking', 'wpntsVisitorTracking', [
					'ajaxUrl' => admin_url('admin-ajax.php'),
					'nonce' => wp_create_nonce('wpnts_visitor_tracking'),
				]);
			}

			// Load maintenance form script only when both maintenance mode and contact form are active
			$maintenance_settings = get_option('wpnts_schedules_maintenannotice_settings', '{}');
			$maintenance_config = json_decode($maintenance_settings, true);
			
			$maintenance_mode_enabled = isset($maintenance_config['maintenance_mode']) ? filter_var($maintenance_config['maintenance_mode'], FILTER_VALIDATE_BOOLEAN) : false;
			$contact_form_enabled = isset($maintenance_config['show_maintenance_contact_form']) ? filter_var($maintenance_config['show_maintenance_contact_form'], FILTER_VALIDATE_BOOLEAN) : false;

			// Only load maintenance form script when both conditions are met
			if ($maintenance_mode_enabled && $contact_form_enabled) {
				wp_enqueue_script( 
					'wpnts_maintenance_form', 
					WP_NOTIFIER_TO_SLACK_DIR_URL . 'assets/maintenance-form.js',
					[], 
					WP_NOTIFIER_TO_SLACK_VERSION,
					true 
				);
			}
	}
}
