# AI Cook & Auto-Fill Cart Feature

This plan details the implementation of Task 5: integrating an AI-powered feature that takes a user's natural language request (e.g., "I want to cook Butter Chicken for 4 people"), generates a recipe with ingredients using an LLM, and automatically matches those ingredients with available products in the store for easy checkout.

## User Review Required

> [!IMPORTANT]
> Please review the proposed architecture and UI flow. Since this feature requires querying external AI services, we will need to add the provided API keys (Groq, OpenRouter, Cloudflare) to your `.env` file.

> [!WARNING]
> Because product names in the store might not exactly match the ingredient names generated by the AI (e.g., AI: "Chicken Breast", Store: "Fresh Halal Chicken Breast 500g"), we will implement a fuzzy search/matching algorithm. Please confirm if you want the system to automatically pick the best match, or show a few options for the user to select from before adding to the cart.

## Open Questions

1. **User Interface**: Where should the entry point for this feature be? A dedicated page (e.g., `/cook-with-ai`), a floating button, or embedded in the homepage/cart page?
2. **Product Matching**: If an ingredient is out of stock or cannot be found, should we show a warning message, or completely omit it from the list?
3. **AI Models**: Do you have a preferred model to use for Groq (e.g., `llama-3.1-70b-versatile` or `llama-3.1-8b-instant`)?

## Proposed Changes

---

### Backend Service (AI Integration)

We will create a robust service to handle the AI calls with built-in fallback mechanisms.

#### [NEW] `app/Services/RecipeAiService.php`
- Implement the logic to call Groq via Laravel's HTTP client.
- Include a specific system prompt instructing the LLM to return a strict JSON structure containing the recipe name, instructions, and an array of required ingredients.
- Implement `try-catch` blocks to seamlessly fallback to OpenRouter, and then to Cloudflare Workers AI if Groq fails or rate-limits.

---

### Backend Controller & Routing

#### [NEW] `app/Http/Controllers\Frontend\AiRecipeController.php`
- **Method `index()`**: Render the AI recipe generation page.
- **Method `generate()`**: Accept the user's prompt, call the `RecipeAiService` to get the JSON response. Parse the JSON and iterate through the ingredients.
- For each ingredient, perform a database search against the `products` table using the existing search logic (`name LIKE %ingredient%` or Full-Text search) to find the closest matching products in stock.
- Return the recipe, matched products, and unmatched ingredients to the frontend.

#### [MODIFY] `routes/web.php`
- Add routes for `GET /cook-with-ai` and `POST /cook-with-ai/generate`.

---

### Frontend Configuration

#### [NEW] `resources/views/frontend/pages/ai-recipe.blade.php`
- A sleek, modern page where users can type their prompt.
- Will display a loading animation (skeleton loader or spinner) while waiting for the AI response.
- Once generated, it will display the recipe instructions on one side, and the shoppable ingredients on the other side.

#### [NEW] `resources/views/frontend/pages/ai-recipe-results.blade.php` (or Vue Component)
- Display a "Match Found" card for each ingredient.
- Include a 1-click "Add All to Cart" button that submits all matched product IDs and quantities to the existing CartController.

---

### Configuration

#### [MODIFY] `config/services.php`
- Add configurations for `groq`, `openrouter`, and `cloudflare` using `env()` variables.

#### [MODIFY] `.env.example`
- Document the required keys: `GROQ_API_KEY`, `OPENROUTER_API_KEY`, `CLOUDFLARE_AI_KEY`, `CLOUDFLARE_ACCOUNT_ID`.

## Verification Plan

### Automated Tests
- Create a unit test `Tests\Unit\RecipeAiServiceTest` mocking the HTTP facade to ensure the fallback mechanism triggers correctly when Groq returns a 500 or timeout error.
- Create a feature test `Tests\Feature\AiRecipeControllerTest` to verify that an AI-generated JSON response correctly maps to database products and renders the view.

### Manual Verification
- Go to `/cook-with-ai`.
- Enter "I want to cook a simple Dal Makhani".
- Verify that the API successfully returns a recipe.
- Verify that the ingredients (like Urad Dal, Butter, Cream) correctly match with actual products in the Punjabi Mandi database.
- Click "Add All to Cart" and verify the cart total updates correctly.
