<?php
require_once __DIR__ . '/../config/db.php';

function current_user() {
    if (session_status() === PHP_SESSION_NONE) session_start();
    if (empty($_SESSION['user_id'])) return null;
    $id = (int)$_SESSION['user_id'];
    $res = db()->query("SELECT id, name, email, avatar_url FROM users WHERE id = $id");
    return $res ? $res->fetch_assoc() : null;
}

function require_login($next = null) {
    if (session_status() === PHP_SESSION_NONE) session_start();
    if (empty($_SESSION['user_id'])) {
        $loc = '/WATCHING/pages/login.php';
        if ($next) $loc .= '?next=' . urlencode($next);
        header('Location: ' . $loc);
        exit;
    }
}