<?php
declare(strict_types=1);

namespace Itdatex\EInvoiceCore;

/**
 * Validierungsergebnis — Format + Issue-Liste.
 */
final class ValidationReport
{
    /**
     * @param ValidationIssue[] $issues
     */
    public function __construct(
        public readonly Format $format,
        public readonly array $issues,
    ) {}

    public function isValid(): bool
    {
        foreach ($this->issues as $i) {
            if ($i->severity === 'error') return false;
        }
        return true;
    }

    public function errorCount(): int
    {
        return count(array_filter($this->issues, fn($i) => $i->severity === 'error'));
    }

    public function warningCount(): int
    {
        return count(array_filter($this->issues, fn($i) => $i->severity === 'warning'));
    }

    public function toArray(): array
    {
        return [
            'format'    => $this->format->value,
            'valid'     => $this->isValid(),
            'errors'    => $this->errorCount(),
            'warnings'  => $this->warningCount(),
            'issues'    => array_map(fn($i) => $i->toArray(), $this->issues),
        ];
    }
}
