<?php

declare(strict_types=1);

namespace App\Domain\Security\Ssh\Exceptions;

use App\Domain\Sysapi\Contracts\SysapiResult;
use RuntimeException;

class SshKeyException extends RuntimeException
{
    public function __construct(string $message, public readonly ?SysapiResult $sysapiResult = null)
    {
        parent::__construct($message);
    }

    public static function invalidKey(string $reason = ''): self
    {
        $msg = 'Geçersiz SSH anahtarı.';
        if ($reason !== '') {
            $msg .= " ({$reason})";
        }

        return new self($msg);
    }

    public static function notFound(string $fingerprint): self
    {
        return new self("SSH anahtarı bulunamadı: {$fingerprint}");
    }

    public static function sysapiFailed(string $op, SysapiResult $r): self
    {
        return new self("SSH key {$op} başarısız (exit {$r->exitCode}): {$r->stderr}", $r);
    }
}
