<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Build\Validation;

use PpPhp\Core\Designer\Build\DesignerBuildRuntime;
use PpPhp\Core\Designer\Build\DesignerExportRuntime;

final class DesignerBuildVerifier
{
    public function verify(): array
    {
        $tables = [
            'customers' => [
                ['name' => 'id', 'type' => 'number'],
                ['name' => 'name', 'type' => 'text'],
                ['name' => 'email', 'type' => 'email'],
            ],
            'orders' => [
                ['name' => 'id', 'type' => 'number'],
                ['name' => 'customer', 'type' => 'text'],
                ['name' => 'total', 'type' => 'currency'],
            ],
        ];

        $buildRuntime = new DesignerBuildRuntime();
        $manifest = $buildRuntime->buildCrudApplication('crm_app', 'CRM Application', $tables);
        $buildStats = $buildRuntime->stats($manifest);

        $exportRuntime = new DesignerExportRuntime();
        $package = $exportRuntime->export($manifest);
        $exportStats = $exportRuntime->stats($package);

        $manifestArray = $manifest->toArray();
        $packageArray = $package->toArray();

        $checks = [
            'manifest' => ($manifestArray['applicationId'] ?? '') === 'crm_app',
            'pages' => ($buildStats['generatedPages'] ?? 0) >= 17,
            'routes' => ($buildStats['routes'] ?? 0) >= 17,
            'assets' => ($buildStats['assets'] ?? 0) >= 4,
            'artifacts' => ($buildStats['artifacts'] ?? 0) >= 23,
            'manifestArtifact' => str_contains(json_encode($manifestArray), 'manifest.json'),
            'exportPackage' => ($packageArray['archiveName'] ?? '') === 'crm_app-export.zip',
            'writePlan' => ($exportStats['writePlan'] ?? 0) >= 23,
            'checksums' => str_contains(json_encode($packageArray['writePlan'] ?? []), 'checksum'),
            'operations' => ($buildStats['operations'] ?? 0) >= 1 && ($exportStats['operations'] ?? 0) >= 1,
        ];

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

        return [
            'ok' => !in_array(false, $checks, true),
            'checks' => $checks,
            'issues' => $issues,
            'manifest' => $manifestArray,
            'package' => $packageArray,
            'summary' => [
                'checks' => count($checks),
                'passed' => count(array_filter($checks)),
                'failed' => count($checks) - count(array_filter($checks)),
                'generatedPages' => $buildStats['generatedPages'] ?? 0,
                'routes' => $buildStats['routes'] ?? 0,
                'assets' => $buildStats['assets'] ?? 0,
                'artifacts' => $buildStats['artifacts'] ?? 0,
                'exportPackages' => $exportStats['packages'] ?? 0,
                'writePlan' => $exportStats['writePlan'] ?? 0,
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ],
        ];
    }
}
