<?php

declare(strict_types=1);

namespace App\Business\DataImport;

final readonly class DataImportPrecheckResult
{
    /**
     * @param  list<DataImportRowPrecheckOutcome>  $rowOutcomes
     */
    public function __construct(
        public array $rowOutcomes,
        public int $validatedCount,
        public int $failedCount,
    ) {}

    public function passed(): bool
    {
        return $this->failedCount === 0 && $this->validatedCount > 0;
    }

    /**
     * @return list<string>
     */
    public function summaryErrors(int $limit = 20): array
    {
        $errors = [];

        foreach ($this->rowOutcomes as $outcome) {
            if ($outcome->passed()) {
                continue;
            }

            foreach ($outcome->errorMessages as $message) {
                $errors[] = $message;

                if (count($errors) >= $limit) {
                    return $errors;
                }
            }
        }

        return $errors;
    }
}
