# Phase 7 — Unified Social Inbox
## Weeks 13–14

---

## STEP 7.1 — Inbox Aggregation

### Command: `inbox:sync` (runs every 5 minutes via Task Scheduler)

For each active SocialAccount, fetch:
- Instagram: comments on recent posts + DMs (Graph API)
- Facebook: page comments + messages (Graph API)
- Twitter: mentions (Twitter API v2 filtered stream OR polling)
- LinkedIn: post comments
- YouTube: video comments

Store in `inbox_messages` table (already in schema).

### Real-time (Webhooks):
- Register webhooks for Instagram, Facebook (Meta webhook subscriptions)
- `POST /webhooks/meta` → `WebhookController@meta`
- Verify with hub.verify_token on subscription
- On new message event: store + broadcast via Laravel Reverb
- `Livewire.on('new-inbox-message', () => $wire.refresh())`

---

## STEP 7.2 — Inbox UI

**Route:** `GET /inbox` → Livewire: `Inbox\InboxDashboard`

UI (3-column layout):
```
┌──────────────────┬────────────────────────────┬───────────────────────┐
│ FILTERS          │ MESSAGE LIST               │ MESSAGE DETAIL        │
│                  │                            │                       │
│ All (12)         │ 🐦 @user1 • 2m ago         │ [Avatar] @user1       │
│ Twitter (5)      │ "Love this content!"        │ Posted on: Tweet #xyz │
│ Instagram (4)    │ 💚 Positive                 │                       │
│ Facebook (3)     │ ────────────────────────── │ "Love this content!"  │
│                  │ 📘 John M. • 15m ago       │                       │
│ Comments         │ "What's the price?"         │ 😊 Sentiment: Positive│
│ DMs              │ 💛 Neutral                  │                       │
│ Mentions         │ ────────────────────────── │ [Reply textarea]      │
│                  │ 📸 sarah_k • 1h ago         │ [AI Suggest Reply]    │
│ Unread only      │ "Amazing shot! 🔥"          │ Tone: [Friendly ▼]    │
│ Starred          │ 💚 Positive                 │                       │
│                  │                            │ [Send Reply]          │
│ [Search...]      │                            │ [Mark Resolved]       │
│                  │                            │ [Star] [Archive]      │
└──────────────────┴────────────────────────────┴───────────────────────┘
```

Unread count badges in sidebar navigation (uses Reverb WebSocket broadcast).

---

## STEP 7.3 — AI-Assisted Replies

### `app/Services/AI/ReplyGeneratorService.php`
```php
public function suggestReply(InboxMessage $message, string $tone): string
// Prompt: "Generate a {tone} reply to this {platform} comment: '{message}'"
// Tone options: friendly, professional, apologetic, playful

public function checkAutoReplyRules(InboxMessage $message): ?ReplyTemplate
// Checks if message body matches any keyword in reply_templates
// Returns matching template or null
```

### Auto-Reply Rules UI (in Settings):
```
[+ Add Rule]
Keyword: [price]       Template: [Here's our pricing: ...]   Platform: [All ▼]
Keyword: [thank you]   Template: [Thank you so much! ...]    Platform: [Instagram ▼]
```

---

## COMPLETION CHECKLIST
- [ ] inbox:sync command working for all platforms
- [ ] Meta webhook receiver (verify + process)
- [ ] Real-time inbox updates via Reverb
- [ ] 3-column inbox UI (filter | list | detail)
- [ ] Sentiment badge per message (Gemini)
- [ ] Reply directly from dashboard
- [ ] Mark read/unread, resolve, star, archive
- [ ] AI reply suggestion working
- [ ] Tone selector for AI replies
- [ ] Saved reply templates
- [ ] Auto-reply rules working
- [ ] Unread count badges in navigation

When done: update to `8`, commit `feat(phase7): complete unified social inbox`, stop.
