# Deployment Guide - insert_menu_items Service

## Quick Fix for Server Error

The error you encountered means the `INSERT_MENU_ITEMS_SERVICE_KEY` environment variable is not set on the server.

### ✅ Fix Applied

The service config has been updated to make this optional. The service will now:
- Use `INSERT_MENU_ITEMS_SERVICE_KEY` if set
- Fall back to `AUTHORIZATION_KEY` (global token) if not set

## Deployment Steps

### 1. Update Environment Variables (Optional)

If you want a separate token for this service, add to your server's `.env` file or environment:

```bash
INSERT_MENU_ITEMS_SERVICE_KEY=1f82e364-9c2d-4c59-86b0-8f83c7f0285f
```

**Or** just use the global token (already set):
```bash
AUTHORIZATION_KEY=1f82e364-9c2d-4c59-86b0-8f83c7f0285f
```

### 2. Rebuild and Restart

```bash
# If using Docker
docker-compose down
docker-compose up -d --build

# If running directly
uvicorn app.main:app --reload
```

### 3. Verify Service Started

Check logs for:
```
insert_menu_items_service_initialized
```

### 4. Test the Endpoint

```bash
curl --location 'http://your-server:8000/menu-items/insert' \
--header 'Authorization: Bearer 1f82e364-9c2d-4c59-86b0-8f83c7f0285f' \
--form 'file=@"menu.jpg"' \
--form 'city="sofia"' \
--form 'token="8800a3a64cfa947c552391f48069cacd"'
```

Expected response (202 Accepted):
```json
{
  "status": "processing",
  "message": "Image uploaded successfully. Menu extraction and creation in progress.",
  "request_id": "550e8400-e29b-41d4-a716-446655440000"
}
```

## Environment Variables Summary

### Required (Already Set)
- `AUTHORIZATION_KEY` - Global auth token ✅
- `OPENAI_API_KEY_MENU_EXTRACTOR` - For menu extraction ✅

### Optional
- `INSERT_MENU_ITEMS_SERVICE_KEY` - Service-specific token (falls back to AUTHORIZATION_KEY)

## Docker Deployment

If using Docker, make sure your `docker-compose.yml` or Dockerfile passes the environment variables:

### docker-compose.yml
```yaml
services:
  app:
    environment:
      - AUTHORIZATION_KEY=${AUTHORIZATION_KEY}
      - OPENAI_API_KEY_MENU_EXTRACTOR=${OPENAI_API_KEY_MENU_EXTRACTOR}
      - INSERT_MENU_ITEMS_SERVICE_KEY=${INSERT_MENU_ITEMS_SERVICE_KEY:-${AUTHORIZATION_KEY}}
```

### Dockerfile ENV (if needed)
```dockerfile
ENV AUTHORIZATION_KEY=${AUTHORIZATION_KEY}
ENV INSERT_MENU_ITEMS_SERVICE_KEY=${INSERT_MENU_ITEMS_SERVICE_KEY}
```

## Troubleshooting

### Service doesn't start
1. Check logs: `docker logs <container_name>` or server logs
2. Verify `AUTHORIZATION_KEY` is set
3. Verify `config/services/insert_menu_items.yaml` exists and has `enabled: true`

### Authentication fails
1. Verify token matches in request header
2. Check if using global token or service-specific token
3. Logs will show: `authentication_successful` or `invalid_bearer_token`

### Menu extraction fails
1. Verify `OPENAI_API_KEY_MENU_EXTRACTOR` is set
2. Check OpenAI API quota and rate limits
3. Logs will show: `STEP_1_FAILED: Menu extraction error`

### createMenu.php fails
1. Check logs for curl command: `logs/curl_commands.log`
2. Test curl manually to verify API is accessible
3. Verify city and token are correct
4. Logs will show: `API_REQUEST_HTTP_ERROR` or `API_REQUEST_SUCCESS`

## Log Files

After deployment, monitor these files:
- `logs/app.log` - All application logs
- `logs/curl_commands.log` - All curl commands sent to createMenu.php

## Health Check

```bash
# Check if service is running
curl http://your-server:8000/health

# Check specific service endpoint
curl http://your-server:8000/docs
# Look for /menu-items/insert endpoint
```

## Rollback

If there are issues, you can disable the service:

**config/services/insert_menu_items.yaml**
```yaml
enabled: false
```

Then restart the server. The app will continue to work without this service.
