<?php

declare(strict_types=1);

/*
 * This file is part of the Ferienpass package.
 *
 * (c) Richard Henkenjohann <richard@ferienpass.online>
 *
 * For more information visit the project website <https://ferienpass.online>
 * or the documentation under <https://docs.ferienpass.online>.
 */

namespace Ferienpass\AdminBundle\Controller\Fragment;

use Ferienpass\CoreBundle\Entity\User;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

/**
 * ESI fragment that renders the header notification bell.
 *
 * The surrounding admin page is cached for a year; rendering the bell as an ESI
 * fragment lets it stay per-user and always current. Unlike the user dropdown
 * (which is stable and tag-invalidated), the unread badge changes on every new
 * notification — including ones created in a worker (inbound email replies)
 * where host-based cache purging is awkward — so the fragment is simply not
 * stored: Varnish fetches it fresh on every page view.
 *
 * Not a mapped route: dispatched at request time by Symfony's FragmentListener
 * via render_esi() in _header.html.twig.
 */
final class NotificationBellController extends AbstractController
{
    public function __invoke(): Response
    {
        if (!$this->isGranted('ROLE_ADMIN') || !$this->getUser() instanceof User) {
            return new Response('');
        }

        $response = $this->render('@FerienpassAdmin/layout/_notification_bell.html.twig');

        // Per-user and ever-changing: never cache the badge.
        $response->setPrivate();
        $response->headers->addCacheControlDirective('no-store');
        $response->setVary(['Cookie']);

        return $response;
    }
}
