<?php

namespace App\Infrastructure\Providers;

use App\Domain\Blockchain\Contracts\BlockchainGateway;
use App\Domain\Blockchain\Data\{AddressCursor, AddressRequest, ChainTransaction, DepositAddress, FeeQuote, FeeRequest, ProviderTransfer, TransactionPage, TransferRequest, VerifiedProviderEvent};
use Illuminate\Support\Str;
use RuntimeException;

final class DisabledBlockchainGateway implements BlockchainGateway
{
    public function createDepositAddress(AddressRequest $request): DepositAddress
    {
        $seed = strtoupper(substr(hash('sha256', $request->environment.'|'.$request->network.'|'.$request->invoiceId), 0, 32));

        return new DepositAddress('TST'.$seed, 'sandbox_'.$request->invoiceId);
    }

    public function validateAddress(string $network, string $address): bool
    {
        return strlen(trim($address)) >= 8;
    }

    public function listAddressTransactions(AddressCursor $cursor): TransactionPage
    {
        return new TransactionPage([]);
    }

    public function getTransaction(string $network, string $txHash): ChainTransaction
    {
        throw new RuntimeException('SANDBOX_TRANSACTION_NOT_FOUND');
    }

    public function estimateNetworkFee(FeeRequest $request): FeeQuote
    {
        return new FeeQuote(config('gateway.tron.default_fee_atomic', '1000000'), $request->assetCode);
    }

    public function createTransfer(TransferRequest $request): ProviderTransfer
    {
        return new ProviderTransfer('sandbox_transfer_'.Str::ulid(), 'pending', null);
    }

    public function getTransfer(string $providerTransferId): ProviderTransfer
    {
        return new ProviderTransfer($providerTransferId, 'pending', null);
    }

    public function verifyWebhook(string $rawBody, array $headers): VerifiedProviderEvent
    {
        throw new RuntimeException('BLOCKCHAIN_PROVIDER_NOT_CONFIGURED');
    }
}
