<?php

namespace App\Download\Framework\Repository;

use App\Download\Framework\Entity\Download;
use App\User\Framework\Entity\User;
use App\Util\Paginator;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Component\Security\Core\User\UserInterface;

/**
 * @extends ServiceEntityRepository<Download>
 */
class DownloadRepository extends ServiceEntityRepository
{
    private ManagerRegistry $managerRegistry;

    private Paginator $paginator;

    public function __construct(ManagerRegistry $registry, ManagerRegistry $managerRegistry, Paginator $paginator)
    {
        parent::__construct($registry, Download::class);
        $this->managerRegistry = $managerRegistry;
        $this->paginator = $paginator;
    }

    public function getCompletePaginated(UserInterface $user, int $pageNumber = 1, int $perPage = 10, string $term = ""): Paginator
    {
        $query = $this->createQueryBuilder('d')
            ->andWhere('d.status IN (:statuses)')
            ->andWhere('d.user = :user')
            ->andWhere('(d.title LIKE :term OR d.filename LIKE :term OR d.imdbId LIKE :term OR d.status LIKE :term OR d.mediaType LIKE :term)')
            ->orderBy('d.id', 'DESC')
            ->setParameter('statuses', ['Complete'])
            ->setParameter('user', $user)
            ->setParameter('term', '%' . $term . '%')
            ->getQuery();

        return $this->paginator->paginate($query, $pageNumber, $perPage);
    }

    public function getActivePaginated(UserInterface $user, int $pageNumber = 1, int $perPage = 5, string $term = ""): Paginator
    {
        $query = $this->createQueryBuilder('d')
            ->andWhere('d.status IN (:statuses)')
            ->andWhere('d.user = :user')
            ->andWhere('(d.title LIKE :term OR d.filename LIKE :term OR d.imdbId LIKE :term OR d.status LIKE :term OR d.mediaType LIKE :term)')
            ->orderBy('d.id', 'ASC')
            ->setParameter('statuses', ['New', 'In Progress', 'Paused'])
            ->setParameter('user', $user)
            ->setParameter('term', '%' . $term . '%')
            ->getQuery();

        return $this->paginator->paginate($query, $pageNumber, $perPage);
    }

    public function insert(
        UserInterface $user,
        string $url,
        string $title,
        string $filename,
        string $imdbId,
        string $mediaType,
        string $batchId,
        string $status = 'New'
    ): Download {
        /** @var User $user */
        $download = (new Download())
            ->setUser($user)
            ->setUrl($url)
            ->setTitle($title)
            ->setFilename($filename)
            ->setImdbId($imdbId)
            ->setMediaType($mediaType)
            ->setBatchId($batchId)
            ->setProgress(0)
            ->setStatus($status);

        $this->getEntityManager()->persist($download);
        $this->getEntityManager()->flush();

        return $download;
    }

    public function updateStatus(int $id, string $status): Download
    {
        $download = $this->find($id);
        $download->setStatus($status);
        $this->getEntityManager()->flush();
        return $download;
    }

    public function delete(int $id)
    {
        $entity = $this->find($id);
        $this->getEntityManager()->remove($entity);
        $this->getEntityManager()->flush();
    }

    public function latest(int $limit = 1)
    {
        return $this->createQueryBuilder('d')
            ->andWhere('d.status IN (:statuses)')
            ->setParameter('statuses', ['Complete'])
            ->setMaxResults($limit)
            ->orderBy('d.id', 'DESC')
            ->getQuery()
            ->getResult();
    }
}
