<?php
namespace WooNooW\Compat;

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

class MenuProvider {
    const SNAPSHOT_OPTION = 'wnw_wc_menu_snapshot';
    const SNAPSHOT_TTL    = 60; // seconds (dev: keep small)

    public static function init() {
        add_action('admin_menu', [__CLASS__, 'flag_menus_ready'], 9999);
        add_action('admin_head', [__CLASS__, 'snapshot_menus']);
        add_action('switch_theme', [__CLASS__, 'flush']);
        add_action('activated_plugin', [__CLASS__, 'flush']);
        add_action('deactivated_plugin', [__CLASS__, 'flush']);
    }

    public static function flag_menus_ready() {
        // no-op; ensures our priority ordering
    }

    public static function snapshot_menus() {
        if ( defined('WNW_DEV') && WNW_DEV ) {
            delete_transient(self::SNAPSHOT_OPTION);
        }
        $cached = get_transient(self::SNAPSHOT_OPTION);
        if ( $cached && ! empty($cached['items']) ) return;

        global $menu, $submenu;
        $items = [];

        $push = static function($parent, $entry) use (&$items) {
            $title = isset($entry[0]) ? wp_strip_all_tags($entry[0]) : '';
            $cap   = $entry[1] ?? '';
            $slug  = $entry[2] ?? '';
            $href  = (strpos($slug, 'http') === 0) ? $slug : admin_url($slug);
            $items[] = [
                'title'       => $title,
                'capability'  => $cap,
                'slug'        => (string) $slug,
                'href'        => $href,
                'parent_slug' => $parent,
            ];
        };

        foreach ((array)$menu as $m) {
            if (empty($m[2])) continue;
            $pslug = $m[2];
            $push(null, $m);
            if ( isset($submenu[$pslug]) ) {
                foreach ($submenu[$pslug] as $sub) $push($pslug, $sub);
            }
        }

        // classify
        foreach ($items as &$it) {
            $it['area'] = self::classify($it);
            $it['mode'] = 'bridge'; // default; SPA may override per known routes
            $it['bridge_url'] = $it['href'];
        }

        // filter out separators and empty titles
        $items = array_values(array_filter($items, function($it) {
            $title = trim($it['title'] ?? '');
            $slug  = $it['slug'] ?? '';
            $href  = $it['href'] ?? '';
            if ($title === '') return false;
            if (stripos($slug, 'separator') === 0) return false;
            if (preg_match('/separator-woocommerce/i', $slug)) return false;
            if (preg_match('/separator/i', $href)) return false;
            return true;
        }));

        set_transient(self::SNAPSHOT_OPTION, ['items' => $items, 'ts' => time()], self::SNAPSHOT_TTL);
        update_option(self::SNAPSHOT_OPTION, ['items' => $items, 'ts' => time()], false); // backup for inspection
    }

    private static function classify(array $it): string {
        $slug = (string) ($it['slug'] ?? '');
        $href = (string) ($it['href'] ?? '');
        $hay  = $slug . ' ' . $href;

        // Main areas
        if (preg_match('~shop_order|wc-orders|orders~i', $hay)) return 'orders';
        if (preg_match('~post_type=product|products~i', $hay))  return 'products';
        if (preg_match('~wc-admin|analytics|marketing~i', $hay)) return 'dashboard';

        // Settings family
        if (preg_match('~wc-settings|woocommerce|status|addons~i', $hay)) return 'settings';

        // Unknowns fall under Settings → Add-ons by default
        return 'addons';
    }

    public static function get_snapshot(): array {
        $data = get_transient(self::SNAPSHOT_OPTION);
        if ( ! $data ) $data = get_option(self::SNAPSHOT_OPTION, []);
        return $data['items'] ?? [];
    }

    public static function flush() {
        delete_transient(self::SNAPSHOT_OPTION);
        delete_option(self::SNAPSHOT_OPTION);
    }
}