<?php

if (!defined('ABSPATH')) { exit; }

if (class_exists('AIEO_Command_Palette')) { return; }

class AIEO_Command_Palette {

    const HANDLE_JS  = 'aieo-command-palette';
    const HANDLE_CSS = 'aieo-command-palette';

    public static function init() {
        
        
        
        
        
        
        return;
        
        if (!is_admin()) { return; }
        add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue'));
    }

    
    private static function is_aieo_admin_page() {
        if (!isset($_GET['page'])) { return false; }
        $page = sanitize_key((string) wp_unslash($_GET['page']));
        return $page !== '' && strpos($page, 'ai-eshop-') === 0;
    }

    public static function enqueue() {
        if (!self::is_aieo_admin_page()) { return; }
        if (!current_user_can(defined('AIEO_CAPABILITY') ? AIEO_CAPABILITY : 'edit_pages')) {
            return;
        }

        
        
        
        $css_path = AIEO_DIR . 'assets/css/aieo-command-palette.css';
        $js_path  = AIEO_DIR . 'assets/js/aieo-command-palette.js';
        $css_ver  = file_exists($css_path) ? (string) filemtime($css_path) : AIEO_VERSION;
        $js_ver   = file_exists($js_path)  ? (string) filemtime($js_path)  : AIEO_VERSION;

        wp_register_style(
            self::HANDLE_CSS,
            AIEO_URI . 'assets/css/aieo-command-palette.css',
            array(),
            $css_ver
        );
        wp_register_script(
            self::HANDLE_JS,
            AIEO_URI . 'assets/js/aieo-command-palette.js',
            array(),
            $js_ver,
            true
        );

        wp_localize_script(self::HANDLE_JS, 'aieoCommandPalette', array(
            'commands' => self::build_commands(),
            'i18n'     => array(
                'placeholder' => __('Search AIEO actions… (try "surveys", "discount", "recompute")', 'ai-eshop-optimizer'),
                'noResults'   => __('No matches.', 'ai-eshop-optimizer'),
                'hint'        => __('↑/↓ to navigate · Enter to open · Esc to close', 'ai-eshop-optimizer'),
            ),
        ));

        wp_enqueue_style(self::HANDLE_CSS);
        wp_enqueue_script(self::HANDLE_JS);
    }

    
    private static function build_commands() {
        $commands = array();

        
        $commands[] = array(
            'name'     => 'aieo/analytics',
            'label'    => __('AIEO Analytics', 'ai-eshop-optimizer'),
            'context'  => 'AIEO',
            'url'      => admin_url('admin.php?page=ai-eshop-optimizer'),
            'keywords' => 'home dashboard analytics',
        );
        $commands[] = array(
            'name'     => 'aieo/ai-chat',
            'label'    => __('AI Chat', 'ai-eshop-optimizer'),
            'context'  => 'AIEO',
            'url'      => admin_url('admin.php?page=ai-eshop-chat'),
            'keywords' => 'chat assistant claude',
        );

        
        
        
        if (class_exists('AIEO_DMM')) {
            $dmm  = AIEO_DMM::instance();
            $base = admin_url('admin.php?page=' . AIEO_DMM::MENU_SLUG);

            
            
            $group_labels = array();
            foreach ($dmm->get_group_definitions() as $g) {
                $group_labels[$g['id']] = wp_strip_all_tags($g['label']);
            }

            foreach ($dmm->get_subtabs() as $tab) {
                $gid     = isset($tab['group']) ? $tab['group'] : null;
                $context = $gid && isset($group_labels[$gid])
                    ? $group_labels[$gid]
                    : __('Discounts & Marketplaces', 'ai-eshop-optimizer');

                $commands[] = array(
                    'name'     => 'aieo/dmm/' . sanitize_title($tab['id']),
                    'label'    => (string) $tab['label'],
                    'context'  => $context,
                    'url'      => $base . '&dmm_tab=' . rawurlencode($tab['id']),
                    'keywords' => $tab['id'],
                );
            }

            
            
            
            
            $commands[] = array(
                'name'     => 'aieo/action/surveys-recompute',
                'label'    => __('Recompute survey intelligence + k-NN', 'ai-eshop-optimizer'),
                'context'  => __('Customer Profiling', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=surveys#recompute',
                'keywords' => 'recompute knn extract priorities surveys',
            );
            $commands[] = array(
                'name'     => 'aieo/action/surveys-ingest',
                'label'    => __('Ingest GF surveys (F18 + F20)', 'ai-eshop-optimizer'),
                'context'  => __('Customer Profiling', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=surveys#ingest',
                'keywords' => 'ingest gravity forms gf surveys',
            );
            $commands[] = array(
                'name'     => 'aieo/action/webpush-populate-ci',
                'label'    => __('Populate Customer Intelligence (Web Push)', 'ai-eshop-optimizer'),
                'context'  => __('Customer Profiling', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=web-push#recompute-ci',
                'keywords' => 'populate ci customer intelligence webpush recompute',
            );
            
            
            
            $commands[] = array(
                'name'     => 'aieo/action/utm-explorer',
                'label'    => __('UTM Explorer (campaign drill-down)', 'ai-eshop-optimizer'),
                'context'  => isset($group_labels['attribution']) ? $group_labels['attribution'] : __('Attribution', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=roi_wizard',
                'keywords' => 'utm explorer source medium campaign term content drill down cascade campaign tags searchable multi-select attribution',
            );
            $commands[] = array(
                'name'     => 'aieo/action/roi-comparison',
                'label'    => __('ROI Wizard — period comparison & ad-spend ROI', 'ai-eshop-optimizer'),
                'context'  => isset($group_labels['attribution']) ? $group_labels['attribution'] : __('Attribution', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=roi_wizard',
                'keywords' => 'roi roas return on ad spend ledger reco lift per channel period comparison previous period same period last year year over year yoy creative campaign channel offer',
            );
            
            
            
            $commands[] = array(
                'name'     => 'aieo/action/product-layout-order',
                'label'    => __('Product page layout & order (positioning)', 'ai-eshop-optimizer'),
                'context'  => isset($group_labels['product_display']) ? $group_labels['product_display'] : __('Storefront Display', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=product_display#aieo-card-display-order',
                'keywords' => 'position positioning layout order reorder product page title price add to cart brand under image sku barcode categories rating short description placement display order drag drop',
            );
            
            
            $commands[] = array(
                'name'     => 'aieo/action/abandoned-carts',
                'label'    => __('Saved & abandoned carts', 'ai-eshop-optimizer'),
                'context'  => isset($group_labels['catalog_sync']) ? $group_labels['catalog_sync'] : __('Catalog Sync', 'ai-eshop-optimizer'),
                'url'      => $base . '&dmm_tab=saved_abandoned_carts#aieo-card-saved-abandoned-carts',
                'keywords' => 'abandoned cart saved cart recovery checkout draft lost revenue conversion shared cart resume basket most abandoned products lost demand',
            );
            
            
            $cp_ctx = isset($group_labels['customer_profiling']) ? $group_labels['customer_profiling'] : __('Customer Profiling', 'ai-eshop-optimizer');
            $commands[] = array(
                'name'     => 'aieo/action/user-vitals',
                'label'    => __('User Vitals — ERP migration', 'ai-eshop-optimizer'),
                'context'  => $cp_ctx,
                'url'      => $base . '&dmm_tab=user_vitals',
                'keywords' => 'user extension erp soft1 vitals pods migration customer turnover loyalty points bonus profile fields',
            );
            $commands[] = array(
                'name'     => 'aieo/action/professional-signup',
                'label'    => __('Professional signup (Gravity Forms)', 'ai-eshop-optimizer'),
                'context'  => $cp_ctx,
                'url'      => $base . '&dmm_tab=user_vitals#aieo-gf-save',
                'keywords' => 'professional signup registration gravity forms membership_gf pro student roles epaggelmaties spoudastes',
            );
            $commands[] = array(
                'name'     => 'aieo/action/loyalty-connection',
                'label'    => __('Loyalty connection (Bonus Card)', 'ai-eshop-optimizer'),
                'context'  => $cp_ctx,
                'url'      => $base . '&dmm_tab=user_vitals#aieo-loy-save',
                'keywords' => 'loyalty bonus card connection identification points soft1 customer_loyalty aieo_loyalty_information mybonus',
            );
            
            $pricing_ctx = __('Pricing & Offers', 'ai-eshop-optimizer');
            $commands[] = array(
                'name'     => 'aieo/action/omnibus-price-history',
                'label'    => __('EU Omnibus — Price History', 'ai-eshop-optimizer'),
                'context'  => $pricing_ctx,
                'url'      => $base . '&dmm_tab=price_history',
                'keywords' => 'omnibus price history lowest 30 day prior price reduction backfill cron schedule reports xlsx most changes',
            );
            $commands[] = array(
                'name'     => 'aieo/action/lowest-price-message',
                'label'    => __('Lowest-price (sale) message', 'ai-eshop-optimizer'),
                'context'  => $pricing_ctx,
                'url'      => $base . '&dmm_tab=brand_discounts',
                'keywords' => 'lowest price sale message omnibus on-sale prior price block lowest_price reduced',
            );
            $commands[] = array(
                'name'     => 'aieo/action/role-pricing-format',
                'label'    => __('Role / pro pricing & price formatting', 'ai-eshop-optimizer'),
                'context'  => $pricing_ctx,
                'url'      => $base . '&dmm_tab=role_pricing',
                'keywords' => 'role pricing pro price professional discount price formatting strikethrough sale display badge pro student',
            );
        }

        
        return apply_filters('aieo_command_palette_commands', $commands);
    }
}

AIEO_Command_Palette::init();
