<?php

namespace Limb_Chatbot\Includes\Admin;

use Limb_Chatbot\Includes\Data_Objects\Setting;
use Limb_Chatbot\Includes\Services\Dataset_Service;
use Limb_Chatbot\Includes\Traits\SingletonTrait;

/**
 * Class Admin
 *
 * Initializes the admin dashboard for the Limb Chatbot plugin.
 * Registers admin pages, scripts, styles, vector service hooks, and admin actions.
 *
 * @package Limb_Chatbot\Includes\Admin
 * @since 1.0.0
 */
class Admin {

	use SingletonTrait;

	/**
	 * Slug identifier for the parent admin menu.
	 *
	 * @since 1.0.0
	 * @var string
	 */
	static string $menu_slug = 'lbaic-dashboard';

	/**
	 * Settings page instance.
	 *
	 * @since 1.0.0
	 * @var Pages\Live_Agent\Page
	 */
	protected Pages\Live_Agent\Page $live_agent_page;

	/**
	 * Default chatbot page instance.
	 *
	 * @since 1.0.0
	 * @var Pages\Default_Chatbot\Page
	 */
	protected Pages\Default_Chatbot\Page $default_chatbot_page;

	/**
	 * Analytics page instance.
	 *
	 * @since 1.0.0
	 * @var Pages\Analytics\Page
	 */
	protected Pages\Analytics\Page $analytics_page;

	/**
	 * Dataset service.
	 *
	 * @since 1.0.0
	 * @var Dataset_Service
	 */
	private Dataset_Service $dataset_service;
	private Pages\Customers\Page $customers_page;
	private Pages\Settings\Page $settings_page;
	private Pages\Integrations\Page $integrations_page;

	/**
	 * Admin constructor.
	 * Initializes all admin page classes and vector service.
	 *
	 * @since 1.0.0
	 */
	public function __construct() {
		$this->live_agent_page      = new Pages\Live_Agent\Page();
		$this->default_chatbot_page = new Pages\Default_Chatbot\Page( $this->live_agent_page );
		$this->customers_page       = new Pages\Customers\Page( $this->live_agent_page );
		$this->analytics_page       = new Pages\Analytics\Page( $this->live_agent_page );
		$this->integrations_page    = new Pages\Integrations\Page( $this->live_agent_page );
		$this->settings_page        = new Pages\Settings\Page( $this->live_agent_page );
		$this->dataset_service      = new Dataset_Service();
	}

	/**
	 * Bootstraps the admin logic.
	 * Registers menu pages, actions, hooks, and initiates AJAX endpoints.
	 *
	 * @return void
	 * @since 1.0.0
	 *
	 */
	public function boot() {
		add_action( 'admin_menu', array( $this, 'add_options_page' ) );
		add_action( 'admin_init', array( Assets::class, 'admin_init' ) );
		add_action( 'transition_post_status', array( $this->dataset_service, 'post_object_created' ), 10, 3 );
		add_action( 'post_updated', array( $this->dataset_service, 'post_object_updated' ), 10, 3 );
		add_action( 'deleted_post', array( $this->dataset_service, 'post_object_deleted' ), 10, 2 );
		add_action( 'admin_notices', array( $this, 'limb_chatbot_free_activation_notice' ) );
		Ajax::init();
        add_filter( 'plugin_action_links', array( $this, 'add_action_links' ), 10, 4 );
		add_action( 'in_admin_header', function () {
			echo '<div id="lbaic-settings-headerbar" class="lbaic-settings"></div>';
		} );
	}

	/**
	 * Registers the top-level "Limb" admin menu and its submenu pages.
	 *
	 * @return void
	 * @since 1.0.0
	 *
	 */
	public function add_options_page(): void {
		$lbaic_icon = 'data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMzciIGhlaWdodD0iMzYiIHZpZXdCb3g9IjAgMCAzNyAzNiIgZmlsbD0ibm9uZSIgeG1sbnM9Imh0dHA6Ly93d3cudzMub3JnLzIwMDAvc3ZnIj4KPHBhdGggZD0iTTMxLjgwNjggMEg1LjE5MjE5QzIuMzI0NjUgMCAwIDIuMzQ4NjMgMCA1LjI0NTc2VjIzLjkxMzdDMCAyMy45NTY1IDAuMDAyMDE0NDMgMjMuOTk4MiAwLjAwMzAyMTY0IDI0LjA0MDlIMFYzNC45MjUyQzAgMzUuODMyOSAxLjA0NjUgMzYuMzMwNiAxLjczOTQ2IDM1Ljc1MTVMOS42MjM5MyAyOS4xNTk1SDMxLjgwNzhDMzQuNjc1MyAyOS4xNTk1IDM3IDI2LjgxMDkgMzcgMjMuOTEzN1Y1LjI0NTc2QzM3IDIuMzQ4NjMgMzQuNjc1MyAwIDMxLjgwNzggMEgzMS44MDY4Wk0yMi44MDk0IDE2LjMxODNDMjIuODA5NCAxNi43NTU5IDIyLjQ1ODkgMTcuMTEgMjIuMDI1OCAxNy4xMUg4LjQzNjQyVjE1LjM1OTdDOC40MzY0MiAxNC44NDc5IDguODQ2MzYgMTQuNDMzNyA5LjM1Mjk5IDE0LjQzMzdIMjIuODA5NFYxNi4zMTgzWk0yOC4zMjM5IDEwLjI2MzZDMjguMzIzOSAxMC43MDExIDI3Ljk3MzMgMTEuMDU1MyAyNy41NDAyIDExLjA1NTNIOC40MzY0MlY5LjMwNDk5QzguNDM2NDIgOC43OTMxMyA4Ljg0NjM2IDguMzc4OTYgOS4zNTI5OSA4LjM3ODk2SDI4LjMyNDlWMTAuMjYzNkgyOC4zMjM5WiIgZmlsbD0iIzlFQTNBOCIvPgo8L3N2Zz4K';

		add_menu_page( __( 'AI Chatbot', 'limb-chatbot' ), __( 'AI Chatbot', 'limb-chatbot' ), 'manage_options', self::$menu_slug, '', $lbaic_icon, 13 );

		$this->default_chatbot_page->add_submenu_page();
		$this->live_agent_page->add_submenu_page();
		$this->customers_page->add_submenu_page();
		$this->analytics_page->add_submenu_page();
		$this->integrations_page->add_submenu_page();
		$this->settings_page->add_submenu_page();
	}


	/**
	 * Show admin notice after deactivating the free version.
	 */
	function limb_chatbot_free_activation_notice() {
		if ( Setting::exists( 'lbaic.plugin.pro_deactivated' ) ) { ?>
            <div class="notice notice-info is-dismissible"><p><?php esc_html_e( 'Limb AI Chatbot (PRO version) has been deactivated because Limb AI Chatbot is now active.', 'limb-chatbot' ); ?></p></div><?php
			Setting::delete( [ 'key' => 'lbaic.plugin.pro_deactivated' ] );
		}
	}

	/**
	 * Add the settings link
	 *
	 * @param  array  $actions  Existing action links.
	 * @param  string  $plugin_file  Current plugin file.
	 * @param  array  $plugin_data  Plugin data.
	 * @param  string  $context  Context.
	 *
	 * @return array The array of action links.
	 */
	function add_action_links( $actions, $plugin_file, $plugin_data, $context ) {
		if ( $plugin_file !== plugin_basename( LIMB_CHATBOT_FILE ) ) {
			return $actions;
		}

		$settings_link = sprintf(
			'<a href="%s">%s</a>',
			esc_url( admin_url( 'admin.php?page=lbaic-dashboard&menu=chatbot&tab=dashboard' ) ),
			__( 'Settings', 'limb-chatbot' )
		);

		array_unshift( $actions, $settings_link );

		return $actions;
	}
}
