# Task 021 Phase 2 — AI Content Generation, Lead Intelligence & Smart Segments

## Scope

Build missing AI capabilities on top of Task 012/020 inbox classification. **Do not duplicate** inbox AI panel or classification flows.

## Deliverables

### Part 1 — Brand Voice
- [x] Settings → AI → Brand Voice UI
- [x] `SystemSetting.brandVoice` + `aiEnabled`
- [x] Auto-inject into all generation

### Part 2 — AI Email Writer
- [x] Templates, Campaigns, Sequences integration
- [x] `POST /api/ai/email-writer`
- [x] Required language selector (ar/en) with brand voice default

### Part 3 — AI Template Generator
- [x] Full template types
- [x] `POST /api/ai/template-generator`
- [x] Apply fills editor + preview instantly

### Part 4 — AI Sequence Generator
- [x] `/sequences/new` panel
- [x] `POST /api/ai/sequence-generator`
- [x] Language-aware step generation

### Part 5 — AI Segment Builder
- [x] `/segments/new` NL → filters
- [x] `POST /api/ai/segment-builder`

### Part 6 — AI Lead Scoring
- [x] Fields on Lead, Company, Person
- [x] Single + bulk API
- [x] CRM UI panels

### Part 7 — Next Best Action
- [x] `ai-next-best-action.service.ts`
- [x] CRM + inbox panels

### Part 8 — Follow-Up Generator
- [x] Inbox panel
- [x] `POST /api/ai/follow-up`

### Part 9 — Audit Log
- [x] `AiActionLog` model + `logAiAction()`

### Part 10 — Settings Validation
- [x] `checkAiGuard()` + `GET /api/ai/guard`

### Part 11 — Documentation
- [x] `docs/26-ai-content-generation.md`
- [x] Changelog + tasks index

## Acceptance

- Existing inbox AI unchanged
- Brand voice influences generation
- Graceful errors when AI disabled / no API key
- `npm run lint` + `npm run build` pass

See [docs/26-ai-content-generation.md](../docs/26-ai-content-generation.md).
