<?php
namespace AIAutoTool\Features\PromptCustomer;

if ( ! defined( 'ABSPATH' ) ) exit; // Thoát nếu truy cập trực tiếp

/**
 * AI Auto Tool - Prompt Customer Feature
 * 
 * @package AIAutoTool\Features\PromptCustomer
 * @namespace AIAutoTool\Features\PromptCustomer
 */

// Include the full Prompt Customer implementation with all features
require_once dirname(__FILE__) . '/AI_AutoTool_Prompt_Customer.php';

// Create namespace wrapper
if (!class_exists('AIAutoTool\\Features\\PromptCustomer\\PromptCustomer')) {
    class PromptCustomer extends \AI_AutoTool_Prompt_Customer {
        public $active_option_name = 'AI_AutoTool_Prompt_Customer_active';
        public $icon = '<i class="fa-solid fa-user"></i>';
        public $active = false;
        
        public function __construct() {
            // Ensure correct option name
            // Note: AI_AutoTool_Prompt_Customer doesn't extend rendersetting, 
            // so we may need to handle this differently
            parent::__construct();
            
            // Get active status
            $this->active = get_option($this->active_option_name, false);
            
            // Register AJAX handler for activation
            add_action('wp_ajax_update_active_option_canonical_' . $this->active_option_name, array($this, 'update_active_option_callback'));
            
            // Add admin menu if active
            add_action('admin_menu', array($this, 'maybe_add_admin_menu'), 20);
        }
        
        /**
         * Add admin menu for Prompt Customer settings
         */
        public function maybe_add_admin_menu() {
            $this->active = get_option($this->active_option_name, false);
            
            if ($this->active === 'true') {
                // Ensure parent menu exists
                if (!defined('MENUSUBPARRENT') || empty(MENUSUBPARRENT)) {
                    return;
                }
                
                // Check if parent menu exists
                global $submenu;
                $parent_exists = false;
                if (isset($submenu[MENUSUBPARRENT])) {
                    $parent_exists = true;
                }
                
                if (!$parent_exists) {
                    return;
                }
                
                // Add submenu
                add_submenu_page(
                    MENUSUBPARRENT,
                    '<i class="fa-solid fa-user"></i> ' . __('Prompt Customer', 'ai-auto-tool'),
                    '<i class="fa-solid fa-user"></i> ' . __('Prompt Customer', 'ai-auto-tool'),
                    'manage_options',
                    'aiautotool-prompt-customer',
                    array($this, 'render_settings_page')
                );
            }
        }
        
        /**
         * Render settings page
         */
        public function render_settings_page() {
            if (!current_user_can('manage_options')) {
                wp_die(__('You do not have sufficient permissions to access this page.', 'ai-auto-tool'));
            }
            
            ?>
            <div class="wrap">
                <h1><?php echo esc_html__('Prompt Customer Settings', 'ai-auto-tool'); ?></h1>
                <?php $this->render_setting(); ?>
            </div>
            <?php
        }
        
        /**
         * Update active option callback for AJAX
         */
        public function update_active_option_callback() {
            check_ajax_referer('aiautotool_nonce', 'security');
            
            if (!current_user_can('manage_options')) {
                wp_die(__('Permission denied', 'ai-auto-tool'), '', array('response' => 403));
            }
            
            if (isset($_POST['active'])) {
                $active = sanitize_text_field($_POST['active']);
                update_option($this->active_option_name, $active, null, 'no');
                
                // Update local active status
                $this->active = $active;
                
                print_r($active);
            }
            
            wp_die();
        }
        
        /**
         * Render feature for integrations page
         */
        public function render_feature() {
            // Load feature descriptions
            require_once plugin_dir_path(dirname(__FILE__)) . 'lib/feature-descriptions.php';
            
            // Get current active status
            $this->active = get_option($this->active_option_name, false);
            
            $description = \AiAutoTool_Feature_Descriptions::get('prompt_customer');
            if (!$description) {
                $description = __('Customer prompt management and customization', 'ai-auto-tool');
            }
            
            $autoToolBox = new \AutoToolBox(
                esc_html__('Prompt Customer', 'ai-auto-tool'),
                $description,
                "https://doc.aiautotool.com/features/prompt-customer",
                $this->active_option_name,
                ($this->active === 'true' || $this->active === true),
                plugins_url('../../images/logo.svg', __FILE__)
            );
            
            echo $autoToolBox->generateHTML();
        }
        
        /**
         * Note: AI_AutoTool_Prompt_Customer doesn't extend rendersetting,
         * so it may not have render_tab_setting or render_setting methods
         * These would need to be implemented if needed
         */
    }
}
