<?php

declare(strict_types=1);

namespace Plugin\tnt_mcp\src\Mcp\Guide;

use RuntimeException;

/**
 * Carries every error found in a load, not the first one. Loading is all-or-nothing — a half-valid
 * guide catalog is never handed out, because `get_guide` promises its enum matches what is on disk
 * — so this message is the one chance to tell the author everything that is wrong.
 */
final class GuideValidationException extends RuntimeException
{
    /**
     * @param list<GuideValidationError> $errors
     */
    private function __construct(private readonly array $errors, string $message)
    {
        parent::__construct($message);
    }

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

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

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