<?php

namespace Limb_Chatbot\Includes\Admin;

use Limb_Chatbot\Includes\Services\Helper;
use Limb_Chatbot\Includes\Traits\SingletonTrait;

/**
 * Class Assets
 *
 * Handles admin asset registration (CSS/JS) for the Limb Chatbot plugin.
 * Uses WordPress hooks to enqueue styles, scripts, and editor assets.
 *
 * @package Limb_Chatbot\Includes\Admin
 * @since 1.0.0
 */
class Assets {

	use SingletonTrait;

	/**
	 * Initialize admin-related asset hooks.
	 *
	 * Registers all action hooks related to enqueuing scripts/styles
	 * in the WordPress admin and block editor.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public static function admin_init() {
		$instance = self::instance();
		add_action( 'admin_enqueue_scripts', array( $instance, 'styles' ) );
		add_action( 'admin_enqueue_scripts', array( $instance, 'scripts' ) );
		add_action( 'enqueue_block_editor_assets', array( $instance, 'block_editor_scripts' ) );
		add_action( 'admin_footer', array( $instance, 'add_icons_utilities_to_admin' ) );
		add_action( 'admin_footer', array( $instance, 'render_deactivation_feedback_modal' ) );
	}

	/**
	 * Enqueue block editor assets (Gutenberg).
	 *
	 * Empty for now. Can be extended to load block scripts/styles in the editor.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function block_editor_scripts() {
	}

	/**
	 * Enqueue admin stylesheets.
	 *
	 * Intended to register and enqueue plugin-specific CSS for the admin panel.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function styles() {
		if ( $this->is_plugins_screen() ) {
			wp_enqueue_style(
				'lbaic-deactivation-feedback-style',
				Limb_Chatbot()->get_plugin_dir_url() . 'dist/css/admin/lbaic-deactivation-feedback.css',
				array(),
				LIMB_CHATBOT_VERSION
			);
		}
	}

	/**
	 * Enqueue admin JavaScript files.
	 *
	 * Intended to register and enqueue plugin-specific JS for the admin panel.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function scripts() {
		if ( $this->is_plugins_screen() ) {
			$script_asset_path = Limb_Chatbot()->get_plugin_dir_path() . 'dist/js/admin/lbaic-deactivation-feedback.asset.php';
			$script_asset      = file_exists( $script_asset_path )
				? require $script_asset_path
				: array(
					'dependencies' => array( 'jquery' ),
					'version'      => LIMB_CHATBOT_VERSION,
				);

			wp_enqueue_script(
				'lbaic-deactivation-feedback-script',
				Limb_Chatbot()->get_plugin_dir_url() . 'dist/js/admin/lbaic-deactivation-feedback.js',
				$script_asset['dependencies'],
				$script_asset['version'],
				true
			);

			$localize_data = array(
				'pluginBasename' => plugin_basename( LIMB_CHATBOT_FILE ),
				'siteUrl'        => home_url(),
				'siteMode'       => Helper::get_site_mode(),
				'restUrl'        => rest_url( 'limb/chatbot/v1/' ),
				'nonce'          => wp_create_nonce( 'wp_rest' ),
				'i18n'           => array(
					'skipDeactivate'   => __( 'Skip & Deactivate', 'limb-chatbot' ),
					'submitDeactivate' => __( 'Submit & Deactivate', 'limb-chatbot' ),
				),
			);

			wp_localize_script( 'lbaic-deactivation-feedback-script', 'lbaicDeactivation', $localize_data );
		}
	}

	/**
	 * Output the chatbot icons utilities SVG in admin pages.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function add_icons_utilities_to_admin() {
		// Check if we're on the right admin pages
		$should_load_icons = false;

		// Limb dashboard admin page
		if ( isset( $_GET['page'] ) && sanitize_text_field( wp_unslash( $_GET['page'] ) ) === 'lbaic-dashboard' ) {
			$should_load_icons = true;
		}

		// Load the icons utilities
		if ( $should_load_icons ) {
			$plugin_dir_path = Limb_Chatbot()->get_plugin_dir_path();

			include $plugin_dir_path . 'templates/chatbot/lbaic-chatbot-icons.php';
		}
	}

	/**
	 * Check if current page is the plugins page.
	 *
	 * @since 1.0.0
	 * @return bool
	 */
	private function is_plugins_screen(): bool {
		// Check if we are in the admin area.
		if ( ! is_admin() ) {
			return false;
		}

		// Handle case where function is called before get_current_screen() is available.
		if ( ! function_exists( 'get_current_screen' ) ) {
			// Alternative check: look at the current admin page URL.
			global $pagenow;
			return 'plugins.php' === $pagenow;
		}

		// Get the current screen.
		$current_screen = get_current_screen();

		// Check if $current_screen is a valid object before accessing its properties.
		if ( ! is_object( $current_screen ) ) {
			return false;
		}

		// Get the current screen ID.
		$screen_id = $current_screen->id;

		// Check for both regular and network admin plugins screens.
		return ! empty( $screen_id ) && ( 'plugins' === $screen_id || 'plugins-network' === $screen_id );
	}

	/**
	 * Render the deactivation feedback modal on plugins page.
	 *
	 * @since 1.0.0
	 * @return void
	 */
	public function render_deactivation_feedback_modal() {
		if ( ! $this->is_plugins_screen() ) {
			return;
		}

		$reasons = array(
			'just_testing'    => __( 'Just testing', 'limb-chatbot' ),
			'no_value'        => __( 'Didn’t see value quickly', 'limb-chatbot' ),
			'confusing_setup' => __( 'Setup was confusing', 'limb-chatbot' ),
			'not_expected'    => __( 'Not what I expected', 'limb-chatbot' ),
			'missing_feature' => __( 'Missing a feature', 'limb-chatbot' ),
			'performance'     => __( 'Performance or compatibility concerns', 'limb-chatbot' ),
			'other'           => __( 'Other (optional)', 'limb-chatbot' ),
		);

		$plugin_dir_path = Limb_Chatbot()->get_plugin_dir_path();
		include $plugin_dir_path . 'templates/admin/lbaic-deactivation-feedback-modal.php';
	}
}

