<?php
/**
 * SalonPro X - Session Management Class
 */

namespace core;

class Session
{
    public static function start(): void
    {
        if (session_status() === PHP_SESSION_NONE) {
            ini_set('session.name', SESSION_NAME);
            ini_set('session.use_strict_mode', 1);
            ini_set('session.use_only_cookies', 1);
            ini_set('session.cookie_httponly', 1);
            ini_set('session.cookie_secure', 0);
            ini_set('session.cookie_samesite', 'Lax');
            ini_set('session.gc_maxlifetime', SESSION_LIFETIME);
            ini_set('session.cookie_lifetime', 0);
            
            session_start();
            
            if (!isset($_SESSION['_initialized'])) {
                self::regenerate();
                $_SESSION['_initialized'] = true;
                $_SESSION['_created_at'] = time();
            }
        }
    }

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

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

    public static function has(string $key): bool
    {
        return isset($_SESSION[$key]);
    }

    public static function remove(string $key): void
    {
        unset($_SESSION[$key]);
    }

    public static function destroy(): 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();
    }

    public static function regenerate(): void
    {
        session_regenerate_id(true);
    }

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

    public static function hasFlash(string $key): bool
    {
        return isset($_SESSION['_flash'][$key]);
    }

    public static function setAlert(string $type, string $message): void
    {
        self::flash('alert_' . uniqid(), ['type' => $type, 'message' => $message]);
    }

    public static function getAlerts(): array
    {
        $alerts = [];
        if (isset($_SESSION['_flash'])) {
            foreach ($_SESSION['_flash'] as $key => $value) {
                if (str_starts_with($key, 'alert_')) {
                    $alerts[] = $value;
                    unset($_SESSION['_flash'][$key]);
                }
            }
        }
        return $alerts;
    }

    public static function isExpired(): bool
    {
        $lastActivity = $_SESSION['_last_activity'] ?? 0;
        return (time() - $lastActivity) > SESSION_LIFETIME;
    }

    public static function updateActivity(): void
    {
        $_SESSION['_last_activity'] = time();
    }
}
