<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Property\Validation;

use PpPhp\Core\Designer\Component\DesignerComponentRuntime;
use PpPhp\Core\Designer\Property\DesignerPropertyRuntime;

final class DesignerPropertyVerifier
{
    public function verify(): array
    {
        $components = new DesignerComponentRuntime();
        $runtime = new DesignerPropertyRuntime($components);

        $button = $components->create('button', 'verify_button', ['text' => 'Submit']);
        $schema = $runtime->schemaFor('button');
        $inspector = $runtime->inspectorFor($button);
        $updated = $runtime->apply($button, ['text' => 'Updated']);
        $summary = $runtime->summary();

        $checks = [
            'editors' => ($summary['editors'] ?? 0) >= 18,
            'schema' => ($schema['componentType'] ?? '') === 'button' && count($schema['sections'] ?? []) >= 8,
            'inspector' => ($inspector['component']['id'] ?? '') === 'verify_button' && count($inspector['sections'] ?? []) >= 8,
            'values' => str_contains(json_encode($inspector), 'Submit'),
            'apply' => (($updated['props']['text'] ?? '') === 'Updated'),
            'actions' => in_array('duplicate', (array) ($inspector['actions'] ?? []), true),
            'componentCoverage' => ($summary['propertySchemas'] ?? 0) >= 18,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) $issues[] = ['check' => $check, 'message' => 'Designer property 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,
            ]),
        ];
    }
}
