<?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\Support\AssetCatalog;

defined('ABSPATH') || exit;

class CommerceHubPage
{
    public function render() : void
    {
        if (! current_user_can('manage_asuka_designer')) {
            wp_die(esc_html__('You are not allowed to access this page.', 'asuka-product-designer'));
        }

        $overview = AdminOverview::snapshot();
        $theme = sanitize_html_class((string) ($overview['settings']['admin_theme'] ?? 'light'));
        $savedDesignCounts = wp_count_posts('apd_user_design');
        $savedDesignTotal = (int) ($savedDesignCounts->publish ?? 0) + (int) ($savedDesignCounts->draft ?? 0) + (int) ($savedDesignCounts->private ?? 0);
        $pricingRuleCount = count((new AssetCatalog())->getCollection('pricing_rules'));

        $workspaces = [
            [
                'label' => __('WooCommerce products', 'asuka-product-designer'),
                'copy' => __('Bind product configurations and variation-specific overrides.', 'asuka-product-designer'),
                'value' => sprintf(/* translators: Placeholder values are populated from the current administration record. */ _n('%d linked product', '%d linked products', (int) $overview['bindings']['count'], 'asuka-product-designer'), (int) $overview['bindings']['count']),
                'icon' => 'products',
                'url' => admin_url('edit.php?post_type=product'),
                'action' => __('Open products', 'asuka-product-designer'),
            ],
            [
                'label' => __('Production orders', 'asuka-product-designer'),
                'copy' => __('Inspect production packs, previews, measurements, and workshop status.', 'asuka-product-designer'),
                'value' => sprintf(/* translators: Placeholder values are populated from the current administration record. */ _n('%d pending item', '%d pending items', (int) $overview['production']['pending'], 'asuka-product-designer'), (int) $overview['production']['pending']),
                'icon' => 'clipboard',
                'url' => admin_url('admin.php?page=apd-production'),
                'action' => __('Open production', 'asuka-product-designer'),
            ],
            [
                'label' => __('Saved customer designs', 'asuka-product-designer'),
                'copy' => __('Review customer design records and resume links stored by WordPress.', 'asuka-product-designer'),
                'value' => sprintf(/* translators: Placeholder values are populated from the current administration record. */ _n('%d saved design', '%d saved designs', $savedDesignTotal, 'asuka-product-designer'), $savedDesignTotal),
                'icon' => 'portfolio',
                'url' => admin_url('edit.php?post_type=apd_user_design'),
                'action' => __('Open saved designs', 'asuka-product-designer'),
            ],
        ];

        $pricingRulesAvailable = class_exists(PricingRulesPage::class) && AdminAccess::canUse('pricingrules');
        $workspaces[] = [
            'label' => __('Pricing rules', 'asuka-product-designer'),
            'copy' => $pricingRulesAvailable
                ? __('Build reusable conditional pricing rules for product configurations.', 'asuka-product-designer')
                : __('Unlock reusable conditional pricing rules with Asuka Designer Pro.', 'asuka-product-designer'),
            'value' => $pricingRulesAvailable
                ? sprintf(/* translators: Placeholder values are populated from the current administration record. */ _n('%d pricing rule', '%d pricing rules', $pricingRuleCount, 'asuka-product-designer'), $pricingRuleCount)
                : __('Premium', 'asuka-product-designer'),
            'icon' => $pricingRulesAvailable ? 'chart-line' : 'lock',
            'url' => admin_url('admin.php?page=apd-pricing-rules'),
            'action' => $pricingRulesAvailable
                ? __('Manage pricing rules', 'asuka-product-designer')
                : __('Review access', 'asuka-product-designer'),
        ];
        ?>
        <div class="wrap apd-admin-shell apd-admin-theme-<?php echo esc_attr($theme); ?> apd-hub-page">
            <header class="apd-workspace-header">
                <div>
                    <span class="apd-dashboard-eyebrow"><?php esc_html_e('Store operations', 'asuka-product-designer'); ?></span>
                    <h1><?php esc_html_e('Commerce', 'asuka-product-designer'); ?></h1>
                    <p><?php esc_html_e('Connect product configuration to WooCommerce, monitor customer designs, and move customized orders into production.', 'asuka-product-designer'); ?></p>
                </div>
                <span class="apd-runtime-pill is-<?php echo $overview['woocommerce']['active'] ? 'ready' : 'warning'; ?>">
                    <span aria-hidden="true"></span>
                    <?php echo esc_html($overview['woocommerce']['active'] ? __('WooCommerce ready', 'asuka-product-designer') : __('WooCommerce unavailable', 'asuka-product-designer')); ?>
                </span>
            </header>

            <?php if (! $overview['woocommerce']['active']) : ?>
                <div class="apd-dashboard-notice is-warning">
                    <span class="dashicons dashicons-warning" aria-hidden="true"></span>
                    <div>
                        <strong><?php esc_html_e('Commerce workflows are paused', 'asuka-product-designer'); ?></strong>
                        <p><?php esc_html_e('Activate WooCommerce to enable product binding, cart validation, checkout, order metadata, and production packs.', 'asuka-product-designer'); ?></p>
                    </div>
                    <a href="<?php echo esc_url(admin_url('plugins.php')); ?>"><?php esc_html_e('Manage plugins', 'asuka-product-designer'); ?></a>
                </div>
            <?php endif; ?>

            <div class="apd-workspace-grid">
                <?php foreach ($workspaces as $workspace) : ?>
                    <article class="apd-workspace-card">
                        <div class="apd-workspace-card__icon"><span class="dashicons dashicons-<?php echo esc_attr($workspace['icon']); ?>" aria-hidden="true"></span></div>
                        <div class="apd-workspace-card__head">
                            <h2><?php echo esc_html($workspace['label']); ?></h2>
                        </div>
                        <p><?php echo esc_html($workspace['copy']); ?></p>
                        <strong class="apd-workspace-card__value"><?php echo esc_html($workspace['value']); ?></strong>
                        <a href="<?php echo esc_url($workspace['url']); ?>"><?php echo esc_html($workspace['action']); ?> <span aria-hidden="true">&rarr;</span></a>
                    </article>
                <?php endforeach; ?>
            </div>
        </div>
        <?php
    }
}
