<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Dashboard;

final class DesignerDashboardRenderer
{
    public function render(array $dashboardComponent): string
    {
        $props = (array) ($dashboardComponent['props'] ?? $dashboardComponent);
        $widgets = (array) ($props['widgets'] ?? []);

        $html = [];
        $html[] = '<section class="ppd-dashboard" data-dashboard-id="' . $this->e((string) ($props['dashboardId'] ?? $props['id'] ?? 'dashboard')) . '">';
        $html[] = '<header class="ppd-dashboard-header"><h2>' . $this->e((string) ($props['title'] ?? 'Dashboard')) . '</h2><button type="button" data-dashboard-refresh>Refresh</button></header>';
        $html[] = '<div class="ppd-dashboard-grid">';

        foreach ($widgets as $widget) {
            $html[] = $this->renderWidget((array) $widget);
        }

        $html[] = '</div></section>';

        return implode('', $html);
    }

    private function renderWidget(array $widget): string
    {
        $type = (string) ($widget['type'] ?? 'widget');
        $span = (int) ($widget['span'] ?? $widget['layout']['span'] ?? 4);
        $props = (array) ($widget['props'] ?? []);

        $html = '<article class="ppd-dashboard-widget ppd-dashboard-widget-' . $this->e($type) . ' ppd-span-' . $span . '" data-widget-id="' . $this->e((string) ($widget['id'] ?? 'widget')) . '">';

        if ($type === 'stat_card') {
            $html .= '<div class="ppd-stat-icon"><i class="' . $this->e((string) ($props['icon'] ?? 'bi-bar-chart')) . '"></i></div>';
            $html .= '<div class="ppd-stat-body"><span>' . $this->e((string) ($widget['title'] ?? 'Stat')) . '</span><strong>' . $this->e((string) ($props['value'] ?? '0')) . '</strong><small>' . $this->e((string) ($props['trend'] ?? '')) . '</small></div>';
        } elseif ($type === 'chart') {
            $html .= '<h3>' . $this->e((string) ($widget['title'] ?? 'Chart')) . '</h3><div class="ppd-chart-placeholder" data-chart-type="' . $this->e((string) ($props['chartType'] ?? 'bar')) . '">' . $this->e(strtoupper((string) ($props['chartType'] ?? 'bar'))) . ' CHART</div>';
        } elseif ($type === 'quick_action') {
            $html .= '<a class="ppd-quick-action" href="#' . $this->e((string) ($props['target'] ?? '')) . '"><i class="' . $this->e((string) ($props['icon'] ?? 'bi-lightning')) . '"></i><span>' . $this->e((string) ($widget['title'] ?? 'Action')) . '</span></a>';
        } elseif ($type === 'activity_feed') {
            $html .= '<h3>' . $this->e((string) ($widget['title'] ?? 'Recent Activity')) . '</h3><ul class="ppd-activity-feed">';
            foreach ((array) ($props['items'] ?? []) as $item) {
                $html .= '<li>' . $this->e(is_array($item) ? (string) ($item['label'] ?? '') : (string) $item) . '</li>';
            }
            if (($props['items'] ?? []) === []) {
                $html .= '<li>' . $this->e((string) ($props['emptyText'] ?? 'No activity')) . '</li>';
            }
            $html .= '</ul>';
        } else {
            $html .= '<h3>' . $this->e((string) ($widget['title'] ?? 'Widget')) . '</h3><div class="ppd-embedded-widget">' . $this->e($type) . '</div>';
        }

        return $html . '</article>';
    }

    private function e(string $value): string
    {
        return htmlspecialchars($value, ENT_QUOTES, 'UTF-8');
    }
}
