<?php 

namespace App\Service;

use App\Entity\SpecialistView;
use App\Repository\SpecialistViewRepository;
use App\Repository\LocationViewRepository;
Use App\Repository\ReviewRepository;
use App\Service\SpecialistMoreService;
use Knp\Component\Pager\PaginatorInterface;
use Knp\Bundle\PaginatorBundle\Pagination\SlidingPagination;
use App\Repository\PriceListRepository;

class SpecialistService
{
	public function __construct(
		private LocationViewRepository $locationViewRepository,
		private PriceListRepository $priceListRepository,
		private SpecialistViewRepository $specialistViewRepository,
		private ReviewRepository $reviewRepository,
		private PaginatorInterface $paginator
	) {	}

	public function listPaginated(
		array $filters = [], 
		int $page = 1, 
		int $limit = 10
	): SlidingPagination {
		$query = $this->specialistViewRepository
			->createFilteredQueryBuilder($filters)
			->getQuery()
		;
		
        $paginatedSpecialists = $this->paginator->paginate($query, $page, $limit);

	    foreach ($paginatedSpecialists as $key => $specialist) {
	    	$specialist->addSpecialistMoreService(
	    		new SpecialistMoreService(
	    			$this->locationViewRepository,
	    			$this->reviewRepository,
	    			$this->priceListRepository,
	    			$filters['onlineMode'] ?? false
	    		)
	    	);
	    }

	    return $paginatedSpecialists;
	}

	public function list(array $filters = []): array 
	{	
		$specialists = $this->specialistViewRepository
			->createFilteredQueryBuilder($filters)
			->getQuery()
			->getResult()
		;
		
	    foreach ($specialists as $key => $specialist) {
	    	$specialist->addSpecialistMoreService(
	    		new SpecialistMoreService(
	    			$this->locationViewRepository,
	    			$this->reviewRepository,
	    			$this->priceListRepository,
	    			$filters['onlineMode'] ?? false
	    		)
	    	);
	    }

	    return $specialists;
	}

	public function show(array $filters = []): ?SpecialistView 
	{
		$specialist = $this->specialistViewRepository
			->createFilteredQueryBuilder($filters)
			->setMaxResults(1)
			->getQuery()
			->getOneOrNullResult();	
		;

		if ($specialist) {
			$specialist->addSpecialistMoreService(
	    		new SpecialistMoreService(
	    			$this->locationViewRepository,
	    			$this->reviewRepository,
	    			$this->priceListRepository,
	    			$filters['onlineMode'] ?? false
	    		)
	    	);
		}

		return $specialist;	
	}
}