<?php

declare(strict_types=1);

namespace App\Framework\Http\Responses;

use App\Framework\Http\HeaderKey;
use App\Framework\Http\Headers;
use App\Framework\Http\Response;
use App\Framework\Http\Status;
use App\Framework\Http\Streaming\AdaptivePlaylist;
use App\Framework\Http\Streaming\StreamingFormat;

final readonly class AdaptiveStreamResponse implements Response, Streamable
{
    public string $body;

    public Headers $headers;

    public function __construct(
        public AdaptivePlaylist $playlist,
        public Status $status = Status::OK,
        Headers $headers = new Headers(),
        public StreamingFormat $format = StreamingFormat::HLS
    ) {
        $this->body = $this->playlist->generate($this->format);

        $this->headers = $headers
                            ->with(HeaderKey::CACHE_CONTROL, 'max-age=60', )
                            ->with(HeaderKey::ACCESS_CONTROL_ALLOW_ORIGIN, '*')
                            ->with(HeaderKey::ACCESS_CONTROL_ALLOW_METHODS, 'GET, HEAD, OPTIONS')
                            ->with(HeaderKey::ACCESS_CONTROL_ALLOW_HEADERS, 'RANGE');

        // Format-spezifische Header setzen
        #$this->headers = $this->headers->with('Content-Type', $this->format->getContentType());

        // Standard Streaming-Header
        #$this->headers = $this->headers
        #    ->with('Cache-Control', 'max-age=60')
        #    ->with('Access-Control-Allow-Origin', '*')
        #    ->with('Access-Control-Allow-Methods', 'GET, HEAD, OPTIONS')
        #    ->with('Access-Control-Allow-Headers', 'Range');
    }
}
