<?php

namespace App\Controller;

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;

class SuccessController extends AbstractController
{
    #[Route(path: '/success', name: 'app_order_success', methods: Request::METHOD_GET)]
    public function success(Request $request, TicketService $service): Response
    {
        $sessionId = $request->query->get('session_id');

        if (!$sessionId) {
            noty()->error('Etwas ist schiefgelaufen');

            return $this->redirectToRoute('app_ticket');
        }

        if (!$service->completePayment($sessionId)) {
            noty()->error('Etwas ist schiefgelaufen');

            return $this->redirectToRoute('app_ticket');
        }

        return $this->render('ticket/success.html.twig');
    }

    #[Route(path: '/cancelled', name: 'app_cancelled', methods: Request::METHOD_GET)]
    public function cancel(): Response
    {
        noty()->error('Bezahlung abgebrochen');

        return $this->redirectToRoute('app_ticket');
    }

}