<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Component\Validation;

use PpPhp\Core\Designer\Component\DesignerComponentRuntime;

final class DesignerComponentVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerComponentRuntime();
        $summary = $runtime->summary();

        $button = $runtime->create('button', 'button_test', ['text' => 'Save']);
        $grid = $runtime->create('data_grid', 'grid_test');
        $toolbox = $runtime->toolbox()->toolbox();
        $search = $runtime->toolbox()->search('grid');

        $checks = [
            'registry' => ($summary['components'] ?? 0) >= 18,
            'categories' => ($summary['categories'] ?? 0) >= 9,
            'factory' => ($button['type'] ?? '') === 'button' && (($button['props']['text'] ?? '') === 'Save'),
            'rendererMap' => $runtime->rendererMap()->rendererFor('data_grid') === 'table_grid',
            'toolbox' => count($toolbox) >= 9,
            'search' => count($search) >= 1 && str_contains(json_encode($search), 'data_grid'),
            'propertySchemas' => ($summary['propertySchemas'] ?? 0) >= 18,
            'instances' => ($grid['id'] ?? '') === 'grid_test',
        ];

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

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