<?php
session_start();
require_once 'db/config.php';
require_once 'header.php';

// Fetch cart items
$cart_items = [];
$total_quantity = 0;
$subtotal = 0;
$total_bill = 0;

$user_id = $_SESSION['user_id'] ?? null;
$session_id = session_id();

if ($user_id) {
    $stmt = db()->prepare("
        SELECT c.menu_item_id, c.quantity, mi.name, mi.price, mi.image_url, r.name as restaurant_name, r.id as restaurant_id
        FROM cart c
        JOIN menu_items mi ON c.menu_item_id = mi.id
        JOIN restaurants r ON c.restaurant_id = r.id
        WHERE c.user_id = ?
    ");
    $stmt->execute([$user_id]);
    $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
} else {
    $stmt = db()->prepare("
        SELECT c.menu_item_id, c.quantity, mi.name, mi.price, mi.image_url, r.name as restaurant_name, r.id as restaurant_id
        FROM cart c
        JOIN menu_items mi ON c.menu_item_id = mi.id
        JOIN restaurants r ON c.restaurant_id = r.id
        WHERE c.session_id = ?
    ");
    $stmt->execute([$session_id]);
    $cart_items = $stmt->fetchAll(PDO::FETCH_ASSOC);
}

if ($cart_items) {
    foreach ($cart_items as $item) {
        $total_quantity += $item['quantity'];
        $subtotal += $item['price'] * $item['quantity'];
    }
    $total_bill = $subtotal; // For now, total is same as subtotal. Can add taxes/fees later.
}
?>

<div class="container my-5">
    <h1 class="display-5 fw-bold mb-4">Your Shopping Cart</h1>

    <?php if (empty($cart_items)): ?>
        <div class="text-center py-5">
            <i class="fas fa-shopping-cart fa-4x text-muted mb-3"></i>
            <h3 class="text-muted">Your cart is empty.</h3>
            <p class="text-muted">Looks like you haven't added anything to your cart yet.</p>
            <a href="restaurants.php" class="btn btn-primary mt-3">Continue Shopping</a>
        </div>
    <?php else: ?>
        <div class="row">
            <div class="col-lg-8">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <?php foreach ($cart_items as $item): ?>
                            <div class="row align-items-center mb-4">
                                <div class="col-md-2">
                                    <img src="<?php echo htmlspecialchars($item['image_url'] ?: 'https://via.placeholder.com/150'); ?>" class="img-fluid rounded" alt="<?php echo htmlspecialchars($item['name']); ?>">
                                </div>
                                <div class="col-md-4">
                                    <h5 class="mb-0"><?php echo htmlspecialchars($item['name']); ?></h5>
                                    <small class="text-muted">From: <a href="menu.php?restaurant_id=<?php echo $item['restaurant_id']; ?>"><?php echo htmlspecialchars($item['restaurant_name']); ?></a></small>
                                </div>
                                <div class="col-md-3">
                                    <form action="cart_actions.php" method="post" class="d-flex align-items-center">
                                        <input type="hidden" name="action" value="update">
                                        <input type="hidden" name="menu_item_id" value="<?php echo $item['menu_item_id']; ?>">
                                        <button type="submit" name="quantity" value="<?php echo $item['quantity'] - 1; ?>" class="btn btn-outline-secondary btn-sm" <?php echo $item['quantity'] <= 1 ? 'disabled' : ''; ?>>-</button>
                                        <input type="text" class="form-control form-control-sm text-center mx-2" value="<?php echo $item['quantity']; ?>" readonly style="width: 50px;">
                                        <button type="submit" name="quantity" value="<?php echo $item['quantity'] + 1; ?>" class="btn btn-outline-secondary btn-sm">+</button>
                                    </form>
                                </div>
                                <div class="col-md-2 text-end">
                                    <span class="fw-bold">$<?php echo number_format($item['price'] * $item['quantity'], 2); ?></span>
                                </div>
                                <div class="col-md-1 text-end">
                                    <form action="cart_actions.php" method="post">
                                        <input type="hidden" name="action" value="remove">
                                        <input type="hidden" name="menu_item_id" value="<?php echo $item['menu_item_id']; ?>">
                                        <button type="submit" class="btn btn-link text-danger p-0"><i class="fas fa-trash"></i></button>
                                    </form>
                                </div>
                            </div>
                            <?php if (next($cart_items)): ?>
                                <hr>
                            <?php endif; ?>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
            <div class="col-lg-4">
                <div class="card shadow-sm">
                    <div class="card-body">
                        <h4 class="card-title mb-4">Order Summary</h4>
                        <div class="d-flex justify-content-between mb-2">
                            <span>Total Items:</span>
                            <span><?php echo $total_quantity; ?></span>
                        </div>
                        <div class="d-flex justify-content-between mb-3">
                            <span>Subtotal:</span>
                            <span>$<?php echo number_format($subtotal, 2); ?></span>
                        </div>
                        <hr>
                        <div class="d-flex justify-content-between fw-bold fs-5">
                            <span>Total Bill:</span>
                            <span>$<?php echo number_format($total_bill, 2); ?></span>
                        </div>
                        <div class="d-grid gap-2 mt-4">
                            <a href="checkout.php" class="btn btn-primary btn-lg">Proceed to Checkout</a>
                            <a href="restaurants.php" class="btn btn-outline-secondary">Continue Shopping</a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <?php endif; ?>
</div>

<?php require_once 'footer.php'; ?>
