<?php

namespace App\Download\Action\Handler;

use App\Download\Action\Command\PauseDownloadCommand;
use App\Download\Action\Result\PauseDownloadResult;
use App\Download\Framework\Entity\Download;
use App\Download\Framework\Repository\DownloadRepository;
use OneToMany\RichBundle\Contract\CommandInterface;
use OneToMany\RichBundle\Contract\HandlerInterface;
use OneToMany\RichBundle\Contract\ResultInterface;
use Symfony\Contracts\Cache\CacheInterface;

/** @implements HandlerInterface<PauseDownloadCommand, PauseDownloadResult> */
readonly class PauseDownloadHandler implements HandlerInterface
{
    public function __construct(
        private DownloadRepository $downloadRepository,
        private CacheInterface $cache,
    ) {}

    public function handle(CommandInterface $command): ResultInterface
    {
        /** @var Download $download */
        $download = $this->downloadRepository->find($command->downloadId);

        $this->cache->get('download.pause.' . $download->getId(), function () {
            return true;
        });

        return new PauseDownloadResult(200, 'Success', $download);
    }
}
