<?php

namespace App\Controller\Admin;

use App\Repository\TicketRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class CheckinAction extends AbstractController
{
    public function __construct(
        private TicketRepository $ticketRepository,
        private EntityManagerInterface $entityManager,
    ) {
    }

    #[Route(path: '/admin/checkin', name: 'admin_checkin', methods: Request::METHOD_POST)]
    public function __invoke(Request $request): Response
    {
        $entityId = $request->query->get('entityId');

        if (!$entityId || !$ticket = $this->ticketRepository->findOneById($entityId)) {
            noty()->error('Ein Fehler ist aufgetreten.');

            return $this->redirect($request->get('referer'));
        }

        $ticket->setCheckedIn(true);
        $this->entityManager->flush();

        return $this->redirect($request->headers->get('referer'));
    }
}