<?php
/**
 * Registers the SkyServers Sync top-level admin menu and sub-menus.
 *
 * @package SkyEmagSync
 */

namespace SkyEmagSync\Admin;

use SkyEmagSync\Plugin;

defined( 'ABSPATH' ) || exit;

class Menu {

    const PARENT_SLUG      = 'skyservers-product-sync-for-emag';
    const SETTINGS_SLUG    = 'skyservers-product-sync-for-emag';
    const PRODUCTS_SLUG    = 'skyservers-sync-products';
    const CATEGORY_MAP_SLUG = 'skyservers-sync-category-map';
    const CHAR_MAP_SLUG     = 'skyservers-sync-char-map';
    const FAMILY_MAP_SLUG   = 'skyservers-sync-family-map';
    const PUBLISH_SLUG      = 'skyservers-sync-publish';
    const ORDERS_SLUG        = 'skyservers-sync-orders';
    const NOTIFICATIONS_SLUG = 'skyservers-sync-notifications';
    const LOGS_SLUG        = 'skyservers-sync-logs';
    const HELP_SLUG        = 'skyservers-sync-help';
    const SETUP_SLUG       = 'skyservers-sync-setup';
    const CAPABILITY       = 'manage_woocommerce';

    public function register(): void {
        add_action( 'admin_menu', [ $this, 'add_menu' ] );
    }

    public function add_menu(): void {
        // Top-level item (also serves as Settings)
        add_menu_page(
            __( 'SkyServers Sync', 'skyservers-product-sync-for-emag' ),
            __( 'SkyServers Sync', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::PARENT_SLUG,
            [ SettingsPage::class, 'render' ],
            'dashicons-update-alt',
            56  // Just below WooCommerce
        );

        // Settings sub-menu (rename auto-created duplicate)
        add_submenu_page(
            self::PARENT_SLUG,
            __( 'SkyServers Sync Settings', 'skyservers-product-sync-for-emag' ),
            __( 'Settings', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::SETTINGS_SLUG,
            [ SettingsPage::class, 'render' ]
        );

        // Products sub-menu (free)
        add_submenu_page(
            self::PARENT_SLUG,
            __( 'SkyServers Sync Products', 'skyservers-product-sync-for-emag' ),
            __( 'Products', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::PRODUCTS_SLUG,
            [ ProductsPage::class, 'render' ]
        );

        // ── Admin pages that live in the Premium add-on. Each registration
        // checks class_exists() on the consuming page class; the Free build
        // on WordPress.org physically excludes those classes via the
        // Freemius @fs_premium_only directive, so the menu items never
        // appear on Free. No license check is layered on top — the
        // dependency either exists in the build or it does not.
        if ( class_exists( CategoryMapPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Category Map', 'skyservers-product-sync-for-emag' ),
                __( 'Category Map', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::CATEGORY_MAP_SLUG,
                [ CategoryMapPage::class, 'render' ]
            );
        }

        if ( class_exists( CharMapPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Characteristics Map', 'skyservers-product-sync-for-emag' ),
                __( 'Char. Map', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::CHAR_MAP_SLUG,
                [ CharMapPage::class, 'render' ]
            );
        }

        if ( class_exists( FamilyMapPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Family Map', 'skyservers-product-sync-for-emag' ),
                __( 'Family Map', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::FAMILY_MAP_SLUG,
                [ FamilyMapPage::class, 'render' ]
            );
        }

        if ( class_exists( PublishPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Publish Products', 'skyservers-product-sync-for-emag' ),
                __( 'Publish', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::PUBLISH_SLUG,
                [ PublishPage::class, 'render' ]
            );
        }

        if ( class_exists( OrdersPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Orders', 'skyservers-product-sync-for-emag' ),
                __( 'Orders & AWB', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::ORDERS_SLUG,
                [ OrdersPage::class, 'render' ]
            );
        }

        if ( class_exists( NotificationsPage::class ) ) {
            add_submenu_page(
                self::PARENT_SLUG,
                __( 'Notifications', 'skyservers-product-sync-for-emag' ),
                __( 'Notifications', 'skyservers-product-sync-for-emag' ),
                self::CAPABILITY,
                self::NOTIFICATIONS_SLUG,
                [ NotificationsPage::class, 'render' ]
            );
        }

        // Logs sub-menu (free)
        add_submenu_page(
            self::PARENT_SLUG,
            __( 'SkyServers Sync Logs', 'skyservers-product-sync-for-emag' ),
            __( 'Logs', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::LOGS_SLUG,
            [ LogsPage::class, 'render' ]
        );

        // Help sub-menu (free)
        add_submenu_page(
            self::PARENT_SLUG,
            __( 'Help', 'skyservers-product-sync-for-emag' ),
            __( 'Help', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::HELP_SLUG,
            [ HelpPage::class, 'render' ]
        );

        // Hidden setup wizard page — no menu entry, but routable via
        // admin.php?page=skyservers-sync-setup. Empty parent slug makes the
        // submenu invisible while still registering the page callback.
        add_submenu_page(
            '',                                  // parent_slug = '' → hidden
            __( 'SkyServers Sync Setup', 'skyservers-product-sync-for-emag' ),
            __( 'Setup', 'skyservers-product-sync-for-emag' ),
            self::CAPABILITY,
            self::SETUP_SLUG,
            [ SetupWizard::class, 'render_page' ]
        );
    }

    /**
     * Returns the Premium upgrade URL.
     *
     * Routes directly to the Freemius hosted checkout for plugin 25648,
     * plan 42434 (Premium). Bypasses the internal pricing admin page that
     * Freemius does not register when the SDK runs in is_org_compliant
     * mode — that page returned "you do not have permission" on Free
     * builds and broke every upgrade button.
     *
     * The checkout page is public, brand-ed with Freemius, and handles
     * payment + license activation in a single round-trip without
     * requiring WordPress capabilities or an admin login.
     */
    public static function upgrade_url(): string {
        return 'https://checkout.freemius.com/mode/dialog/plugin/25648/plan/42434/';
    }
}
