<?php

// Mulai session jika belum berjalan
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

/**
 * Set session value
 */
function setSession(string $key, $value): void {
    $_SESSION[$key] = $value;
}

/**
 * Get session value
 */
function getSession(string $key) {
    return $_SESSION[$key] ?? null;
}

/**
 * Set flash message (sekali pakai)
 */
function setFlash(string $key, string $message): void {
    $_SESSION['_flash'][$key] = $message;
}

/**
 * Get flash message lalu hapus
 */
function getFlash(string $key): ?string {
    if (!isset($_SESSION['_flash'][$key])) return null;

    $msg = $_SESSION['_flash'][$key];
    unset($_SESSION['_flash'][$key]);
    return $msg;
}

/**
 * Destroy whole session (logout)
 */
function destroySession(): void {
    $_SESSION = [];

    if (ini_get("session.use_cookies")) {
        $params = session_get_cookie_params();
        setcookie(session_name(), '', time() - 42000,
            $params["path"], $params["domain"],
            $params["secure"], $params["httponly"]
        );
    }

    session_destroy();
}
