<?php

namespace App\Twig\Components;

use App\Download\Framework\Repository\DownloadRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\UX\LiveComponent\Attribute\AsLiveComponent;
use Symfony\UX\LiveComponent\Attribute\LiveProp;
use Symfony\UX\LiveComponent\DefaultActionTrait;

#[AsLiveComponent]
final class DownloadList extends AbstractController
{
    use DefaultActionTrait;

    use PaginateTrait;

    #[LiveProp(writable: true)]
    public string $term = "";

    #[LiveProp(writable: true)]
    public string $type;

    #[LiveProp(writable: true)]
    public bool $isWidget = true;

    public function __construct(
        private readonly DownloadRepository $downloadRepository,
    ) {}

    public function getDownloads()
    {
        if ($this->type === "active") {
            return $this->downloadRepository->getActivePaginated($this->getUser(), $this->pageNumber, $this->perPage, $this->term);
        } elseif ($this->type === "complete") {
            return $this->downloadRepository->getCompletePaginated($this->getUser(), $this->pageNumber, $this->perPage, $this->term);
        }

        return [];
    }
}
