<?php

namespace DMD\LaLigaApi\Service\League\getAllLeagues;

use DMD\LaLigaApi\Dto\LeagueDto;
use DMD\LaLigaApi\Repository\LeagueRepository;
use DMD\LaLigaApi\Service\League\getLeagueById\HandleGetLeagueById;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

class HandleGetAllLeagues
{
    private static int $PAGE_SIZE = 100;

    public function __construct(
        public LeagueRepository $leagueRepository
    ){}

    public function __invoke(): JsonResponse
    {
        $page = 1;
        $leagueCollection = $this->leagueRepository->findBy([
            'active' => true
        ],
            limit: self::$PAGE_SIZE,
            offset: ($page * self::$PAGE_SIZE) - self::$PAGE_SIZE
        );
        $leagueArray = [];
        if (!is_null($leagueCollection))
        {
            foreach ($leagueCollection as $leagueObj)
            {
                $leagueDto = new LeagueDto();
                $leagueDto->fillFromObject($leagueObj);
                $leagueArray[] = $leagueDto->getAllLeaguesArray();
            }
        }
        return new JsonResponse(
            data: [
                'success' => true,
                'leagues' => $leagueArray
            ],
            status: Response::HTTP_OK
        );
    }
}