<?php
/**
 * PufferGo 后台菜单集中注册
 *
 * 所有 `add_menu_page` / `add_submenu_page` 仅在此类中注册，避免分散在多个模块。
 * 修改菜单顺序或 slug 时，请同步核对下方「资源清单」中的 admin hook / screen id，以免脚本漏载。
 *
 * ---------------------------------------------------------------------------
 * 资源清单（迁移或改 slug 时请逐项对照）
 * ---------------------------------------------------------------------------
 *
 * 1) 全局 Tailwind（PufferGo_Admin_Loader::load_admin_tailwind）
 *    - 条件：$screen->id 含 puffergo、或 AI 噶站/设置/站点画像页、或 post/post-new
 *    - 样式：build/blocks/ai-admin-app/index.css（handle: puffergo-admin-tailwind）
 *
 * 2) 控制台 / Dashboard（主菜单，父 slug = puffergo）
 *    - admin $hook：toplevel_page_puffergo
 *    - 类：PufferGo_Dashboard::enqueue_scripts
 *    - 脚本：build/blocks/dashboard-app/index.js（+ index.asset.php）
 *    - 另：WP 仪表盘 index.php 上的 Banner（enqueue_wp_dashboard_banner_assets）
 *
 * 3) Site profile / 站点画像（slug：puffergo-site-profile）
 *    - admin $hook：puffergo_page_puffergo-site-profile（enqueue 内用 strpos(..., 'puffergo-site-profile')）
 *    - 类：PufferGo_Site_Profile::enqueue_scripts
 *    - 脚本：build/blocks/site-profile-app/index.js
 *    - 样式：site-profile-app/index.css、style-index.css；可能重复 enqueue ai-admin-app/index.css
 *    - 其它：wp-components、wp_localize_script puffergoSiteProfile
 *
 * 4) AI Builder（slug：puffergo-ai-site-builder，slug 历史原因未改）
 *    - admin $hook：puffergo_page_puffergo-ai-site-builder
 *    - 类：PufferGo_AI_Site_Builder::enqueue_scripts
 *    - 脚本：build/blocks/ai-site-builder-app/index.js
 *    - 样式：ai-site-builder-app/index.css、style-index.css
 *    - 其它：wp_enqueue_media()、wp-components、wp_localize_script puffergoAiSiteBuilder
 *
 * 5) Settings / 设置（slug：puffergo-settings）
 *    - admin $hook：puffergo_page_puffergo-settings
 *    - 类：PufferGo_Settings::enqueue_scripts
 *    - 脚本：build/blocks/settings-app/index.js
 *    - 样式：settings-app/style-index.css
 *    - 其它：wp-components、wp_localize_script puffergoSettings
 *
 * 6) Inquiries / 询盘管理（slug：puffergo-inquiries）
 *    - admin $hook：puffergo_page_puffergo-inquiries
 *    - 类：PufferGo_Contact_Form_Handler::enqueue_inquiries_scripts
 *    - 脚本：build/blocks/inquiries-app/index.js
 *    - 样式：inquiries-app/style-index.css
 *    - 其它：wp-components、wp_localize_script puffergoInquiries
 *    注意：原 App Installer（slug：puffergo-plugin-setup）已合并到 Settings 页面，仅保留 REST 路由。
 * @package PufferGo
 */

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

class PufferGo_Admin_Menu {

    const PARENT_SLUG = 'puffergo';

    /**
     * 强制菜单顺序：PufferGo → Products → Posts → ...
     */
    public static function reorder_menu( $menu_order ) {
        $puffergo_item = 'puffergo';
        $product_item  = 'edit.php?post_type=puffergo_product';

        $puffergo_pos = array_search( $puffergo_item, $menu_order, true );
        $product_pos  = array_search( $product_item, $menu_order, true );

        if ( false !== $product_pos && false !== $puffergo_pos ) {
            unset( $menu_order[ $product_pos ] );
            $menu_order = array_values( $menu_order );
            $puffergo_pos = array_search( $puffergo_item, $menu_order, true );
            if ( false !== $puffergo_pos ) {
                array_splice( $menu_order, $puffergo_pos + 1, 0, array( $product_item ) );
            }
        }

        return $menu_order;
    }

    /**
     * 注册顶级菜单及全部子菜单（单次 admin_menu 回调内完成，保证顺序稳定）。
     *
     * @return void
     */
    public static function register_menus() {
        // Force custom menu ordering so Products appears right after PufferGo
        add_filter( 'custom_menu_order', '__return_true' );
        add_filter( 'menu_order', array( __CLASS__, 'reorder_menu' ) );

        /* translators: Plugin name in the admin sidebar root menu and browser title on the PufferGo home screen. */
        $menu_brand = __( 'PufferGo', 'puffergo' );

        add_menu_page(
            $menu_brand,
            $menu_brand,
            'edit_posts',
            self::PARENT_SLUG,
            array( 'PufferGo_Dashboard', 'render_page' ),
            'dashicons-admin-site-alt3',
            3
        );

        // 与父级 slug 相同：覆盖 WP 自动生成的首条子菜单（否则侧栏会出现两个「PufferGo」）。
        /* translators: First submenu under PufferGo; plugin home / control center (admin.php?page=puffergo). */
        $dashboard_page_title = __( 'Dashboard', 'puffergo' );
        add_submenu_page(
            self::PARENT_SLUG,
            $dashboard_page_title,
            $dashboard_page_title,
            'edit_posts',
            self::PARENT_SLUG,
            array( 'PufferGo_Dashboard', 'render_page' )
        );

        /* translators: Admin submenu: editable site profile (identity/positioning) for AI and templates. */
        $site_profile = __( 'Site Profile', 'puffergo' );
        add_submenu_page(
            self::PARENT_SLUG,
            $site_profile,
            $site_profile,
            'manage_options',
            'puffergo-site-profile',
            array( 'PufferGo_Site_Profile', 'render_page' )
        );

        /* translators: Admin submenu: AI site builder (full workflow). */
        $ai_builder = __( 'AI Builder', 'puffergo' );
        add_submenu_page(
            self::PARENT_SLUG,
            $ai_builder,
            $ai_builder,
            'edit_posts',
            'puffergo-ai-site-builder',
            array( 'PufferGo_AI_Site_Builder', 'render_admin_page' )
        );

        /* translators: Admin submenu: view and manage contact form submissions (inquiries). */
        $inquiries = __( 'Inquiries', 'puffergo' );
        add_submenu_page(
            self::PARENT_SLUG,
            $inquiries,
            $inquiries,
            'manage_options',
            'puffergo-inquiries',
            array( 'PufferGo_Contact_Form_Handler', 'render_inquiries_page' )
        );

        /* translators: Admin submenu: API key and plugin options (WordPress-standard label). */
        $settings = __( 'Settings', 'puffergo' );
        add_submenu_page(
            self::PARENT_SLUG,
            $settings,
            $settings,
            'manage_options',
            'puffergo-settings',
            array( 'PufferGo_Settings', 'render_page' )
        );
    }
}
