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

namespace Licenzo\Hooks\Handler\Admin;

use Licenzo\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
        $menuSlug = 'licenzo-app';
        $baseUrl = admin_url('admin.php?page=' . $menuSlug) . '#';

        add_menu_page(
            __('Licenzo', 'licenzo'),
            __('Licenzo', 'licenzo'),
            'manage_options',
            $menuSlug,
            [$this, 'renderAdminApp'],
            $this->getLogo(),
            30
        );

        add_submenu_page(
            $menuSlug,
            __('Dashboard', 'licenzo'),
            __('Dashboard', 'licenzo'),
            'manage_options',
            $menuSlug,
            ''
        );

        add_submenu_page(
            $menuSlug,
            __('Products', 'licenzo'),
            __('Products', 'licenzo'),
            'manage_options',
            esc_url("{$baseUrl}/products"),
            ''
        );

        add_submenu_page(
            $menuSlug,
            __('Customers', 'licenzo'),
            __('Customers', 'licenzo'),
            'manage_options',
            esc_url("{$baseUrl}/customers"),
            ''
        );
        add_submenu_page(
            $menuSlug,
            __('Licenses', 'licenzo'),
            __('Licenses', 'licenzo'),
            'manage_options',
            esc_url("{$baseUrl}/licenses"),
            ''
        );

        add_submenu_page(
            $menuSlug,
            __('Settings', 'licenzo'),
            __('Settings', 'licenzo'),
            'manage_options',
            esc_url("{$baseUrl}/settings"),
            ''
        );

        // Enqueue media/editor scripts only on Licenzo admin pages
        add_action('admin_enqueue_scripts', function ($hook) {
            if (strpos($hook, 'licenzo') === false) {
                return;
            }
            $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="licenzo-app-admin-app">Loading...</div>';
        }
    }

    public function getLogo()
    {
        $logo = '
                <svg width="512" height="512" viewBox="0 0 144 144" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M61.5693 7.22016C65.3874 5.63761 69.67 5.59382 73.5195 7.09809L111.557 21.9643L102.481 31.3012L69.1514 18.2748C68.1892 17.899 67.1184 17.9096 66.1641 18.3051L23.3809 36.0405C23.1506 36.1362 23 36.3614 23 36.6108V70.7036C23.0001 97.1381 41.6896 119.86 67.584 124.993C93.8211 119.771 112.757 96.7384 112.757 69.9526V54.7289L124.757 42.3823V69.9526C124.757 102.627 101.548 130.7 69.4609 136.856C68.2246 137.093 66.9528 137.094 65.7158 136.858C33.9654 130.794 11.0001 103.028 11 70.7036V36.6108C11 31.5089 14.0733 26.9084 18.7861 24.9545L61.5693 7.22016Z" fill="#1D4961"/>
                    <path d="M71.1214 89.0285L62.9701 97.4164L37.7611 72.9183L45.9124 64.5304L71.1214 89.0285Z" fill="#35A153"/>
                    <path d="M124.102 17.6665L132.489 25.8183L63.1992 97.1111L54.8118 88.9593L124.102 17.6665Z" fill="#35A153"/>
                </svg>
';

        return 'data:image/svg+xml;base64,' . base64_encode($logo);
    }
}
