<?php

namespace DMD\LaLigaApi\Service\Season\getSeasonById;

use DMD\LaLigaApi\Dto\LeagueDto;
use DMD\LaLigaApi\Dto\SeasonDto;
use DMD\LaLigaApi\Repository\SeasonRepository;
use DMD\LaLigaApi\Repository\UserRepository;
use DMD\LaLigaApi\Service\League\LeagueFactory;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\HttpException;

class HandleGetSeasonById
{
    public function __construct(
        public EntityManagerInterface $entityManager,
        public SeasonRepository $seasonRepository,
    ){}

    public function __invoke(Request $request, int $seasonId): JsonResponse
    {
        $seasonObj = $this->seasonRepository->find($seasonId);
        if (is_null($seasonObj))
        {
            throw new HttpException(
                Response::HTTP_NOT_FOUND,
                "Temporada no encontrada."
            );
        }
        $seasonDto = new SeasonDto();
        $seasonDto->fillFromEntity($seasonObj);
        return new JsonResponse(
            data: [
                'success' => true,
                'season' => $seasonDto->createSeasonArray()
            ],
            status: Response::HTTP_OK
        );
    }
}