# Frontend Response Structure Fix

## Problem

The frontend was showing "Network error: Failed to apply promotions" even though the API was returning a successful response. This was due to a mismatch between the expected response structure and the actual API response structure.

## Root Cause

The API returns:

```json
{
  "success": true,
  "data": {
    "success": true,
    "preview_cart": {
      "subtotal": 8100,
      "tax": 0,
      "total": 8100
    },
    "applied_promotions": [...],
    "total_discount": 450
  }
}
```

But the frontend was looking for:

```json
{
    "success": true,
    "data": {
        "success": true,
        "cart": {
            // ← Wrong property name!
            "subtotal": 8100,
            "tax": 0,
            "total": 8100
        }
    }
}
```

## Solution

Updated the frontend code to handle both response structures:

### Files Modified:

1. **`resources/js/stores/cartStore.ts`**

    - Updated to look for `result.data.preview_cart` first, then fallback to `result.data.cart`
    - Added optional chaining to prevent errors

2. **`resources/js/hooks/usePosPromotions.ts`**
    - Updated `PromotionPreviewResponse` interface to include `preview_cart` property
    - Added logic to handle both `preview_cart` and `cart` properties
    - Added cart update logic to sync with preview cart data

### Key Changes:

```typescript
// Before
subtotal: result.data.cart.subtotal || 0,

// After
subtotal: result.data.preview_cart?.subtotal || result.data.cart?.subtotal || 0,
```

## Testing

✅ API returns correct structure with `preview_cart`  
✅ Frontend now handles both `preview_cart` and `cart` properties  
✅ Promotion system working correctly (450 EGP discount applied)  
✅ No more "Network error: Failed to apply promotions"

## Result

The promotion system is now fully functional with proper error handling and response structure compatibility.
