<?php

declare(strict_types=1);

namespace App\Framework\AsyncExamples\Http;

/**
 * HTTP Response Objekt
 */
final readonly class HttpResponse
{
    public function __construct(
        public int $statusCode,
        /** @var array<string, string> $headers */
        public array $headers,
        public string $body,
        public float $requestTime
    ) {
    }

    /**
     * Prüft ob Response erfolgreich war
     */
    public function isSuccess(): bool
    {
        return $this->statusCode >= 200 && $this->statusCode < 300;
    }

    /**
     * Dekodiert JSON Response
     * @return array<string, mixed>
     */
    public function json(): array
    {
        return json_decode($this->body, true) ?? [];
    }

    /**
     * Gibt spezifischen Header zurück
     */
    public function getHeader(string $name): ?string
    {
        return $this->headers[$name] ?? null;
    }

    /**
     * Gibt Content-Type zurück
     */
    public function getContentType(): ?string
    {
        return $this->getHeader('Content-Type');
    }

    /**
     * Konvertiert zu Array
     *
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'status_code' => $this->statusCode,
            'headers' => $this->headers,
            'body' => $this->body,
            'request_time' => $this->requestTime,
        ];
    }
}
