<?php
declare(strict_types=1);

namespace PpPhp\Core\Platform\DI\Validation;

final class ValidationResult
{
    private array $issues = [];

    public function add(ValidationIssue $issue): void { $this->issues[] = $issue; }
    public function issues(): array { return $this->issues; }

    public function errors(): array
    {
        return array_values(array_filter($this->issues, static fn (ValidationIssue $i): bool => $i->severity() === 'error'));
    }

    public function warnings(): array
    {
        return array_values(array_filter($this->issues, static fn (ValidationIssue $i): bool => $i->severity() === 'warning'));
    }

    public function ok(): bool { return count($this->errors()) === 0; }

    public function score(): int
    {
        return max(0, 100 - count($this->errors()) * 30 - count($this->warnings()) * 10);
    }

    public function toArray(): array
    {
        return [
            'ok' => $this->ok(),
            'score' => $this->score(),
            'errors' => count($this->errors()),
            'warnings' => count($this->warnings()),
            'issues' => array_map(static fn (ValidationIssue $i): array => $i->toArray(), $this->issues),
        ];
    }
}
