<?php
/**
 * Cart Actions Handler - FIXED VERSION
 * Digital Product Store
 */

require_once __DIR__ . '/config/database.php';

$action = $_GET['action'] ?? $_POST['action'] ?? '';

switch ($action) {
    case 'add':
        $product_id = (int)($_GET['id'] ?? $_POST['product_id'] ?? 0);
        $quantity = (int)($_GET['quantity'] ?? $_POST['quantity'] ?? 1);
        
        if ($product_id > 0) {
            if (add_to_cart($product_id, $quantity)) {
                set_flash_message('success', 'Product added to cart!');
            } else {
                set_flash_message('error', 'Failed to add product to cart.');
            }
        }
        
        // Check if AJAX request
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
            header('Content-Type: application/json');
            echo json_encode([
                'success' => true,
                'cart_count' => get_cart_count(),
                'message' => 'Product added to cart!'
            ]);
            exit;
        }
        
        header('Location: ' . ($_SERVER['HTTP_REFERER'] ?? BASE_URL . '/products.php'));
        exit;
        
    case 'remove':
        $cart_id = (int)($_GET['id'] ?? $_POST['cart_id'] ?? 0);
        
        if ($cart_id > 0) {
            remove_from_cart($cart_id);
            set_flash_message('success', 'Item removed from cart.');
        }
        
        header('Location: cart.php');
        exit;
        
    case 'update':
        $cart_id = (int)($_POST['cart_id'] ?? 0);
        $quantity = (int)($_POST['quantity'] ?? 1);
        
        if ($cart_id > 0) {
            update_cart_quantity($cart_id, $quantity);
        }
        
        header('Location: cart.php');
        exit;
        
    case 'apply_coupon':
        $code = sanitize($_POST['code'] ?? '');
        $cart_total = get_cart_total();
        
        $validation = validate_coupon($code, $cart_total);
        
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest') {
            header('Content-Type: application/json');
            if ($validation['valid']) {
                $_SESSION['applied_coupon'] = $validation['coupon'];
                echo json_encode(['success' => true, 'message' => 'Coupon applied successfully!']);
            } else {
                echo json_encode(['success' => false, 'message' => $validation['message']]);
            }
            exit;
        }
        
        if ($validation['valid']) {
            $_SESSION['applied_coupon'] = $validation['coupon'];
            set_flash_message('success', 'Coupon applied successfully!');
        } else {
            set_flash_message('error', $validation['message']);
        }
        
        header('Location: cart.php');
        exit;
        
    case 'clear':
        clear_cart();
        unset($_SESSION['applied_coupon']);
        set_flash_message('success', 'Cart cleared.');
        header('Location: cart.php');
        exit;
        
    default:
        header('Location: cart.php');
        exit;
}
