<?php
declare(strict_types=1);

namespace App\Game\Controller;

use App\Game\Service\GameResponseService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

#[Route('/game/api', name: 'game_api_')]
final class GameApiController extends AbstractController
{

    public function __construct(
            protected GameResponseService $gameResponseService) {

    }

    #[Route('/ping', name: 'ping', methods: ['GET'])]
    public function ping(): JsonResponse
    {
        return $this->json([
            'ok' => true,
            'service' => 'game-api',
            'ts' => date('c'),
        ]);
    }

    #[Route('/message', name: 'message', methods: ['POST'])]
    public function message(Request $request): JsonResponse
    {
        $raw = (string) $request->getContent();
        $data = null;
        if ($raw !== '') {
            $data = $this->gameResponseService->getGameResponse($raw);
        }

        return $this->json([
            'ok' => true,
            'result' => $data,
            'ts' => date('c'),
        ]);
    }
}
