<?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\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

class FlashMessageController extends AbstractController
{
    #[Route('/cms/flash-message', name: 'cms_flash_message', methods: ['GET'])]
    public function __invoke(Request $request): Response
    {
        $type = (string) $request->query->get('type', '');
        $headline = $request->query->get('headline');
        $text = $request->query->get('text');
        $href = $request->query->get('href');
        $linkText = $request->query->get('linkText');

        if (str_starts_with($type, 'modal-')) {
            return $this->render('@FerienpassCms/flash_message.stream.html.twig', [
                'type' => $type,
                'headline' => $headline,
                'text' => $text,
                'href' => $href,
                'linkText' => $linkText,
            ]);
        }

        if ('banner-info' === $type) {
            return $this->render('@FerienpassCms/flash_banner.stream.html.twig', [
                'headline' => $headline,
                'text' => $text,
                'href' => $href,
            ]);
        }

        // Map reset_password_error to the error alert; the message is a plain
        // string supplied via the `text` query param.
        $alertType = 'reset_password_error' === $type ? 'error' : $type;

        if (!\in_array($alertType, ['confirmation', 'warning', 'error'], true)) {
            return new Response('', Response::HTTP_NO_CONTENT);
        }

        return $this->render('@FerienpassCms/flash_notification.stream.html.twig', [
            'type' => $alertType,
            'headline' => $headline,
            'text' => $text,
        ]);
    }
}
