<?php

namespace App\Helpers;

class SessionHelper
{
    public static function start(): void
    {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
    }

    public static function user(): ?array
    {
        self::start();
        return $_SESSION['user'] ?? null;
    }

    public static function setUser(array $user): void
    {
        self::start();
        $_SESSION['user'] = $user;
    }

    public static function destroy(): void
    {
        self::start();
        $_SESSION = [];
        session_destroy();
    }

    public static function flash(string $key, ?string $message = null): ?string
    {
        self::start();
        if ($message === null) {
            if (!empty($_SESSION['flash'][$key])) {
                $value = $_SESSION['flash'][$key];
                unset($_SESSION['flash'][$key]);
                return $value;
            }
            return null;
        }

        $_SESSION['flash'][$key] = $message;
        return null;
    }
}

