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

defined('ABSPATH') || exit;

class AssetsHubPage
{
    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();
        $catalog = (new AssetCatalog())->all();
        $templateCounts = wp_count_posts('apd_template');
        $templateTotal = (int) ($templateCounts->publish ?? 0) + (int) ($templateCounts->draft ?? 0);
        $themeCounts = wp_count_posts(ThemePostType::TYPE);
        $themeTotal = (int) ($themeCounts->publish ?? 0) + (int) ($themeCounts->draft ?? 0);
        $theme = sanitize_html_class((string) ($overview['settings']['admin_theme'] ?? 'light'));
        $workspaces = [
            [
                'label' => __('Design library', 'asuka-product-designer'),
                'copy' => __('Reusable artwork customers can add to their products.', 'asuka-product-designer'),
                'count' => count((array) ($catalog['designs'] ?? [])),
                'icon' => 'format-image',
                'url' => admin_url('admin.php?page=apd-designs'),
                'action' => __('Manage designs', 'asuka-product-designer'),
            ],
            [
                'label' => __('Font library', 'asuka-product-designer'),
                'copy' => __('System, Google, and custom fonts exposed in the editor.', 'asuka-product-designer'),
                'count' => count((array) ($catalog['fonts'] ?? [])),
                'icon' => 'editor-textcolor',
                'url' => admin_url('admin.php?page=apd-fonts'),
                'action' => __('Manage fonts', 'asuka-product-designer'),
            ],
            [
                'label' => __('Colors and printing', 'asuka-product-designer'),
                'copy' => __('Color palettes and production methods shared by product configs.', 'asuka-product-designer'),
                'count' => count((array) ($catalog['colors'] ?? [])) + count((array) ($catalog['printing_methods'] ?? [])),
                'icon' => 'art',
                'url' => admin_url('admin.php?page=apd-colors'),
                'action' => __('Manage colors', 'asuka-product-designer'),
            ],
            [
                'label' => __('Shape library', 'asuka-product-designer'),
                'copy' => __('Sanitized reusable shapes available to supported designer views.', 'asuka-product-designer'),
                'count' => count((array) ($catalog['shapes'] ?? [])),
                'icon' => 'forms',
                'url' => admin_url('admin.php?page=apd-shapes'),
                'action' => __('Manage shapes', 'asuka-product-designer'),
            ],
            [
                'label' => __('Product templates', 'asuka-product-designer'),
                'copy' => __('Reusable configuration starting points for related products.', 'asuka-product-designer'),
                'count' => $templateTotal,
                'icon' => 'layout',
                'url' => admin_url('edit.php?post_type=apd_template'),
                'action' => __('Manage templates', 'asuka-product-designer'),
            ],
            [
                'label' => __('Theme Studio', 'asuka-product-designer'),
                'copy' => __('A publishable visual system for every designer surface and control.', 'asuka-product-designer'),
                'count' => $themeTotal,
                'icon' => 'admin-customizer',
                'url' => admin_url('edit.php?post_type=' . ThemePostType::TYPE),
                'action' => __('Manage themes', '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('Shared resources', 'asuka-product-designer'); ?></span>
                    <h1><?php esc_html_e('Asset library', 'asuka-product-designer'); ?></h1>
                    <p><?php esc_html_e('Manage reusable artwork, type, production assets, product templates, and the visual system shared across designer experiences.', 'asuka-product-designer'); ?></p>
                </div>
                <a class="button button-secondary" href="<?php echo esc_url(admin_url('admin.php?page=apd-tools')); ?>"><?php esc_html_e('Import or export assets', 'asuka-product-designer'); ?></a>
            </header>

            <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>
                            <span><?php echo esc_html((string) $workspace['count']); ?></span>
                        </div>
                        <p><?php echo esc_html($workspace['copy']); ?></p>
                        <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
    }
}
