<?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\CmsBundle\Twig\Extension;

use Ferienpass\CmsBundle\Security\LoginRedirectResolver;
use Twig\Attribute\AsTwigFunction;

class LoginRedirectExtension
{
    public function __construct(private readonly LoginRedirectResolver $resolver)
    {
    }

    /**
     * Resolve a trustworthy post-login redirect target for the sign-in templates.
     *
     * Pass the includer's explicit target (e.g. `redirect|default(null)`); the
     * resolver falls back to a signed `?redirect=` or the current page. Never
     * emit a raw `app.request.query.get('redirect')` into a passkey
     * success-redirect attribute — see {@see LoginRedirectResolver}.
     */
    #[AsTwigFunction('cms_login_redirect')]
    public function loginRedirect(?string $explicit = null): string
    {
        return $this->resolver->resolve($explicit);
    }
}
