<?php

use Core\Database\DB;

if (!function_exists('auth')) {
    function auth(): ?array
    {
        if (empty($_SESSION['user_id'])) {
            return null;
        }

        static $user = null;

        if ($user !== null) {
            return $user;
        }

        $stmt = DB::conn()->prepare("
            SELECT *
            FROM users
            WHERE id = :id
            LIMIT 1
        ");

        $stmt->execute([
            'id' => $_SESSION['user_id']
        ]);

        $user = $stmt->fetch(PDO::FETCH_ASSOC);

        return $user ?: null;
    }
}

if (!function_exists('isLoggedIn')) {
    function isLoggedIn(): bool
    {
        return auth() !== null;
    }
}