# RabbitMQ Sarvam API Documentation

## 🚀 Server Setup

```bash
# Install dependencies
python3 -m pip install -r requirements.txt

# Start the API server
python app.py
```

Server will start on: `http://localhost:5000`

---

## 📋 Available Endpoints

### 1. Health Check
**GET** `/health`

**Response:**
```json
{
  "status": "healthy",
  "message": "RabbitMQ Sarvam API is running",
  "version": "1.0.0"
}
```

---

### 2. Queue Calls (JSON Body)
**POST** `/queue-calls`

**Request Body:**
```json
{
  "bid": "6840"
}
```

**Response:**
```json
{
  "success": true,
  "message": "Successfully queued 5 calls for business: 6840",
  "business_id": "6840",
  "queued_count": 5,
  "calls": [
    {"callid": "12345", "bid": "6840"},
    {"callid": "12346", "bid": "6840"}
  ]
}
```

---

### 3. Queue Calls (URL Parameter)
**POST** `/queue-calls/6840`

**Response:**
```json
{
  "success": true,
  "message": "Successfully queued 3 calls for business: 6840",
  "business_id": "6840",
  "queued_count": 3,
  "calls": [
    {"callid": "12345", "bid": "6840"}
  ]
}
```

---

### 4. Process Calls Directly (JSON Body)
**POST** `/process-calls`

**Request Body:**
```json
{
  "bid": "6840"
}
```

**Response:**
```json
{
  "success": true,
  "message": "Calls processed successfully",
  "business_id": "6840",
  "result": {"message": "Translation batch completed"}
}
```

---

### 5. Process Calls Directly (URL Parameter)
**POST** `/process-calls/6840`

**Response:**
```json
{
  "success": true,
  "message": "Calls processed successfully",
  "business_id": "6840",
  "result": {"message": "Translation batch completed"}
}
```

---

### 6. List All Businesses
**GET** `/list-businesses`

**Response:**
```json
{
  "success": true,
  "businesses": [
    {
      "bid": "6840",
      "calls_table": "6840_calls",
      "response_table": "6840_sarvamresponse"
    },
    {
      "bid": "7417",
      "calls_table": "7417_calls",
      "response_table": "7417_sarvamresponse"
    }
  ],
  "count": 2
}
```

---

## 🔧 Postman Setup

### 1. Create New Collection
- Name: `RabbitMQ Sarvam API`

### 2. Add Environment Variables
- `base_url`: `http://localhost:5000`

### 3. Sample Requests

#### Queue Calls for Business 6840
- **Method**: POST
- **URL**: `{{base_url}}/queue-calls`
- **Headers**: 
  - `Content-Type: application/json`
- **Body** (raw JSON):
```json
{
  "bid": "6840"
}
```

#### Queue Calls for Business 7417
- **Method**: POST
- **URL**: `{{base_url}}/queue-calls/7417`

#### Process Calls Directly
- **Method**: POST
- **URL**: `{{base_url}}/process-calls`
- **Headers**: 
  - `Content-Type: application/json`
- **Body** (raw JSON):
```json
{
  "bid": "6840"
}
```

#### Health Check
- **Method**: GET
- **URL**: `{{base_url}}/health`

#### List Businesses
- **Method**: GET
- **URL**: `{{base_url}}/list-businesses`

---

## 🎯 Usage Examples

### Queue Processing (Recommended)
1. **Queue calls**: `POST /queue-calls/6840`
2. **Start consumer**: `python audio_job_consumer.py`
3. **Monitor processing**: Check logs

### Direct Processing
1. **Process directly**: `POST /process-calls/6840`
2. **No RabbitMQ needed**: Processes immediately

---

## ⚠️ Error Responses

### 400 Bad Request
```json
{
  "success": false,
  "error": "No JSON data provided",
  "message": "Please provide JSON with 'bid' field"
}
```

### 500 Internal Server Error
```json
{
  "success": false,
  "error": "Database connection failed",
  "message": "Failed to queue calls"
}
```

---

## 🔄 Complete Workflow

### Option 1: Queue + Consumer (Recommended)
1. **Start API**: `python app.py`
2. **Queue calls**: `POST /queue-calls/6840`
3. **Start consumer**: `python audio_job_consumer.py`
4. **Monitor**: Check logs for processing status

### Option 2: Direct Processing
1. **Start API**: `python app.py`
2. **Process calls**: `POST /process-calls/6840`
3. **Monitor**: Check API response

---

## 📊 Response Codes

- **200**: Success
- **400**: Bad Request (invalid JSON/data)
- **404**: Endpoint not found
- **500**: Internal Server Error
