<?php

namespace AutoAdmin\Bundle\Admin\Helper;

use AutoAdmin\Bundle\Admin\Exception\UnknownToastLevelException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;

class FlashbagHelper
{
    const AUTHORIZED_LEVELS = [
        'error',
        'info',
        'success',
        'warning',
    ];

    public static function addSuccess(Request $request, string $message): void
    {
        self::addFlash($request, 'success', $message);
    }

    public static function addFlash(Request $request, string $level, string $message): void
    {
        /** @var Session $session */
        $session = $request->getSession();

        if (!in_array($level, self::AUTHORIZED_LEVELS)) {
            throw new UnknownToastLevelException($level);
        }

        $session->getFlashBag()->add($level, $message);
    }

    public static function addInfo(Request $request, string $message): void
    {
        self::addFlash($request, 'info', $message);
    }

    public static function addWarning(Request $request, string $message): void
    {
        self::addFlash($request, 'warning', $message);
    }

    public static function addError(Request $request, string $message): void
    {
        self::addFlash($request, 'error', $message);
    }
}
