<?php
/**
 * GET /api/auth/me.php
 * Returns the currently authenticated user, or 401 if not logged in.
 * The front-end calls this on page load to know whether to show
 * login/register links or the logged-in nav.
 */

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../includes/response.php';
require_once __DIR__ . '/../includes/auth.php';

$userId = require_login();

$pdo = Database::connect();
$stmt = $pdo->prepare('SELECT id, email, account_type, created_at FROM users WHERE id = ?');
$stmt->execute([$userId]);
$user = $stmt->fetch();

if (!$user) {
    logout_user();
    send_error('Session invalid.', 401);
}

send_json(true, 'OK', ['user' => $user]);
