# AI Service API Reference

The AI service is a standalone FastAPI (Python) application that provides AI-powered test suggestions. It runs separately from the main Node.js API.

**Default URL**: `http://localhost:8000`  
**Configure via**: `AI_SERVICE_URL` environment variable

---

## GET /

Returns service info.

**Authentication**: None

**Response**:
```json
{
  "message": "Izri AI Service",
  "version": "1.0.0",
  "status": "healthy"
}
```

---

## GET /health

Health check. Returns current AI provider.

**Authentication**: None

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

---

## GET /provider

Returns the currently active AI provider name.

**Authentication**: None

**Response**:
```json
{
  "provider": "openai"
}
```

**Possible values**: `openai`, `anthropic`, `mock`

Provider is selected automatically:
1. If `AI_PROVIDER` env var is set, it is used
2. If `ANTHROPIC_API_KEY` is set (and no `OPENAI_API_KEY`), uses `anthropic`
3. If `OPENAI_API_KEY` is set, uses `openai`
4. Otherwise falls back to `mock`

---

## POST /suggest-tests

Accept repo-analyzer output and return AI-generated test suggestions.

**Authentication**: None (called internally from the Node.js API)

**Request body**:
```json
{
  "analysis": { /* AnalysisResult from repo-analyzer */ },
  "max_suggestions": 10
}
```

| Field | Type | Required | Description |
|-------|------|----------|-------------|
| `analysis` | object | Yes | Full `AnalysisResult` JSON from the repo-analyzer package |
| `max_suggestions` | integer | No | Max number of suggestions to return (default: 10) |

**Response**:
```json
{
  "commit_sha": "abc123def456",
  "summary": "Found 5 files with missing test coverage",
  "suggestions": [
    {
      "file_path": "src/utils/helpers.ts",
      "test_type": "unit",
      "description": "Add unit tests for date formatting utilities",
      "rationale": "No existing tests cover edge cases for null/invalid inputs",
      "example_code": "describe('formatDate', () => { ... })"
    }
  ],
  "suggestion_count": 5
}
```

**Error responses**:
- `500` — Analysis failed (malformed input or AI provider error)

---

## POST /analyze

Analyze a codebase. (Legacy endpoint — prefer `/suggest-tests` for the E2E workflow.)

**Request body**:
```json
{
  "repository_url": "https://github.com/owner/repo",
  "language": "typescript",
  "framework": "react",
  "branch": "main"
}
```

---

## POST /generate-tests

Generate test cases from analysis output. Returns mock data unless a real AI provider is configured.

**Request body**:
```json
{
  "code_analysis": { /* analysis object */ },
  "test_type": "unit",
  "framework": "jest",
  "ai_provider": "openai",
  "ai_model": "gpt-4"
}
```

**Response**:
```json
{
  "tests": { "unit_tests": [...], "integration_tests": [...], "e2e_tests": [...] },
  "coverage_estimate": 85.5,
  "test_count": 3,
  "execution_time": "2m 30s"
}
```
