<?php

namespace App\Framework\HttpClient;

use App\Framework\Http\HeaderKey;
use App\Framework\Http\Headers;
use App\Framework\Http\MimeType;
use App\Framework\Http\Status;

final readonly class ClientResponse
{
    public function __construct(
        public Status  $status,
        public Headers $headers,
        public string  $body
    ) {}

    public function isSuccessful(): bool
    {
        return $this->status->isSuccess();
    }

    public function isJson(): bool
    {
        return str_contains($this->getContentType(), "json");

        return $this->headers->has(HeaderKey::CONTENT_TYPE)
            && in_array(MimeType::APPLICATION_JSON->value, $this->headers->get(HeaderKey::CONTENT_TYPE));
    }

    public function json(): array
    {
        return json_decode($this->body, true);
    }

    public function getContentType():string
    {
        return $this->headers->get(HeaderKey::CONTENT_TYPE)[0];
    }
}
