<?php

namespace LaraBB\Torrent\UI\Web\Handlers;

use Illuminate\Http\RedirectResponse;
use LaraBB\Torrent\Actions\StoreAction;
use LaraBB\Torrent\UI\Web\Requests\StoreRequest;

/**
 *
 */
class StoreHandler
{
    /**
     * @param StoreAction $storeAction
     */
    public function __construct(
        private readonly StoreAction $storeAction
    ) {

    }

    /**
     * @param StoreRequest $request
     * @return RedirectResponse
     */
    public function __invoke(StoreRequest $request): RedirectResponse
    {
        $torrent = $this->storeAction->run($request);

        return !$torrent ? $request->failed() : $request->success($torrent->uuid);
    }
}
