<?php
declare(strict_types=1);

namespace PpPhp\Core\Designer\Auth\Validation;

use PpPhp\Core\Designer\Auth\DesignerAuthRuntime;

final class DesignerAuthVerifier
{
    public function verify(): array
    {
        $runtime = new DesignerAuthRuntime();
        $suite = $runtime->createSuite();
        $loginHtml = $runtime->render($suite['login']);
        $profile = $runtime->validate($suite['profile']);
        $stats = $runtime->stats($suite);

        $checks = [
            'suite' => count($suite) === 8,
            'login' => ($suite['login']['pageType'] ?? '') === 'login',
            'register' => count($suite['register']['components'][0]['props']['fields'] ?? []) === 4,
            'profileSecurity' => ($profile['csrf'] ?? false) === true && (($suite['profile']['components'][0]['props']['security']['authRequired'] ?? false) === true),
            'guestSecurity' => (($suite['login']['components'][0]['props']['security']['guestOnly'] ?? false) === true),
            'renderer' => str_contains($loginHtml, 'ppd-auth-page') && str_contains($loginHtml, 'data-auth-action'),
            'statistics' => ($stats['pages'] ?? 0) === 8,
        ];

        $issues = [];
        foreach ($checks as $check => $ok) {
            if (!$ok) {
                $issues[] = ['check' => $check, 'message' => 'Designer auth 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)),
                'score' => in_array(false, $checks, true) ? 80 : 100,
            ]),
        ];
    }
}
