<?php

declare(strict_types=1);

namespace App\Framework\Console\ErrorRecovery;

final readonly class ErrorRecoveryAnalysis
{
    /**
     * @param \Throwable $error
     * @param string[] $recoveryOptions
     */
    public function __construct(
        public \Throwable $error,
        public array $recoveryOptions
    ) {
    }

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

    public function getRecoveryOptions(): array
    {
        return $this->recoveryOptions;
    }

    public function getErrorType(): string
    {
        return get_class($this->error);
    }

    public function getErrorMessage(): string
    {
        return $this->error->getMessage();
    }

    public function isRecoverable(): bool
    {
        return $this->hasRecoveryOptions() &&
               ! $this->isCriticalError();
    }

    private function isCriticalError(): bool
    {
        return $this->error instanceof \Error ||
               str_contains($this->error->getMessage(), 'Fatal') ||
               str_contains($this->error->getMessage(), 'Critical');
    }
}
