<?php

declare(strict_types=1);

namespace App\Controller;

use App\Entity\GameMode;
use App\Enum\ThrowRule;
use App\Repository\GameModeRepository;
use App\Service\GameModeBootstrapper;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

#[Route('/api')]
class ApiGameModeController extends AbstractController
{
    #[Route('/modes', name: 'api_modes_list', methods: ['GET'])]
    public function list(
        GameModeBootstrapper $bootstrapper,
        GameModeRepository $gameModeRepository,
    ): JsonResponse {
        $bootstrapper->ensureDefaults();

        $modes = $gameModeRepository->findAllSortedByStartScore();

        return $this->json([
            'modes' => array_map(static fn (GameMode $mode): array => [
                'id' => $mode->getId(),
                'key' => $mode->getKey(),
                'name' => $mode->getName(),
                'startScore' => $mode->getStartScore(),
                'type' => $mode->getType()->value,
            ], $modes),
            'throwRules' => array_map(
                static fn (ThrowRule $rule): string => $rule->value,
                ThrowRule::cases(),
            ),
        ]);
    }
}
