<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Visual\Validation;

use PpPhp\Core\Designer\Visual\DesignerCanvasRuntime;
use PpPhp\Core\Designer\Visual\DesignerFieldsIntegrationRuntime;
use PpPhp\Core\Designer\Visual\DesignerHeaderVisualRuntime;
use PpPhp\Core\Designer\Visual\DesignerInspectorVisualRuntime;
use PpPhp\Core\Designer\Visual\DesignerRuntimeHealth;
use PpPhp\Core\Designer\Visual\DesignerSelectionOverlayRuntime;
use PpPhp\Core\Designer\Visual\DesignerToolboxVisualRuntime;
use PpPhp\Core\Designer\Visual\DesignerVisualRuntime;

final class DesignerVisualVerifier
{
    public function verify(): array
    {
        $page = ['pageId'=>'customers_list','pageType'=>'list','components'=>[]];
        $target = ['id'=>'customers_grid','type'=>'data_grid','parentPath'=>['page','row']];
        $fields = [['name'=>'id','type'=>'int','label'=>'Id'], ['name'=>'email','type'=>'email','label'=>'Email']];
        $components = [['id'=>'data_grid','label'=>'Data Grid','category'=>'Data'], ['id'=>'form','label'=>'Form','category'=>'Forms'], ['id'=>'dashboard','label'=>'Dashboard','category'=>'Dashboard']];

        $canvas = (new DesignerCanvasRuntime())->clean($page, 'normal');
        $overlay = (new DesignerSelectionOverlayRuntime())->target('customers_grid', 'data_grid', ['page', 'row']);
        $inspector = (new DesignerInspectorVisualRuntime())->inspectorFor($target);
        $toolbox = (new DesignerToolboxVisualRuntime())->viewModel($components);
        $fieldsRuntime = new DesignerFieldsIntegrationRuntime();
        $gridColumn = $fieldsRuntime->fieldToGridColumn($fields[1]);
        $formField = $fieldsRuntime->fieldToFormControl($fields[1]);
        $header = (new DesignerHeaderVisualRuntime())->viewModel();
        $health = (new DesignerRuntimeHealth())->check();
        $visual = (new DesignerVisualRuntime())->compose($page, $target, $fields, $components);

        $checks = [
            'canvasCleanup' => ($canvas['showEmptySlots'] ?? true) === false && ($canvas['componentBorders'] ?? '') === 'minimal',
            'selectionOverlay' => isset($overlay['states']['selected']) && ($overlay['classes']['selected'] ?? '') === 'designer-selected',
            'inspectorV3' => ($inspector['title'] ?? '') === 'Grid Properties' && ($inspector['mode'] ?? '') === 'schema-driven',
            'toolboxV2' => ($toolbox['search']['enabled'] ?? false) === true && count($toolbox['groups'] ?? []) >= 8,
            'fieldsIntegration' => ($gridColumn['type'] ?? '') === 'grid_column' && ($formField['type'] ?? '') === 'field',
            'headerModernization' => isset($header['groups']['build']) && count($header['groups']['build']) === 2,
            'runtimeHealth' => ($health['status'] ?? '') === 'PASS' && ($health['passed'] ?? 0) >= 15,
            'runtimeIntegration' => isset($visual['canvas'], $visual['selection'], $visual['inspector'], $visual['toolbox'], $visual['health']),
        ];

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

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'summary' => [
                'checks'=>count($checks),
                'passed'=>count(array_filter($checks)),
                'failed'=>count($checks)-count(array_filter($checks)),
                'canvasCleanup'=>$checks['canvasCleanup'],
                'selectionOverlay'=>$checks['selectionOverlay'],
                'inspectorV3'=>$checks['inspectorV3'],
                'toolboxV2'=>$checks['toolboxV2'],
                'fieldsIntegration'=>$checks['fieldsIntegration'],
                'runtimeHealth'=>$checks['runtimeHealth'],
                'runtimeIntegration'=>$checks['runtimeIntegration'],
                'score'=>in_array(false, $checks, true) ? 80 : 100,
            ],
        ];
    }
}
