<?php
header('Content-Type: application/json');
require_once __DIR__ . '/auth_helper.php';

if (is_logged_in()) {
    try {
        $pdo = db();
        $stmt = $pdo->prepare("SELECT id, email, shopping_list FROM users WHERE id = ?");
        $stmt->execute([get_logged_in_user_id()]);
        $user = $stmt->fetch();
        if ($user) {
            if ($user['shopping_list']) {
                $user['shopping_list'] = json_decode($user['shopping_list'], true);
            }
            echo json_encode(['success' => true, 'logged_in' => true, 'user' => $user]);
        } else {
            logout_user();
            echo json_encode(['success' => true, 'logged_in' => false]);
        }
    } catch (PDOException $e) {
        echo json_encode(['success' => false, 'error' => $e->getMessage()]);
    }
} else {
    echo json_encode(['success' => true, 'logged_in' => false]);
}
