# DB-Driven AI Config

**Task**: AZ-174_db_driven_ai_config
**Name**: Fetch AIRecognitionConfig from DB by userId instead of UI-passed config
**Description**: Replace UI-passed AI configuration with database-driven config fetched by userId from the annotations service.
**Complexity**: 2 points
**Dependencies**: Annotations service needs new endpoint `GET /api/users/{userId}/ai-settings`
**Component**: Configuration
**Jira**: AZ-174
**Parent**: AZ-172

## Problem

`AIRecognitionConfig` is currently built from a dict passed by the caller (UI). In the distributed architecture, the UI should not own or pass detection configuration — it should be stored server-side per user.

## Current State

- `main.py`: `AIConfigDto` Pydantic model with hardcoded defaults, passed as `config_dict`
- `ai_config.pyx`: `AIRecognitionConfig.from_dict(data)` builds from dict with defaults
- Camera settings (`altitude`, `focal_length`, `sensor_width`) baked into the config DTO
- No DB interaction for config

## Target State

- Extract userId from JWT (already parsed in `TokenManager._decode_exp`)
- Call annotations service: `GET /api/users/{userId}/ai-settings`
- Response contains merged `AIRecognitionSettings` + `CameraSettings` fields
- Build `AIRecognitionConfig` from the API response
- Remove `AIConfigDto` from `main.py` (or keep as optional override for testing)
- Remove `paths` field from `AIRecognitionConfig` entirely

## DB Tables (from schema)

**AIRecognitionSettings:**
- FramePeriodRecognition (default 4)
- FrameRecognitionSeconds (default 2)
- ProbabilityThreshold (default 0.25)
- TrackingDistanceConfidence
- TrackingProbabilityIncrease
- TrackingIntersectionThreshold
- ModelBatchSize
- BigImageTileOverlapPercent

**CameraSettings:**
- Altitude (default 400m)
- FocalLength (default 24mm)
- SensorWidth (default 23.5mm)

**Linking:** These tables currently have no FK to Users. The backend needs either:
- Add `UserId` FK to both tables, or
- Create a `UserAIConfig` join table referencing both

## Backend Dependency

The annotations C# service needs:
1. New endpoint: `GET /api/users/{userId}/ai-settings` returning merged config
2. On user creation: seed default `AIRecognitionSettings` + `CameraSettings` rows
3. Optional: `PUT /api/users/{userId}/ai-settings` for user to update their config

## Acceptance Criteria

- [ ] Detection endpoint extracts userId from JWT
- [ ] AIRecognitionConfig is fetched from annotations service by userId
- [ ] Fallback to sensible defaults if service is unreachable
- [ ] `paths` field removed from `AIRecognitionConfig`
- [ ] Camera settings come from DB, not request payload

## File Changes

| File | Action | Description |
|------|--------|-------------|
| `src/main.py` | Modified | Fetch config from annotations service via HTTP |
| `src/ai_config.pxd` | Modified | Remove `paths` field |
| `src/ai_config.pyx` | Modified | Remove `paths` from `__init__` and `from_dict` |
| `src/loader_http_client.pyx` | Modified | Add method to fetch user AI config |
| `src/loader_http_client.pxd` | Modified | Declare new method |
