<?php

/**
 * Menu Sync API - Ultra lightweight admin page for menu synchronization
 */

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

class FleekdashMenuSyncAPI
{
    private const PAGE_TITLE = 'Menu Sync';

    public function __construct()
    {
        add_action('admin_menu', [$this, 'addMenuSyncPage']);
    }

    /**
     * Add a hidden admin page for menu sync
     */
    public function addMenuSyncPage(): void
    {
        add_submenu_page(
            null,
            self::PAGE_TITLE,
            self::PAGE_TITLE,
            'manage_options',
            'fleekdash-menu-sync',
            [$this, 'renderMenuSyncPage']
        );
    }

    /**
     * Render the menu sync page
     */
    public function renderMenuSyncPage(): void
    {
        if (isset($_GET['fleekdash-frame']) && $_GET['fleekdash-frame'] === '1') {
            $sync_type = isset($_GET['sync-type']) ? sanitize_text_field($_GET['sync-type']) : 'menu';
            $this->renderIframeSyncContent($sync_type);
            return;
        }

        echo '<div class="wrap"><h1>FleekDash Sync</h1><p>This page is used for various synchronization tasks.</p></div>';
    }

    /**
     * Render iframe-specific content with sync functionality
     */
    private function renderIframeSyncContent(string $sync_type = 'menu'): void
    {
        $sync_labels = [
            'menu' => self::PAGE_TITLE,
            'plugins' => 'Plugin Sync',
            'themes' => 'Theme Sync',
            'notifications' => 'Notification Sync',
            'settings' => 'Settings Sync'
        ];

        $sync_label = isset($sync_labels[$sync_type]) ? $sync_labels[$sync_type] : 'Generic Sync';

        ?>
        <style>
            /* CRITICAL: Force admin menu to be rendered but invisible */
            #adminmenumain, #adminmenuwrap, #adminmenuback, #adminmenu {
                display: block !important;
                visibility: visible !important;
                opacity: 1 !important;
                position: absolute !important;
                left: -99999px !important;
                top: -99999px !important;
                width: auto !important;
                height: auto !important;
                z-index: -1 !important;
            }

            /* Ensure menu items are also rendered */
            #adminmenu li, #adminmenu a, #adminmenu .wp-submenu {
                display: block !important;
                visibility: visible !important;
                opacity: 1 !important;
            }

            /* Hide non-essential UI to reduce noise */
            #wpfooter, .update-nag, .notice, .error, #wpadminbar, #screen-meta, #screen-meta-links {
                display: none !important;
            }

            /* Show a small indicator */
            body::before {
                content: "FleekDash <?php echo esc_html($sync_label); ?> Active";
                position: fixed;
                top: 10px;
                right: 10px;
                background: #0073aa;
                color: white;
                padding: 5px 10px;
                border-radius: 3px;
                font-size: 11px;
                z-index: 9999;
            }
        </style>

        <div class="wrap">
            <p style="color: #666; font-size: 12px;"><?php echo esc_html($sync_label); ?> monitoring active...</p>
            <div style="margin-top: 10px; padding: 10px; background: #f9f9f9; border-left: 4px solid #0073aa;">
                <strong>Sync Type:</strong> <?php echo esc_html($sync_type); ?><br>
                <strong>Status:</strong> Monitoring for changes<br>
                <strong>Last Check:</strong> <?php echo current_time('H:i:s'); ?>
            </div>
        </div>
        <?php
    }
}

// Initialize the Menu Sync API
// Auto-initialization removed - now handled by ApiRegistry::registerAllApis()
