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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
# MetaRec API Documentation

## Overview
MetaRec is a restaurant recommendation system with AI-powered preference extraction and interactive confirmation flow. The API provides intelligent restaurant recommendations based on user queries and preferences.

## Base URL
```
http://localhost:8000
```

## Authentication
No authentication required for current implementation.

---

## API Endpoints

### 1. Health Check

#### GET `/health`
Check if the API is running and healthy.

**Response:**
```json
{
  "status": "healthy",
  "timestamp": "2024-01-01T12:00:00Z"
}
```

---

### 2. Root Endpoint

#### GET `/`
Get basic API information.

**Response:**
```json
{
  "message": "MetaRec API is running!",
  "version": "1.0.0"
}
```

---

### 3. Smart Restaurant Recommendations (Primary)

#### POST `/api/recommend`
Get restaurant recommendations based on user query only. The API will intelligently extract preferences from the query.

**Request Body:**
```typescript
{
  "query": string                    // User's natural language query
}
```

**Response:**
```typescript
{
  "restaurants": Restaurant[],        // Array of recommended restaurants (empty for confirmation)
  "confirmation_request": {          // Always present for confirmation
    "message": string,               // Full prompt with extracted preferences
    "preferences": Record<string, any>, // Extracted preferences
    "needs_confirmation": boolean    // Always true
  }
}
```

**Example Request:**
```json
{
  "query": "I want a romantic dinner for date night, budget 100-200 SGD, near Marina Bay"
}
```

**Example Response:**
```json
{
  "restaurants": [],
  "confirmation_request": {
    "message": "Based on your query 'I want a romantic dinner for date night, budget 100-200 SGD, near Marina Bay', I understand you want:\n\nβ€’ Restaurant Type: Fine Dining\nβ€’ Dining Purpose: Date Night\nβ€’ Budget Range: 100-200 SGD per person\nβ€’ Location: Marina Bay\n\nIs this correct?",
    "preferences": {
      "restaurant_types": ["fine-dining"],
      "flavor_profiles": ["any"],
      "dining_purpose": "date-night",
      "budget_range": {
        "min": 100,
        "max": 200,
        "currency": "SGD",
        "per": "person"
      },
      "location": "Marina Bay"
    },
    "needs_confirmation": true
  }
}
```

---

### 4. Direct Recommendations with Constraints

#### POST `/api/recommend-with-constraints`
Get restaurant recommendations with explicit constraints (bypasses confirmation).

**Request Body:**
```typescript
{
  "query": string,                    // User's natural language query
  "constraints": {
    "restaurantTypes": string[],      // ["casual", "fine-dining", "fast-casual", "street-food", "buffet", "cafe"]
    "flavorProfiles": string[],       // ["spicy", "savory", "sweet", "sour", "mild"]
    "diningPurpose": string,          // "date-night", "family", "business", "solo", "friends", "celebration"
    "budgetRange": {
      "min": number,                  // Optional: minimum budget
      "max": number,                  // Optional: maximum budget
      "currency": "SGD" | "USD" | "CNY" | "EUR",
      "per": "person" | "table"
    },
    "location": string                // Optional: location preference
  },
  "meta": {
    "source": string,                 // "MetaRec-UI"
    "sentAt": string,                 // ISO timestamp
    "uiVersion": string               // "0.0.1"
  }
}
```

**Response:**
```typescript
{
  "restaurants": Restaurant[],        // Array of recommended restaurants
  "thinking_steps": ThinkingStep[]    // AI thinking process
}
```

**Response Types:**
```typescript
interface Restaurant {
  id: string
  name: string
  cuisine?: string
  location?: string
  rating?: number
  price?: number
  highlights?: string[]
  reason?: string
  reference?: string
}

interface ThinkingStep {
  step: string
  description: string
  status: "thinking" | "completed" | "error"
  details?: string
}
```

---

### 5. Confirm Preferences and Start Processing

#### POST `/api/confirm`
Confirm user preferences and start background processing task.

**Request Body:**
```typescript
{
  "query": string,                    // Original user query
  "preferences": Record<string, any>  // Extracted preferences from confirmation
}
```

**Response:**
```typescript
{
  "task_id": string,                  // Task ID for polling status
  "message": string                   // Confirmation message
}
```

**Example Request:**
```json
{
  "query": "I want a romantic dinner for date night, budget 100-200 SGD, near Marina Bay",
  "preferences": {
    "restaurant_types": ["fine-dining"],
    "flavor_profiles": ["any"],
    "dining_purpose": "date-night",
    "budget_range": {
      "min": 100,
      "max": 200,
      "currency": "SGD",
      "per": "person"
    },
    "location": "Marina Bay"
  }
}
```

**Example Response:**
```json
{
  "task_id": "123e4567-e89b-12d3-a456-426614174000",
  "message": "Task started successfully"
}
```

---

### 6. Get Task Status

#### GET `/api/status/{task_id}`
Get the current status of a processing task.

**Response:**
```typescript
{
  "task_id": string,
  "status": "processing" | "completed" | "error",
  "progress": number,                 // 0-100
  "message": string,
  "result"?: RecommendationResponse,  // Present when completed
  "error"?: string                   // Present when error
}
```

**Example Response (Processing):**
```json
{
  "task_id": "123e4567-e89b-12d3-a456-426614174000",
  "status": "processing",
  "progress": 50,
  "message": "Searching restaurant database...",
  "result": null,
  "error": null
}
```

**Example Response (Completed):**
```json
{
  "task_id": "123e4567-e89b-12d3-a456-426614174000",
  "status": "completed",
  "progress": 100,
  "message": "Recommendations ready!",
  "result": {
    "restaurants": [
      {
        "id": "1",
        "name": "Odette",
        "cuisine": "French",
        "location": "Marina Bay",
        "rating": 4.8,
        "price": "$$$$",
        "highlights": ["Fine Dining", "3 Michelin Stars", "Romantic"],
        "reason": "World-class French cuisine with impeccable service and atmosphere",
        "reference": "https://www.odetterestaurant.com"
      }
    ],
    "thinking_steps": [
      {
        "step": "analyze_query",
        "description": "Analyzing your requirements...",
        "status": "completed",
        "details": "Identified keywords: romantic, dinner, date, night, budget, SGD, Marina, Bay"
      }
    ]
  },
  "error": null
}
```

---

### 7. Update Preferences

#### POST `/api/update-preferences`
Update user preferences and get processed constraints.

**Request Body:**
```typescript
{
  "restaurantTypes": string[],        // ["casual", "fine-dining", "fast-casual", "street-food", "buffet", "cafe"]
  "flavorProfiles": string[],         // ["spicy", "savory", "sweet", "sour", "mild"]
  "diningPurpose": string,            // "date-night", "family", "business", "solo", "friends", "celebration"
  "budgetRange": {
    "min": number,                    // Optional: minimum budget
    "max": number,                    // Optional: maximum budget
    "currency": "SGD" | "USD" | "CNY" | "EUR",
    "per": "person" | "table"
  },
  "location": string                  // Optional: location preference
}
```

**Response:**
```typescript
{
  "message": string,
  "preferences": Record<string, any>  // Processed preferences
}
```

---

### 5. Get All Restaurants (Debug)

#### GET `/api/restaurants`
Get all available restaurants in the database (for debugging purposes).

**Response:**
```json
{
  "restaurants": [
    {
      "id": "1",
      "name": "Din Tai Fung",
      "cuisine": "Taiwanese",
      "location": "Orchard",
      "rating": 4.2,
      "price": "$$",
      "highlights": ["Xiao Long Bao", "Noodles", "Family-friendly"],
      "reason": "Perfect for family dining with authentic Taiwanese cuisine and famous soup dumplings",
      "reference": "https://www.dintaifung.com.sg"
    }
  ]
}
```

---

## Frontend-Backend Interaction Flow

### 1. Smart Recommendation Flow (Primary)
```
1. Frontend β†’ POST /api/recommend (query only)
2. Backend β†’ Response with confirmation_request (always)
3. Frontend β†’ Display confirmation dialog with full prompt
4. User β†’ Confirm preferences
5. Frontend β†’ POST /api/confirm (query + preferences)
6. Backend β†’ Response with task_id
7. Frontend β†’ Poll GET /api/status/{task_id} until completed
8. Backend β†’ Response with recommendations + thinking_steps
```

### 2. Direct Recommendation Flow (Bypass Confirmation)
```
1. Frontend β†’ POST /api/recommend-with-constraints (query + constraints)
2. Backend β†’ Response with recommendations + thinking_steps
```

### 3. Manual Preferences Update
```
1. Frontend β†’ User modifies preferences in UI
2. Frontend β†’ POST /api/update-preferences (preferences)
3. Backend β†’ Response with processed preferences
4. Frontend β†’ Update UI with processed preferences
```

---

## Error Handling

### HTTP Status Codes
- `200` - Success
- `422` - Validation Error (invalid request body)
- `500` - Internal Server Error

### Error Response Format
```json
{
  "detail": "Error message description"
}
```

### Common Error Scenarios
1. **Invalid JSON**: Malformed request body
2. **Missing required fields**: Required fields not provided
3. **Server error**: Internal processing error

---

## Data Models

### Restaurant Types
- `casual` - Casual Dining
- `fine-dining` - Fine Dining
- `fast-casual` - Fast Casual
- `street-food` - Street Food
- `buffet` - Buffet
- `cafe` - Cafe

### Flavor Profiles
- `spicy` - Spicy
- `savory` - Savory
- `sweet` - Sweet
- `sour` - Sour
- `mild` - Mild

### Dining Purposes
- `date-night` - Date Night
- `family` - Family Dining
- `business` - Business Meeting
- `solo` - Solo Dining
- `friends` - Friends Gathering
- `celebration` - Celebration

### Price Levels
- `$` - Budget (under $20)
- `$$` - Moderate ($20-40)
- `$$$` - Expensive ($40-80)
- `$$$$` - Very Expensive ($80+)

---

## CORS Configuration
The API is configured to accept requests from:
- `http://localhost:5173` (Vite dev server)
- `http://127.0.0.1:5173` (Alternative localhost)

---

## Rate Limiting
Currently no rate limiting implemented.

---

## Testing
Use the interactive API documentation at:
```
http://localhost:8000/docs
```

This provides a Swagger UI for testing all endpoints directly in the browser.