<?php

declare(strict_types=1);

namespace App\Framework\ErrorHandling;

use App\Framework\ErrorHandling\Handlers\HandlerResult;

/**
 * Result of processing an exception through the error handler chain
 */
final readonly class ErrorHandlingResult
{
    /**
     * @param bool $handled Whether the exception was handled by any handler
     * @param HandlerResult[] $results Results from all handlers that processed the exception
     * @param \Throwable $exception The original exception
     */
    public function __construct(
        public bool $handled,
        public array $results,
        public \Throwable $exception
    ) {}

    /**
     * Get the first handler result (usually the most important)
     */
    public function getFirstResult(): ?HandlerResult
    {
        return $this->results[0] ?? null;
    }

    /**
     * Get all handler messages
     *
     * @return string[]
     */
    public function getMessages(): array
    {
        return array_map(
            fn(HandlerResult $result) => $result->message,
            $this->results
        );
    }

    /**
     * Get combined data from all handlers
     */
    public function getCombinedData(): array
    {
        return array_merge(
            ...array_map(
                fn(HandlerResult $result) => $result->data,
                $this->results
            )
        );
    }
}
