<?php

namespace App\Twig;

use App\Controller\Admin\TicketCrudController;
use App\Enum\FoodData;
use App\Enum\TicketData;
use chillerlan\QRCode\QRCode;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class Ticket extends AbstractExtension
{
    public function __construct(private readonly UrlGeneratorInterface $urlGenerator)
    {
    }

    public function getFilters(): array
    {
        return [
            new TwigFilter('food', $this->getFoodName(...)),
            new TwigFilter('ticket', $this->getTicket(...)),
            new TwigFilter('qr', $this->generateQr(...)),
            new TwigFilter('url', $this->url(...)),
        ];
    }

    public function getFoodName(int $id): string
    {
        return FoodData::FOOD_DATA[$id] ?? 'N/A';
    }

    public function getTicket(int $id): array
    {
        return TicketData::TICKET_DATA[$id] ?? ['name' => 'N/A', 'price' => 'N/A'];
    }

    public function generateQr(\App\Entity\Ticket $ticket): string
    {
        return (new QRCode)->render($this->url($ticket));
    }

    public function url(\App\Entity\Ticket $ticket): string
    {
        return $this->urlGenerator->generate('admin', [
            'crudAction' => 'detail',
            'crudControllerFqcn' => TicketCrudController::class,
            'entityId' => $ticket->getCustomer()?->getId()
        ], UrlGeneratorInterface::ABSOLUTE_URL);
    }
}