<?php
// backend/utils/Auth.php
class Auth {
    public static function startSession() {
        if (session_status() === PHP_SESSION_NONE) {
            session_start();
        }
    }

    public static function isLoggedIn() {
        self::startSession();
        return isset($_SESSION['user_id']);
    }

    public static function getUserId() {
        self::startSession();
        return $_SESSION['user_id'] ?? null;
    }

    public static function requireLogin() {
        self::startSession();
        if (!self::isLoggedIn()) {
            http_response_code(401);
            echo json_encode(['error' => 'Unauthorized']);
            exit;
        }
    }

    public static function login($userId) {
        self::startSession();
        $_SESSION['user_id'] = $userId;
    }

    public static function logout() {
        self::startSession();
        session_destroy();
    }
}
?>