<?php

declare(strict_types=1);

namespace App\Framework\Http;

use App\Framework\Http\Responses\Streamable;

final readonly class ResponseManipulator
{
    public function withBody(Response $response, string $body): Response
    {
        // Spezielle Response-Typen erhalten
        if ($response instanceof Streamable) {
            return $response; // SSE-Body wird gestreamt, nicht statisch gesetzt
        }

        return new HttpResponse(
            status: $response->status,
            headers: $response->headers,
            body: $body
        );
    }

    public function withHeader(Response $response, HeaderKey|string $name, string $value): Response
    {
        // Spezielle Response-Typen erhalten
        if ($response instanceof Streamable) {
            return $response; // SSE-Header sind spezifisch und sollten nicht geändert werden
        }

        $headers = clone $response->headers;
        $headers = $headers->with($name, $value);

        return new HttpResponse(
            status: $response->status,
            headers: $headers,
            body: $response->body
        );
    }

    public function withHeaders(Response $response, Headers $headers): Response
    {
        // Spezielle Response-Typen erhalten
        if ($response instanceof Streamable) {
            return $response;
        }

        return new HttpResponse(
            status: $response->status,
            headers: $headers,
            body: $response->body
        );
    }

    public function withStatus(Response $response, Status $status): Response
    {
        // Spezielle Response-Typen erhalten
        if ($response instanceof Streamable) {
            return $response;
        }

        return new HttpResponse(
            status: $status,
            headers: $response->headers,
            body: $response->body
        );
    }
}
