<?php
/**
 * Agents Page Class
 *
 * @package SAAI\Classes
 */

namespace SAAI\Classes;

use SAAI\Abstracts\Admin_Page;
use SAAI\Helpers\Connect;
use SAAI\Traits\Singleton_Trait;

/**
 * Agents_Page class.
 *
 * @since 1.0.0
 */
class Agents_Page extends Admin_Page {

    use Singleton_Trait;

    /**
     * Holds the class instance object.
     *
     * @var Agents_Page $instance
     * @since 1.0.0
     */
    protected static $instance;

    /**
     * Initialize the admin page.
     *
     * @since 1.0.0
     * @return void
     */
    protected function init() {
        $this->page_title  = __( 'AI Agents', 'storeagent-ai-for-woocommerce' );
        $this->menu_title  = __( 'AI Agents', 'storeagent-ai-for-woocommerce' );
        $this->capability  = 'manage_options';
        $this->menu_slug   = 'storeagent-agents';
        $this->template    = 'settings-page.php';
        $this->parent_slug = Store_Agent_AI_Admin_Page::MENU_SLUG;
        $this->position    = 10; // Position below Dashboard.
    }

    /**
     * Highlight active admin menu.
     *
     * @param string $parent_file Parent file.
     *
     * @return string
     */
    public function highlight_active_admin_menu( $parent_file ) {
        global $submenu_file, $pagenow;

        $definition = array(
            'page' => FILTER_SANITIZE_FULL_SPECIAL_CHARS,
        );
        $qs         = filter_input_array( INPUT_GET, $definition );

        // This is the Agents page.
        if ( 'admin.php' === $pagenow && 'storeagent-agents' === $qs['page'] ) {
            $submenu_file = 'storeagent-agents'; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
            $parent_file  = Store_Agent_AI_Admin_Page::MENU_SLUG;
        }

        return $parent_file;
    }

    /**
     * Add hook handlers for rendering the admin page.
     *
     * @since 1.0.0
     * @return void
     */
    public function run() {
        if ( ! Connect::is_connected() ) {
            /***************************************************************************
             * Bail early
             ***************************************************************************
             *
             * We don't want to run the admin page if the store is not connected to
             * the StoreAgent API service.
             */
            return;
        }

        parent::run();
        add_filter( 'parent_file', array( $this, 'highlight_active_admin_menu' ) );
    }
}
