<?php

namespace App\Services\Certification;

final class EnterpriseCertificationEngine
{
    public static function run(): array
    {
        $results = [];

        foreach (CertificationRegistry::enabled() as $key => $module) {

            $label = (string)($module['label'] ?? $key);
            $service = $module['service'] ?? null;
            $method = $module['method'] ?? null;

            if ($service === null) {
                $results[$key] = CertificationResult::pass(
                    $label,
                    [
                        'note' => 'Certification engine foundation is active.'
                    ]
                );
                continue;
            }

            if (!class_exists($service)) {
                $results[$key] = CertificationResult::fail(
                    $label,
                    [
                        'Certification service not found: ' . $service
                    ]
                );
                continue;
            }

            if (!method_exists($service, $method)) {
                $results[$key] = CertificationResult::fail(
                    $label,
                    [
                        'Certification method not found: ' . $service . '::' . $method
                    ]
                );
                continue;
            }

            try {

                $raw = $service::$method();

                $score = (int)($raw['score'] ?? 100);
                $warnings = (array)($raw['warnings'] ?? []);
                $errors = (array)($raw['errors'] ?? []);

                if (!empty($errors)) {
                    $results[$key] = CertificationResult::fail(
                        $label,
                        $errors,
                        $warnings,
                        $raw
                    );
                } elseif (!empty($warnings) || $score < 100) {
                    $results[$key] = CertificationResult::warning(
                        $label,
                        $score,
                        $warnings,
                        $raw
                    );
                } else {
                    $results[$key] = CertificationResult::pass(
                        $label,
                        $raw
                    );
                }

            } catch (\Throwable $e) {

                $results[$key] = CertificationResult::fail(
                    $label,
                    [
                        $e->getMessage()
                    ]
                );

            }
        }

        return self::summary($results);
    }

    private static function summary(array $results): array
    {
        $total = count($results);
        $scoreSum = 0;
        $passed = 0;
        $warnings = 0;
        $failed = 0;

        foreach ($results as $r) {
            $scoreSum += (int)($r['score'] ?? 0);

            if (($r['status'] ?? '') === 'PASS') {
                $passed++;
            } elseif (($r['status'] ?? '') === 'WARNING') {
                $warnings++;
            } else {
                $failed++;
            }
        }

        $overall = $total > 0
            ? (int)round($scoreSum / $total)
            : 100;

        return [
            'overall_score' => $overall,
            'status' => $failed > 0
                ? 'FAIL'
                : ($warnings > 0 ? 'WARNING' : 'PASS'),
            'total_modules' => $total,
            'passed' => $passed,
            'warnings' => $warnings,
            'failed' => $failed,
            'results' => $results
        ];
    }
}
