<?php

declare(strict_types=1);

namespace App\Domain\Database\Exceptions;

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

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

    public static function invalidName(string $name): self
    {
        return new self("Geçersiz veritabanı/kullanıcı adı: '{$name}' (sadece a-z, 0-9, alt çizgi; 3-32 karakter)");
    }

    public static function alreadyExists(string $name): self
    {
        return new self("Bu isimde bir veritabanı/kullanıcı zaten var: {$name}");
    }

    public static function packageLimitExceeded(int $current, int $max): self
    {
        return new self("Paket limiti aşıldı: {$current}/{$max} veritabanı");
    }

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