<?php

namespace App\Controller;

use App\DataObjects\TicketFormData;
use App\Service\EventService;
use App\Service\TicketService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

final class TicketController extends AbstractController
{
    public function __construct(
        private TicketService $service,
        private SerializerInterface $serializer,
        private EventService $eventService,
    ) {
    }

    #[Route('/ticket', name: 'app_ticket')]
    public function index(): Response
    {
        return $this->render('ticket/index.html.twig');
    }

    #[Route(path: '/ticket/submit', name: 'app_submit', methods: Request::METHOD_POST)]
    public function submit(Request $request): Response
    {
        $ticketData = $this->serializer->deserialize($request->getContent(), TicketFormData::class, 'json');

        return $this->json(['id' => $this->service->handleTicketData($ticketData)->id]);
    }

    #[Route(path: '/calendar', name: 'app_calendar')]
    public function calendar(Request $request): Response
    {
        $userAgent = $request->headers->get('User-Agent');
        $isApple = str_contains($userAgent, 'Mac') || str_contains($userAgent, 'iPhone') || str_contains($userAgent, 'iPad');

        return $isApple
            ? $this->eventService->generateIcs()
            : $this->eventService->generateGoogleRedirect();
    }
}
