# Toggle Recommended Product Endpoint

## Overview
Added `toggleRecommended` endpoint to both Vendor and Admin product controllers to toggle the `is_recommended` field on products.

---

## Endpoints

### 1. Vendor Endpoint
**PATCH** `/api/dashboard/vendor/products/{product}/toggle-recommended`

**Route Name:** `products.toggle-recommended`

**Middleware:** `auth:api`, `vendor`, `subscription`

### 2. Admin Endpoint
**PATCH** `/api/dashboard/admin/products/{product}/toggle-recommended`

**Route Name:** `products.toggle-recommended`

**Middleware:** `auth:api`, `admin`

---

## Request

### Headers
```
Authorization: Bearer {token}
Content-Type: application/json
```

### URL Parameters
| Parameter | Type | Description |
|-----------|------|-------------|
| `product` | integer | Product ID |

### Body
No request body required.

---

## Response

### Success Response (200 OK)
```json
{
  "status": "success",
  "message": "Record updated successfully",
  "data": {
    "id": 123,
    "name": "Product Name",
    "is_recommended": true,
    "is_active": true,
    "is_returnable": true,
    "is_exchangeable": false,
    ...
  }
}
```

### Error Response (404 Not Found)
```json
{
  "status": "fail",
  "message": "Product not found",
  "data": []
}
```

---

## How It Works

The `toggleRecommended` method uses the base `toggle()` method from `BaseApiController`:

```php
public function toggleRecommended($id): JsonResponse
{
    return $this->toggle($id, 'is_recommended');
}
```

**Behavior:**
- If `is_recommended` is `true`, it becomes `false`
- If `is_recommended` is `false`, it becomes `true`
- Returns the updated product data

---

## Usage Examples

### Example 1: Vendor Toggles Recommended Status
```bash
curl -X PATCH \
  https://api.example.com/api/dashboard/vendor/products/123/toggle-recommended \
  -H 'Authorization: Bearer YOUR_VENDOR_TOKEN' \
  -H 'Content-Type: application/json'
```

### Example 2: Admin Toggles Recommended Status
```bash
curl -X PATCH \
  https://api.example.com/api/dashboard/admin/products/123/toggle-recommended \
  -H 'Authorization: Bearer YOUR_ADMIN_TOKEN' \
  -H 'Content-Type: application/json'
```

### Example 3: JavaScript/Axios
```javascript
// Vendor
axios.patch(`/api/dashboard/vendor/products/${productId}/toggle-recommended`, {}, {
  headers: {
    'Authorization': `Bearer ${vendorToken}`
  }
})
.then(response => {
  console.log('Recommended status:', response.data.data.is_recommended);
})
.catch(error => {
  console.error('Error:', error.response.data.message);
});

// Admin
axios.patch(`/api/dashboard/admin/products/${productId}/toggle-recommended`, {}, {
  headers: {
    'Authorization': `Bearer ${adminToken}`
  }
})
.then(response => {
  console.log('Recommended status:', response.data.data.is_recommended);
})
.catch(error => {
  console.error('Error:', error.response.data.message);
});
```

---

## Related Toggle Endpoints

| Endpoint | Field | Description |
|----------|-------|-------------|
| `PATCH /products/{product}/toggle-active` | `is_active` | Toggle product active status |
| `PATCH /products/{product}/toggle-return` | `is_returnable` | Toggle product returnable status |
| `PATCH /products/{product}/toggle-exchange` | `is_exchangeable` | Toggle product exchangeable status |
| `PATCH /products/{product}/toggle-recommended` | `is_recommended` | Toggle product recommended status |

---

## Database Field

**Table:** `products`

**Column:** `is_recommended`

**Type:** `boolean` (tinyint)

**Default:** `false` (0)

**Description:** Indicates whether the product is marked as recommended. Recommended products can be featured in special sections of the app/website.

---

## Use Cases

1. **Featured Products Section**: Display recommended products on the homepage
2. **Vendor Highlights**: Allow vendors to highlight their best products
3. **Admin Curation**: Admins can curate recommended products across all vendors
4. **Marketing Campaigns**: Mark products for special promotions
5. **Product Discovery**: Help customers find popular or high-quality products

---

## Notes

1. **Vendor Access**: Vendors can only toggle recommended status for their own products
2. **Admin Access**: Admins can toggle recommended status for any product
3. **Subscription Required**: Vendor endpoint requires active subscription
4. **Idempotent**: Calling the endpoint multiple times toggles the status each time
5. **No Validation**: No additional validation required, just toggles the boolean field

---

## Implementation Details

### Files Modified

1. **Controller (Admin):**
   - `app/Http/Controllers/Api/Dashboard/Admin/Product/ProductController.php`
   - Added `toggleRecommended()` method

2. **Routes (Vendor):**
   - `routes/api/dashboard/vendor.php`
   - Added route: `Route::patch('products/{product}/toggle-recommended', 'toggleRecommended')`

3. **Routes (Admin):**
   - `routes/api/dashboard/admin.php`
   - Added route: `Route::patch('products/{product}/toggle-recommended', 'toggleRecommended')`

### Vendor Controller
The vendor controller already had the `toggleRecommended()` method at line 55-58.

---

## Testing

### Test Case 1: Toggle from false to true
```bash
# Initial state: is_recommended = false
curl -X PATCH https://api.example.com/api/dashboard/vendor/products/123/toggle-recommended

# Expected: is_recommended = true
```

### Test Case 2: Toggle from true to false
```bash
# Initial state: is_recommended = true
curl -X PATCH https://api.example.com/api/dashboard/vendor/products/123/toggle-recommended

# Expected: is_recommended = false
```

### Test Case 3: Non-existent product
```bash
curl -X PATCH https://api.example.com/api/dashboard/vendor/products/99999/toggle-recommended

# Expected: 404 Not Found
```

### Test Case 4: Vendor accessing another vendor's product
```bash
# Vendor A tries to toggle Vendor B's product
curl -X PATCH https://api.example.com/api/dashboard/vendor/products/123/toggle-recommended

# Expected: 403 Forbidden or 404 Not Found (depending on implementation)
```
