<?php

declare(strict_types=1);

namespace App\Domain\Account\Exceptions;

use App\Domain\Account\Enums\AccountStatus;
use App\Domain\Account\Models\Account;
use App\Domain\Sysapi\Contracts\SysapiResult;
use RuntimeException;
use Throwable;

class AccountException extends RuntimeException
{
    public function __construct(
        string $message,
        public readonly ?Account $account = null,
        public readonly ?SysapiResult $sysapiResult = null,
        int $code = 0,
        ?Throwable $previous = null,
    ) {
        parent::__construct($message, $code, $previous);
    }

    public static function invalidTransition(Account $account, AccountStatus $target): self
    {
        return new self(
            message: "Hesap geçişi geçersiz: {$account->status->value} → {$target->value} (hesap: {$account->username})",
            account: $account,
        );
    }

    public static function alreadyProvisioned(Account $account): self
    {
        return new self(
            message: "Hesap zaten provision edilmiş (uid: {$account->linux_uid}, hesap: {$account->username})",
            account: $account,
        );
    }

    public static function sysapiFailed(Account $account, string $operation, SysapiResult $result): self
    {
        return new self(
            message: "Hesap {$operation} başarısız (hesap: {$account->username}, exit: {$result->exitCode}): {$result->stderr}",
            account: $account,
            sysapiResult: $result,
        );
    }
}
