<?php
/**
 * 2026 Paysera
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@prestashop.com so we can send you a copy immediately.
 *
 * @author Paysera <plugins@paysera.com>
 * @copyright 2026 Paysera
 * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0)
 * International Registered Trademark & Property of Paysera
 */

declare(strict_types=1);

namespace Paysera\PrestashopPayments\Service;

if (!defined('_PS_VERSION_')) {
    exit;
}

/**
 * Handles flash messages storage and retrieval via cookies.
 * Supports namespaced messages for different pages/contexts.
 */
class FlashMessageHandler
{
    private const NAMESPACE_AUTHORIZATION = 'authorization';
    private const NAMESPACE_PAYMENT_SETTINGS = 'payment_settings';
    private const COOKIE_PREFIX = 'paysera_';
    private const COOKIE_SUCCESS_SUFFIX = '_success';
    private const COOKIE_ERRORS_SUFFIX = '_errors';

    private object $cookie;
    private string $namespace;

    public function __construct(object $cookie, string $namespace)
    {
        $this->cookie = $cookie;
        $this->namespace = $namespace;
    }

    public static function forAuthorization(object $cookie): self
    {
        return new self($cookie, self::NAMESPACE_AUTHORIZATION);
    }

    public static function forPaymentSettings(object $cookie): self
    {
        return new self($cookie, self::NAMESPACE_PAYMENT_SETTINGS);
    }

    public function addSuccess(string $message): void
    {
        $key = $this->getSuccessKey();
        $this->cookie->$key = $message;
        $this->cookie->write();
    }

    /**
     * @param list<string> $errors
     */
    public function addErrors(array $errors): void
    {
        if (count($errors) === 0) {
            return;
        }

        $key = $this->getErrorsKey();
        $this->cookie->$key = json_encode($errors);
        $this->cookie->write();
    }

    public function getSuccess(): ?string
    {
        $key = $this->getSuccessKey();

        if (!isset($this->cookie->$key)) {
            return null;
        }

        $message = $this->cookie->$key;
        unset($this->cookie->$key);
        $this->cookie->write();

        return $message;
    }

    /**
     * @return list<string>
     */
    public function getErrors(): array
    {
        $key = $this->getErrorsKey();

        if (!isset($this->cookie->$key)) {
            return [];
        }

        $errorsJson = $this->cookie->$key;
        unset($this->cookie->$key);
        $this->cookie->write();

        $errors = json_decode($errorsJson, true);

        if (!is_array($errors)) {
            return [];
        }

        return array_values($errors);
    }

    public function hasSuccess(): bool
    {
        $key = $this->getSuccessKey();

        return isset($this->cookie->$key);
    }

    public function hasErrors(): bool
    {
        $key = $this->getErrorsKey();

        return isset($this->cookie->$key);
    }

    private function getSuccessKey(): string
    {
        return self::COOKIE_PREFIX . $this->namespace . self::COOKIE_SUCCESS_SUFFIX;
    }

    private function getErrorsKey(): string
    {
        return self::COOKIE_PREFIX . $this->namespace . self::COOKIE_ERRORS_SUFFIX;
    }
}
