<?php

namespace LaraBB\Torrent\UI\Web\Handlers;

use LaraBB\Torrent\Actions\DownloadAction;
use LaraBB\Torrent\UI\Web\Requests\DownloadRequest;
use Rhilip\Bencode\TorrentFile;

class DownloadHandler
{
    public function __construct(
        private readonly DownloadAction $downloadAction
    ) {

    }

    public function __invoke(DownloadRequest $request)
    {
        $return = $this->downloadAction->run($request);

        if(!is_null($return)) {
            return response($return['torrentFile']->dumpToString())->header('Content-Type', 'application/x-bittorrent')
                                                                   ->header('Content-Disposition', 'attachment; filename="' . $return['filename'] . '.torrent"');
        }
    }
}
