<?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.min.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 );
		}
		$custom_css = 'ul.wp-submenu span.lbaic-badge-premium::after, #wpadminbar span.lbaic-badge-premium::after {content:"' . \__( 'Pro', 'limb-chatbot' ) . '";}' . \PHP_EOL;
		$custom_css .= 'ul.wp-submenu span.lbaic-badge-premium, #wpadminbar span.lbaic-badge-premium {display:inline-flex;align-items:center;justify-content:center;margin-left:6px;padding:0 6px;min-height:16px;border-radius:999px;background-color:#8c45f6;color:#ffffff;font-size:10px;font-weight:600;line-height:1;letter-spacing:.02em;vertical-align:middle;}' . \PHP_EOL;
		wp_register_style( 'lbaic-admin-global', false, array(), LIMB_CHATBOT_VERSION );
		wp_enqueue_style( 'lbaic-admin-global' );
		wp_add_inline_style( 'lbaic-admin-global', $custom_css );
	}

	/**
	 * 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(
			'hard_to_setup'   => array(
				'label'       => __( 'It\'s too hard to set up', 'limb-chatbot' ),
				'sub_reasons' => array(
					'ai_training_confusing'      => __( 'AI training was confusing', 'limb-chatbot' ),
					'widgets_confusing'          => __( 'Widgets was confusing', 'limb-chatbot' ),
					'other'                      => __( 'Other', 'limb-chatbot' ),
				),
			),
			'poor_design'     => array(
				'label'       => __( 'The design/UI looks "poor"', 'limb-chatbot' ),
				'sub_reasons' => array(
					'outdated_fonts_buttons'  => __( 'The fonts and buttons look outdated/cheap.', 'limb-chatbot' ),
					'confusing_dashboard'     => __( 'The dashboard is confusing to navigate.', 'limb-chatbot' ),
					'chat_blocks_button'      => __( 'The chat window blocks my \'Add to Cart\' button.', 'limb-chatbot' ),
					'other'                   => __( 'Other', 'limb-chatbot' ),
				),
			),
			'broke_site'      => array(
				'label'       => __( 'It broke something on my site', 'limb-chatbot' ),
				'sub_reasons' => array(
					'conflicts_with_theme'    => __( 'It conflicts with my theme', 'limb-chatbot' ),
					'no_mobile_widget'        => __( 'The chat widget doesn\'t show up on mobile.', 'limb-chatbot' ),
					'crashed_checkout'        => __( 'It crashed my checkout page.', 'limb-chatbot' ),
					'other'                   => __( 'Other', 'limb-chatbot' ),
				),
			),
			'found_better'    => array(
				'label'       => __( 'I found a better plugin', 'limb-chatbot' ),
				'sub_reasons' => array(
					'tidio'        => __( 'Tidio', 'limb-chatbot' ),
					'chatbot_com'  => __( 'ChatBot.com', 'limb-chatbot' ),
					'amio'         => __( 'Amio', 'limb-chatbot' ),
					'other'        => __( 'Other', 'limb-chatbot' ),
				),
			),
			'needed_briefly'  => array(
				'label'       => __( 'I only needed it for a moment', 'limb-chatbot' ),
				'sub_reasons' => array(),
			),
		);

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

