<?php

// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedNamespaceFound -- APD is the established namespace shared with the Pro add-on and retained for backward compatibility.
namespace APD\Admin;

use APD\PostTypes\ProductConfigPostType;
use APD\PostTypes\ThemePostType;
defined('ABSPATH') || exit;

class AdminMenu
{
    public function __construct(private readonly SupportPage $supportPage)
    {
    }

    public function register() : void
    {
        $dashboardPage = new DashboardPage();
        $pricingRulesPage = class_exists(PricingRulesPage::class) ? new PricingRulesPage() : null;

        add_menu_page(
            __('Asuka Designer', 'asuka-product-designer'),
            __('Asuka Designer', 'asuka-product-designer'),
            'manage_asuka_designer',
            'apd-dashboard',
            [$dashboardPage, 'render'],
            ASUKA_PRODUCT_DESIGNER_ASSETS_URL . 'images/brand/asuka-designer-admin-menu-icon.svg?ver=1',
            58
        );

        // The top-level page already owns the render callback for this hook.
        add_submenu_page('apd-dashboard', __('Overview', 'asuka-product-designer'), __('Overview', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-dashboard');
        add_submenu_page('apd-dashboard', __('Support center', 'asuka-product-designer'), __('Support center', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-support', [$this->supportPage, 'render']);
        add_submenu_page('apd-dashboard', __('Products', 'asuka-product-designer'), __('Products', 'asuka-product-designer'), 'manage_asuka_designer', 'edit.php?post_type=' . ProductConfigPostType::TYPE);
        add_submenu_page('apd-dashboard', __('Assets', 'asuka-product-designer'), __('Assets', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-assets', [new AssetsHubPage(), 'render']);
        add_submenu_page('apd-dashboard', __('Themes', 'asuka-product-designer'), __('Themes', 'asuka-product-designer'), 'manage_asuka_designer', 'edit.php?post_type=' . ThemePostType::TYPE);
        add_submenu_page('apd-dashboard', __('Commerce', 'asuka-product-designer'), __('Commerce', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-commerce', [new CommerceHubPage(), 'render']);
        add_submenu_page(
            'apd-dashboard',
            __('Pricing rules', 'asuka-product-designer'),
            __('Pricing rules', 'asuka-product-designer'),
            'manage_asuka_designer',
            'apd-pricing-rules',
            $pricingRulesPage ? [$pricingRulesPage, 'render'] : [$this, 'renderPricingRulesUnavailable']
        );
        add_submenu_page('apd-dashboard', __('Translations', 'asuka-product-designer'), __('Translations', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-translations', [new TranslationsPage(), 'render']);
        add_submenu_page('apd-dashboard', __('Import / Export', 'asuka-product-designer'), __('Import / Export', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-tools', [new ToolsPage(), 'render']);
        add_submenu_page('apd-dashboard', __('Changelog', 'asuka-product-designer'), __('Changelog', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-changelog', [new ChangelogPage(), 'render']);
        // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- The apd_ hook is a documented integration API retained for backward compatibility.
        if ((bool) apply_filters('apd_show_plugin_documentation_menu', true)) {
            add_submenu_page('apd-dashboard', __('Documentation', 'asuka-product-designer'), __('Documentation', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-documentation', [new DocumentationPage(), 'render']);
        }
        add_submenu_page('apd-dashboard', __('Settings', 'asuka-product-designer'), __('Settings', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-settings', [new SettingsPage(), 'render']);

        add_submenu_page(null, __('Design library', 'asuka-product-designer'), __('Design library', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-designs', [new DesignsPage(), 'render']);
        add_submenu_page(null, __('Font library', 'asuka-product-designer'), __('Font library', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-fonts', [new FontsPage(), 'render']);
        add_submenu_page(null, __('Colors & printing', 'asuka-product-designer'), __('Colors & printing', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-colors', [new ColorsPage(), 'render']);
        add_submenu_page(null, __('Shapes library', 'asuka-product-designer'), __('Shapes library', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-shapes', [new ShapesPage(), 'render']);
        add_submenu_page(null, __('Production', 'asuka-product-designer'), __('Production', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-production', [new OrderProductionPage(), 'render']);
        add_submenu_page(null, __('QA & Diagnostics', 'asuka-product-designer'), __('QA & Diagnostics', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-qa-diagnostics', [new QADiagnosticsPage(), 'render']);
        add_submenu_page(null, __('Edit design', 'asuka-product-designer'), __('Edit design', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-designs-editor', [new DesignsPage(), 'renderEditor']);
        add_submenu_page(null, __('Edit font', 'asuka-product-designer'), __('Edit font', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-fonts-editor', [new FontsPage(), 'renderEditor']);
        add_submenu_page(null, __('Edit color', 'asuka-product-designer'), __('Edit color', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-colors-editor', [new ColorsPage(), 'renderColorEditor']);
        add_submenu_page(null, __('Edit printing method', 'asuka-product-designer'), __('Edit printing method', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-printing-methods-editor', [new ColorsPage(), 'renderPrintingMethodEditor']);
        if ($pricingRulesPage) {
            add_submenu_page(null, __('Edit pricing rule', 'asuka-product-designer'), __('Edit pricing rule', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-pricing-rules-editor', [$pricingRulesPage, 'renderEditor']);
        }
        add_submenu_page(null, __('Edit shape', 'asuka-product-designer'), __('Edit shape', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-shapes-editor', [new ShapesPage(), 'renderEditor']);
        add_submenu_page(null, __('Themes', 'asuka-product-designer'), __('Themes', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-theme', [new ThemePage(), 'redirectLegacy']);
        add_submenu_page(null, __('Designer Studio', 'asuka-product-designer'), __('Designer Studio', 'asuka-product-designer'), 'manage_asuka_designer', 'apd-designer-studio', [new DesignerStudioPage(), 'render']);
    }

    public function renderPricingRulesUnavailable() : void
    {
        AdminAccess::renderUnavailable(
            __('Pricing rules', 'asuka-product-designer'),
            __('Install and activate the Asuka Designer Pro add-on, then connect an eligible license to use conditional pricing rules.', 'asuka-product-designer')
        );
    }
}
