<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Action;

use RuntimeException;

/**
 * Carries every error found in a load, not the first one. Loading is eager and all-or-nothing —
 * a half-valid catalog is never handed out — so the one chance to tell the author what is wrong is
 * this message, and stopping at error one would turn a single fix-up into a dozen round trips.
 */
final class CatalogValidationException extends RuntimeException
{
    /**
     * @param list<CatalogValidationError> $errors
     */
    private function __construct(private readonly array $errors, string $message)
    {
        parent::__construct($message);
    }

    /**
     * @param list<CatalogValidationError> $errors
     */
    public static function fromErrors(array $errors): self
    {
        $lines = \array_map(
            static fn (CatalogValidationError $error): string => $error->describe(),
            $errors,
        );

        return new self(
            $errors,
            \sprintf("Action catalog is invalid (%d error(s)):\n%s", \count($errors), \implode("\n", $lines)),
        );
    }

    /** @return list<CatalogValidationError> */
    public function errors(): array
    {
        return $this->errors;
    }
}
