<?php

declare(strict_types=1);

namespace App\Framework\HttpClient;

use App\Framework\Async\AsyncService;
use App\Framework\HttpClient\Contracts\AsyncCapable;

/**
 * Decorator der jede HttpClient-Implementierung um async Property erweitert
 *
 * Usage: $httpClient->async->sendMultiple([...])
 */
final readonly class AsyncAwareHttpClient implements HttpClient, AsyncCapable
{
    private AsyncHttpClientAdapter $asyncAdapter;

    public function __construct(
        private HttpClient $httpClient,
        private AsyncService $asyncService
    ) {
        $this->asyncAdapter = new AsyncHttpClientAdapter($this->httpClient, $this->asyncService);
    }

    // === STANDARD HTTP CLIENT INTERFACE (Delegation) ===

    public function send(ClientRequest $request): ClientResponse
    {
        return $this->httpClient->send($request);
    }

    // === ASYNC CAPABILITY ===

    /**
     * Get async adapter for parallel operations
     */
    public function async(): AsyncHttpClientAdapter
    {
        return $this->asyncAdapter;
    }

    /**
     * Get wrapped HTTP client instance
     */
    public function getWrappedClient(): HttpClient
    {
        return $this->httpClient;
    }

    /**
     * Get statistics including async metrics
     */
    public function getStats(): array
    {
        $baseStats = method_exists($this->httpClient, 'getStats') ? $this->httpClient->getStats() : [];

        return array_merge($baseStats, [
            'async_enabled' => true,
            'async_stats' => $this->asyncAdapter->getStats(),
            'underlying_client' => get_class($this->httpClient),
        ]);
    }
}
