# Feasibility Check ID Fix

## Issue
When adding products to cart and proceeding to checkout, the order creation was failing with error:
```json
{"errors": {"feasibility_check_id": ["The feasibility check id field is required."]}}
```

This happened even though the user had completed a feasibility check.

## Root Cause
When adding products to cart from the products list page and products detail page (for fixed pricing products), the `feasibility_check_id` was not being included in the cart item data.

## What Was Fixed

### 1. Products List Page (`afinet-portal/src/app/(portal)/products/page.js`)

**Before:**
```javascript
addToCart({
  id: product.id,
  name: product.name,
  description: product.description,
  monthly_price: parseFloat(product.monthly_price),
  installation_fee: parseFloat(product.installation_fee || 0),
  router_purchase_price: parseFloat(product.router_purchase_price || 0),
  extension_cost: 0,
  routerOption: 'purchase',
  quantity: 1,
});
```

**After:**
```javascript
addToCart({
  id: product.id,
  name: product.name,
  description: product.description,
  monthly_price: parseFloat(product.monthly_price),
  installation_fee: parseFloat(product.installation_fee || 0),
  router_purchase_price: parseFloat(product.router_purchase_price || 0),
  extension_cost: 0,
  routerOption: 'purchase',
  quantity: 1,
  feasibility_check_id: checkId, // ✅ Added
  feasibility_status: feasibilityStatus, // ✅ Added
  order_type: 'direct', // ✅ Added
  valueAddedServices: [], // ✅ Added
});
```

### 2. Products Detail Page (`afinet-portal/src/app/(portal)/products/[id]/page.js`)

Fixed the second `addToCart` call (for fixed pricing products) to include `feasibility_check_id`:

**Before:**
```javascript
// For services with fixed pricing (like Starlink), allow direct ordering
if (product.has_fixed_pricing && product.monthly_price !== null) {
  addToCart({
    // ... other fields
    feasibility_status: feasibilityStatus,
    order_type: 'direct',
    // ❌ Missing feasibility_check_id
  });
}
```

**After:**
```javascript
// For services with fixed pricing (like Starlink), allow direct ordering
if (product.has_fixed_pricing && product.monthly_price !== null) {
  addToCart({
    // ... other fields
    feasibility_check_id: feasibilityCheckId, // ✅ Added
    feasibility_status: feasibilityStatus,
    order_type: 'direct',
  });
}
```

## How Feasibility Check ID Works

### Storage Flow:
1. User completes coverage check at `/coverage-check`
2. Feasibility check is saved to database with an `id` (database primary key)
3. Check data is stored in localStorage as `last_feasibility_check`:
   ```javascript
   {
     id: 123, // Database ID
     check_id: "FC-2024-001", // Human-readable ID
     feasibility_status: "high",
     customer_id: 456,
     address: "...",
     created_at: "2024-02-26T..."
   }
   ```

### Retrieval Flow:
1. Products page loads
2. Checks localStorage for `last_feasibility_check`
3. Validates check is:
   - Less than 30 days old
   - Belongs to current customer
   - Has valid database ID
4. Sets `checkId` state variable with the database ID
5. Falls back to API history if localStorage is invalid/missing

### Cart Flow:
1. User clicks "Order Now" on a product
2. Product is added to cart with `feasibility_check_id: checkId`
3. Cart stores this data
4. Checkout reads `items[0]?.feasibility_check_id`
5. Order is created with the feasibility check ID

## Testing Instructions

1. **Clear cart and localStorage**:
   ```javascript
   localStorage.removeItem('cart-storage')
   localStorage.removeItem('last_feasibility_check')
   ```

2. **Complete a coverage check**:
   - Go to `/coverage-check`
   - Enter your address
   - Submit the check
   - Note the feasibility status (should be "high" for most areas)

3. **Verify localStorage**:
   ```javascript
   JSON.parse(localStorage.getItem('last_feasibility_check'))
   // Should show: { id: 123, feasibility_status: "high", ... }
   ```

4. **Add product to cart**:
   - Go to `/products`
   - Click "Order Now" on Starlink (or any fixed-pricing product)

5. **Verify cart data**:
   ```javascript
   const cart = JSON.parse(localStorage.getItem('cart-storage'))
   console.log(cart.state.items[0].feasibility_check_id)
   // Should show a number (e.g., 123), not null or undefined
   ```

6. **Proceed to checkout**:
   - Fill in service address
   - Select installation date
   - Click "Proceed to Payment"
   - Should NOT get "feasibility_check_id required" error

## Expected Behavior After Fix

### Success Case:
- User completes coverage check ✅
- Feasibility check ID is stored ✅
- Product is added to cart with feasibility_check_id ✅
- Order is created successfully ✅
- User proceeds to payment ✅

### Error Cases (Expected):
- User never did coverage check → Redirected to `/coverage-check`
- Coverage check expired (>30 days) → Redirected to `/coverage-check`
- Coverage check belongs to different customer → Redirected to `/coverage-check`

## Files Modified
- ✅ `afinet-portal/src/app/(portal)/products/page.js` - Added feasibility_check_id to addToCart
- ✅ `afinet-portal/src/app/(portal)/products/[id]/page.js` - Added feasibility_check_id to fixed pricing addToCart

## Files Already Correct
- ✅ `afinet-portal/src/app/(portal)/products/[id]/page.js` - First addToCart (high feasibility) already had it
- ✅ `afinet-portal/src/app/(portal)/checkout/page.js` - Already reads feasibility_check_id from cart items

## Status
✅ **COMPLETE** - Feasibility check ID now properly included when adding products to cart
✅ **Order Creation Fixed** - Orders can now be created without "feasibility_check_id required" error
⚠️ **Action Required** - Clear cart and re-add items to test the fix
