<?php
session_start();
include 'config.php'; // db connection if needed

if (isset($_POST['product_id'])) {
    $product_id   = $_POST['product_id'];
    $product_name = $_POST['product_name'];
    $product_price= $_POST['product_price'];
    $product_image= $_POST['product_image'];

    // Agar cart session empty hai to create
    if (!isset($_SESSION['cart'])) {
        $_SESSION['cart'] = [];
    }

    $found = false;
    foreach ($_SESSION['cart'] as &$item) {
        if ($item['id'] == $product_id) {
            $item['quantity'] += 1; // quantity badhao
            $found = true;
            break;
        }
    }

    if (!$found) {
        $_SESSION['cart'][] = [
            "id" => $product_id,
            "name" => $product_name,
            "price" => $product_price,
            "image" => $product_image,
            "quantity" => 1
        ];
    }
}

// Redirect back to products page
header("Location: gallery.php");
exit;
