<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Navigation\Validation;

use PpPhp\Core\Designer\Navigation\DesignerNavigationRuntime;

final class DesignerNavigationVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerNavigationRuntime();

        $menu = $runtime->createMenu('main_menu', 'Main Menu', 'sidebar');
        $menu = $runtime->addItem($menu, ['id' => 'dashboard', 'label' => 'Dashboard', 'type' => 'dashboard', 'target' => 'dashboard']);
        $menu = $runtime->addItem($menu, ['id' => 'customers', 'label' => 'Customers', 'type' => 'section', 'target' => 'customers']);
        $menu = $runtime->addItem($menu, ['id' => 'customers_list', 'label' => 'Customer List', 'type' => 'page', 'target' => 'customers_list'], 'customers');
        $menu = $runtime->moveItem($menu, 'customers', 0);

        $pages = [
            ['pageId' => 'dashboard', 'title' => 'Dashboard', 'pageType' => 'dashboard'],
            ['pageId' => 'customers_list', 'title' => 'Customers', 'pageType' => 'list'],
            ['pageId' => 'customers_add', 'title' => 'Add Customer', 'pageType' => 'add'],
        ];

        $fromPages = $runtime->fromPages('generated_menu', $pages);
        $routes = $runtime->routesFromPages($pages);
        $shell = $runtime->applicationShell('demo_app', $pages);
        $html = $runtime->render($menu);
        $validation = $runtime->validate($menu);
        $stats = $runtime->stats();

        $checks = [
            'menu' => ($menu['type'] ?? '') === 'navigation_menu',
            'items' => ($validation['itemCount'] ?? 0) === 3,
            'nested' => str_contains(json_encode($menu), 'customers_list'),
            'move' => (($menu['props']['items'][0]['id'] ?? '') === 'customers'),
            'fromPages' => (($fromPages['props']['itemCount'] ?? 0) === 3),
            'routes' => count($routes) === 3,
            'shell' => ($shell['type'] ?? '') === 'application_shell' && count($shell['routes'] ?? []) === 3,
            'renderer' => str_contains($html, 'ppd-navigation') && str_contains($html, 'data-nav-id'),
            'validation' => ($validation['ok'] ?? false) === true,
            'operations' => ($stats['operations'] ?? 0) >= 8,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Designer navigation 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)),
                'items' => $validation['itemCount'] ?? 0,
                'routes' => count($routes),
                'shellComponents' => count($shell['components'] ?? []),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ]),
        ];
    }
}
