<?php
declare(strict_types=1);

namespace App\Application\Media;

use App\Framework\Attributes\Route;
use App\Framework\Core\PathProvider;
use App\Framework\Http\Request;
use App\Framework\Http\Responses\MediaType;
use App\Framework\Http\Responses\StreamResponse;
use App\Framework\Http\Streaming\MimeTypeDetector;
use App\Framework\Http\Streaming\RangeParser;

final readonly class ShowVideo
{
    public function __construct(
        private PathProvider $pathProvider,
    ) {}

    #[Route('/videos/{filename}')]
    public function __invoke(string $filename): StreamResponse
    {
        $filePath = $this->pathProvider->resolvePath('/storage/uploads/videos/'.$filename);

        #dd($path);

        #$filePath = "/media/{$filename}";
        $fileSize = filesize($filePath);
        $mimeType = MimeTypeDetector::detect($filePath);

        $range = null;
        if (isset($_SERVER['HTTP_RANGE'])) {
            $range = RangeParser::parseRange($_SERVER['HTTP_RANGE'], $fileSize);
        }

        return new StreamResponse(
            filePath : $filePath,
            fileSize : $fileSize,
            mimeType : $mimeType,
            range    : $range,
            mediaType: MediaType::fromMimeType($mimeType)
        );

    }
}
