## 1. FastAPI Pattern Detection Endpoints

- [x] 1.1 Extract pattern detection logic from `generate_talib_annotations.py` into a reusable module (e.g., `app/patterns.py`) with `detect_patterns(candles, pattern_names)` function
- [x] 1.2 Add `GET /patterns/available` endpoint returning all supported CDL pattern names with display names
- [x] 1.3 Add `POST /patterns/detect` endpoint accepting `{candles, patterns}`, running selected CDL functions, returning span annotations with source "talib"
- [x] 1.4 Add input validation: reject invalid pattern names with HTTP 400, handle empty patterns list as "run all"

## 2. FastAPI Training Endpoints

- [x] 2.1 Add `POST /training/start` endpoint that launches training in a background thread, returns `{run_id, status: "running"}`, and rejects concurrent runs with HTTP 409
- [x] 2.2 Add `GET /training/runs` endpoint returning training run history from the `training_runs` table, sorted by date descending
- [x] 2.3 Add `GET /training/dataset-info` endpoint returning labeled dataset file path, existence, size, and row count
- [x] 2.4 Add background training thread management: track active run, update DB status on completion/failure

## 3. FastAPI Model Loading Endpoint

- [x] 3.1 Add `POST /model/load` endpoint accepting `{run_id}`, looking up the training run, loading the model artifact, and replacing the active model in `AppState`
- [x] 3.2 Add thread-safe model swap with locking to prevent conflicts with in-flight prediction requests

## 4. Next.js Proxy Routes

- [x] 4.1 Add `GET /api/patterns/available` proxy route
- [x] 4.2 Add `POST /api/patterns/detect` proxy route
- [x] 4.3 Add `POST /api/training/start` proxy route
- [x] 4.4 Add `GET /api/training/runs` proxy route
- [x] 4.5 Add `GET /api/training/dataset-info` proxy route
- [x] 4.6 Add `POST /api/model/load` proxy route
- [x] 4.7 Extend `DELETE /api/span-annotations` to support `source` and `label` query parameters for bulk deletion

## 5. TA-Lib Pattern UI Panel

- [x] 5.1 Create `TalibPatternPanel` component with collapsible section, fetching available patterns from `/api/patterns/available` on mount
- [x] 5.2 Add pattern checkboxes grouped by category with "Select All" / "Deselect All" toggle
- [x] 5.3 Add "Detect Patterns" button that sends selected patterns + chart candles to `/api/patterns/detect`, shows loading state
- [x] 5.4 Save detection results as span annotations via `POST /api/span-annotations` with `source: "talib"` and refresh the annotation list
- [x] 5.5 Add detection results summary showing pattern counts grouped by name
- [x] 5.6 Add "Clear All TA-Lib" bulk delete button calling `DELETE /api/span-annotations?source=talib&chartId=X`
- [x] 5.7 Add per-pattern-type delete in results summary

## 6. Training UI Panel

- [x] 6.1 Create `TrainingPanel` component with collapsible section
- [x] 6.2 Add model type dropdown (Random Forest / XGBoost) defaulting to Random Forest
- [x] 6.3 Add dataset info display fetching from `/api/training/dataset-info`, showing warning if missing
- [x] 6.4 Add "Start Training" button with loading state, disabled when training active or dataset missing
- [x] 6.5 Add training status polling (5s interval) while a run is active, showing progress indicator
- [x] 6.6 Add training completion/failure handling with success message and metrics display
- [x] 6.7 Add training run history list fetching from `/api/training/runs`, showing 5 most recent runs with model type, status, date, metrics

## 7. Model Selector Integration

- [x] 7.1 Create `ModelSelector` dropdown component fetching completed training runs from `/api/training/runs`
- [x] 7.2 Integrate `ModelSelector` into `PredictionPanel` above action buttons, showing current model as active
- [x] 7.3 Wire model switch: on selection call `POST /api/model/load`, clear prediction cache, refresh model info
- [x] 7.4 Handle model load errors: show error toast, keep previous model active

## 8. Sidebar Layout Integration

- [x] 8.1 Add `TalibPatternPanel` to the sidebar in `page.tsx` between SpanAnnotationList and PredictionPanel
- [x] 8.2 Add `TrainingPanel` to the sidebar between TalibPatternPanel and PredictionPanel
- [x] 8.3 Make TalibPatternPanel, TrainingPanel, and PredictionPanel collapsible (default collapsed for new panels)
- [x] 8.4 Wire all new component state and callbacks in `page.tsx`
