<?php
// get_cart_count.php
session_start();
require_once 'config/database.php';

if (!isset($_SESSION['isLoggedIn']) || $_SESSION['isLoggedIn'] !== true) {
    echo json_encode(['success' => false, 'cart_count' => 0]);
    exit();
}

$user_id = $_SESSION['user_id'] ?? 0;
$db = Database::getInstance()->getConnection();

try {
    $stmt = $db->prepare("SELECT SUM(quantity) as total FROM cart WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $cart_count = $stmt->fetchColumn() ?: 0;

    echo json_encode(['success' => true, 'cart_count' => $cart_count]);
} catch (PDOException $e) {
    echo json_encode(['success' => false, 'cart_count' => 0]);
}