<?php

declare(strict_types=1);

/*
 * Chill is a software for social workers
 *
 * For the full copyright and license information, please view
 * the LICENSE file that was distributed with this source code.
 */

namespace Chill\TicketBundle\Controller;

use Chill\PersonBundle\Security\Authorization\PersonVoter;
use Chill\PersonBundle\Entity\Person;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;

final readonly class TicketListController
{
    public function __construct(
        private Security $security,
        private Environment $twig,
    ) {}

    #[Route('/{_locale}/ticket/ticket/list', name: 'chill_ticket_ticket_list')]
    public function __invoke(Request $request): Response
    {
        if (!$this->security->isGranted('ROLE_USER')) {
            throw new AccessDeniedHttpException('only user can access this page');
        }

        return new Response(
            $this->twig->render('@ChillTicket/Ticket/list.html.twig')
        );
    }

    #[Route('/{_locale}/ticket/by-person/{id}/list', name: 'chill_person_ticket_list')]
    public function listByPerson(Request $request, Person $person): Response
    {
        if (!$this->security->isGranted(PersonVoter::SEE, $person)) {
            throw new AccessDeniedHttpException('you are not allowed to see this person');
        }

        return new Response(
            $this->twig->render('@ChillTicket/Person/list.html.twig', [
                'person' => $person,
            ])
        );
    }
}
