<?php
/**
 * Download Statistics Widget Controller
 *
 * @package Jeremias\WcLicensedProduct\Admin
 */

declare(strict_types=1);

namespace Jeremias\WcLicensedProduct\Admin;

use Jeremias\WcLicensedProduct\Product\VersionManager;

/**
 * Handles the WordPress admin dashboard widget for download statistics
 */
final class DownloadWidgetController
{
    private VersionManager $versionManager;

    public function __construct(VersionManager $versionManager)
    {
        $this->versionManager = $versionManager;
        $this->registerHooks();
    }

    /**
     * Register WordPress hooks
     */
    private function registerHooks(): void
    {
        add_action('wp_dashboard_setup', [$this, 'registerDashboardWidget']);
    }

    /**
     * Register the dashboard widget
     */
    public function registerDashboardWidget(): void
    {
        if (!current_user_can('manage_woocommerce')) {
            return;
        }

        wp_add_dashboard_widget(
            'wclp_download_statistics',
            __('Download Statistics', 'wc-licensed-product'),
            [$this, 'renderWidget']
        );
    }

    /**
     * Render the dashboard widget content
     */
    public function renderWidget(): void
    {
        $stats = $this->versionManager->getDownloadStatistics();
        ?>
        <style>
            .wclp-download-widget-stats {
                display: grid;
                grid-template-columns: 1fr;
                gap: 12px;
                margin-bottom: 16px;
            }
            .wclp-download-stat-card {
                background: #f8f9fa;
                border: 1px solid #e2e4e7;
                border-radius: 4px;
                padding: 12px;
                text-align: center;
                border-left: 3px solid #2271b1;
            }
            .wclp-download-stat-number {
                font-size: 32px;
                font-weight: 600;
                color: #1d2327;
                line-height: 1.2;
            }
            .wclp-download-stat-label {
                font-size: 12px;
                color: #646970;
                text-transform: uppercase;
                letter-spacing: 0.5px;
                margin-top: 4px;
            }
            .wclp-download-list {
                margin: 0;
                padding: 0;
                list-style: none;
            }
            .wclp-download-list li {
                display: flex;
                justify-content: space-between;
                align-items: center;
                padding: 8px 0;
                border-bottom: 1px solid #e2e4e7;
            }
            .wclp-download-list li:last-child {
                border-bottom: none;
            }
            .wclp-download-list .product-name {
                font-weight: 500;
                color: #1d2327;
                flex: 1;
                overflow: hidden;
                text-overflow: ellipsis;
                white-space: nowrap;
                margin-right: 12px;
            }
            .wclp-download-list .version-info {
                font-size: 12px;
                color: #646970;
            }
            .wclp-download-list .download-count {
                background: #e7f5ff;
                color: #0a4b78;
                padding: 2px 8px;
                border-radius: 10px;
                font-size: 12px;
                font-weight: 600;
                white-space: nowrap;
            }
            .wclp-download-section-title {
                margin: 16px 0 8px 0;
                font-size: 13px;
                color: #1d2327;
                font-weight: 600;
            }
            .wclp-no-downloads {
                color: #646970;
                font-style: italic;
                text-align: center;
                padding: 12px 0;
            }
        </style>

        <div class="wclp-download-widget-stats">
            <div class="wclp-download-stat-card">
                <div class="wclp-download-stat-number"><?php echo esc_html(number_format_i18n($stats['total'])); ?></div>
                <div class="wclp-download-stat-label"><?php esc_html_e('Total Downloads', 'wc-licensed-product'); ?></div>
            </div>
        </div>

        <h4 class="wclp-download-section-title">
            <?php esc_html_e('Top Products', 'wc-licensed-product'); ?>
        </h4>
        <?php if (!empty($stats['by_product'])): ?>
            <ul class="wclp-download-list">
                <?php foreach (array_slice($stats['by_product'], 0, 5) as $product): ?>
                    <li>
                        <span class="product-name"><?php echo esc_html($product['product_name']); ?></span>
                        <span class="download-count">
                            <?php echo esc_html(number_format_i18n($product['downloads'])); ?>
                        </span>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p class="wclp-no-downloads"><?php esc_html_e('No downloads yet', 'wc-licensed-product'); ?></p>
        <?php endif; ?>

        <h4 class="wclp-download-section-title">
            <?php esc_html_e('Top Versions', 'wc-licensed-product'); ?>
        </h4>
        <?php if (!empty($stats['by_version'])): ?>
            <ul class="wclp-download-list">
                <?php foreach (array_slice($stats['by_version'], 0, 5) as $version): ?>
                    <li>
                        <span class="product-name">
                            <?php echo esc_html($version['product_name']); ?>
                            <span class="version-info">v<?php echo esc_html($version['version']); ?></span>
                        </span>
                        <span class="download-count">
                            <?php echo esc_html(number_format_i18n($version['downloads'])); ?>
                        </span>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p class="wclp-no-downloads"><?php esc_html_e('No downloads yet', 'wc-licensed-product'); ?></p>
        <?php endif; ?>
        <?php
    }
}
