<?php

declare(strict_types=1);

namespace App\Framework\MagicLinks\ValueObjects;

final readonly class ErrorCollection
{
    /** @param array<string> $errors */
    private function __construct(
        private array $errors
    ) {
    }

    public static function empty(): self
    {
        return new self([]);
    }

    public static function fromArray(array $errors): self
    {
        return new self($errors);
    }

    public static function single(string $error): self
    {
        return new self([$error]);
    }

    public function add(string $error): self
    {
        $errors = $this->errors;
        $errors[] = $error;

        return new self($errors);
    }

    public function addMultiple(array $newErrors): self
    {
        return new self(array_merge($this->errors, $newErrors));
    }

    public function hasErrors(): bool
    {
        return ! empty($this->errors);
    }

    public function isEmpty(): bool
    {
        return empty($this->errors);
    }

    public function toArray(): array
    {
        return $this->errors;
    }

    public function count(): int
    {
        return count($this->errors);
    }

    public function first(): ?string
    {
        return $this->errors[0] ?? null;
    }

    public function toString(string $separator = ', '): string
    {
        return implode($separator, $this->errors);
    }
}
