<?php
// CoreFlux/core/SessionManager.php

namespace Core;

class Session
{
    private static function ensureStarted()
    {
        // Skip session start if running from CLI
        if (php_sapi_name() === 'cli') {
            return;
        }

        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
    }

    public static function start()
    {
        self::ensureStarted();
    }

    public static function set(string $key, $value)
    {
        self::ensureStarted();
        $_SESSION[$key] = $value;
    }

    public static function get(string $key, $default = null)
    {
        self::ensureStarted();
        return $_SESSION[$key] ?? $default;
    }

    public static function remove(string $key)
    {
        self::ensureStarted();
        if (isset($_SESSION[$key])) {
            unset($_SESSION[$key]);
        }
    }

    public static function destroy()
    {
        self::ensureStarted();

        if (session_status() === PHP_SESSION_ACTIVE) {
            $_SESSION = []; 
            session_destroy();
        }
    }
}
