<?php
/**
 * Admin Menu Handler
 *
 * @package ZenoChatbot\Hooks\Handler\Admin
 */

namespace ZenoChatbot\Hooks\Handler\Admin;

use ZenoChatbot\Hooks\Handler\BaseHandler;

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Handles admin menu registration and rendering
 */
class MenuHandler extends BaseHandler
{
    /**
     * Handle admin menu registration
     */
    public function handle(...$args)
    {
        // Only in the admin context
        if (!is_admin()) {
            return;
        }

        $this->registerAdminMenus();
    }

    /**
     * Register admin menu pages
     */
    private function registerAdminMenus()
    {
        // Main menu page - this will handle all SPA routing
        add_menu_page(
            __('Zeno Chatbot', 'zeno-chatbot-ai'),
            __('Zeno Chatbot', 'zeno-chatbot-ai'),
            'manage_options',
            'zeno-chatbot-ai',
            [$this, 'renderAdminApp'],
            'dashicons-format-chat',
            30
        );

        $this->setupMediaEditor();
    }

    /**
     * Setup media editor functionality
     */
    private function setupMediaEditor()
    {
        if (function_exists('wp_enqueue_media')) {
            // Editor default styles.
            add_filter('user_can_richedit', '__return_true');
            wp_tinymce_inline_scripts();
            wp_enqueue_editor();
            wp_enqueue_media();
            if (current_user_can('upload_files')) {
                wp_enqueue_script('media-upload');
            }
            add_thickbox();
        }
    }

    /**
     * Render admin SPA container
     */
    public function renderAdminApp()
    {
        // Always render the same SPA container
        // Vue Router will handle client-side routing
        $adminLayoutPath = $this->app->resourcePath('Admin/views/admin-layout.php');
        if (file_exists($adminLayoutPath)) {
            include $adminLayoutPath;
        } else {
            echo '<div id="zeno-chatbot-ai-admin-app">Loading...</div>';
        }
    }
}
