<?php
declare(strict_types=1);

namespace PpPhp\Core\IconManager\Validation;

use PpPhp\Core\IconManager\IconManager;
use PpPhp\Core\IconManager\IconRuntime;
use PpPhp\Core\Resources\ResourceRegistry;

final class DesignerIconVerifier
{
    public function verify(): array
    {
        ResourceRegistry::reset();

        $runtime = new IconRuntime();
        $stats = $runtime->stats();
        $packs = $runtime->packs();
        $search = $runtime->search('user', null, 20);
        $picker = $runtime->picker('save', null, 20);
        $svg = $runtime->search('company', 'custom-svg', 10);
        $assets = IconManager::assets();

        $checks = [
            'registry' => ($stats['packs'] ?? 0) >= 5,
            'providers' => isset($packs['bootstrap-icons'], $packs['heroicons'], $packs['material-symbols'], $packs['custom-svg']),
            'search' => count($search) >= 2,
            'picker' => ($picker['total'] ?? 0) >= 1 && count($picker['groups'] ?? []) >= 1,
            'customSvg' => count($svg) >= 1 && ($stats['svgIcons'] ?? 0) >= 1,
            'assets' => count($assets['css'] ?? []) >= 1,
            'statistics' => ($stats['icons'] ?? 0) >= 20,
            'hydration' => $runtime->find('bi-person', 'bootstrap-icons') !== null,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Designer icon verification failed: ' . $check];
            }
        }

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => array_replace($stats, [
                'checks' => count($checks),
                'passed' => count(array_filter($checks)),
                'failed' => count($checks) - count(array_filter($checks)),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ]),
        ];
    }
}
