# Inbox Test Commands (Tinker)

Run the seeder first, then use any command below.

```bash
php artisan db:seed --class=InboxDemoSeeder
php artisan tinker
```

---

## Demo IDs (seeded by InboxDemoSeeder)

| Channel    | Key                  | Value                          |
|------------|----------------------|--------------------------------|
| WhatsApp   | phone_number_id      | `DEMO_PHONE_ID_111222333`      |
| WhatsApp   | verify_token         | `demo-whatsapp-verify-token-abc123` |
| Instagram  | instagram_page_id    | `DEMO_IG_PAGE_ID_444555666`    |
| Instagram  | instagram_account_id | `DEMO_IG_ACCOUNT_ID_999888777` |
| Messenger  | page_id              | `DEMO_MESSENGER_PAGE_ID_777888999` |

---

## 1. WhatsApp — Incoming Text Message

```php
app(\App\Modules\Whatsapp\Services\WhatsappDriver::class)->processWebhookPayload([
    'object' => 'whatsapp_business_account',
    'entry' => [[
        'id' => 'DEMO_WABA_123456789',
        'changes' => [[
            'field' => 'messages',
            'value' => [
                'messaging_product' => 'whatsapp',
                'metadata' => [
                    'display_phone_number' => '+1555DEMOWA',
                    'phone_number_id' => 'DEMO_PHONE_ID_111222333',
                ],
                'contacts' => [[
                    'profile' => ['name' => 'Test User'],
                    'wa_id' => '8801700000001',
                ]],
                'messages' => [[
                    'from' => '8801700000001',
                    'id' => 'wamid.DEMO_' . time(),
                    'timestamp' => time(),
                    'type' => 'text',
                    'text' => ['body' => 'Hello! I need help with my order.'],
                ]],
            ],
        ]],
    ]],
]);
```

---

## 2. WhatsApp — Incoming Image Message

```php
app(\App\Modules\Whatsapp\Services\WhatsappDriver::class)->processWebhookPayload([
    'object' => 'whatsapp_business_account',
    'entry' => [[
        'id' => 'DEMO_WABA_123456789',
        'changes' => [[
            'field' => 'messages',
            'value' => [
                'messaging_product' => 'whatsapp',
                'metadata' => [
                    'display_phone_number' => '+1555DEMOWA',
                    'phone_number_id' => 'DEMO_PHONE_ID_111222333',
                ],
                'contacts' => [[
                    'profile' => ['name' => 'Test User'],
                    'wa_id' => '8801700000002',
                ]],
                'messages' => [[
                    'from' => '8801700000002',
                    'id' => 'wamid.DEMO_IMG_' . time(),
                    'timestamp' => time(),
                    'type' => 'image',
                    'image' => [
                        'id' => 'DEMO_MEDIA_ID_123',
                        'mime_type' => 'image/jpeg',
                        'sha256' => 'abc123',
                        'caption' => 'Screenshot of the error',
                    ],
                ]],
            ],
        ]],
    ]],
]);
```

---

## 3. WhatsApp — Status Update (delivered → read)

```php
app(\App\Modules\Whatsapp\Services\WhatsappDriver::class)->processWebhookPayload([
    'object' => 'whatsapp_business_account',
    'entry' => [[
        'id' => 'DEMO_WABA_123456789',
        'changes' => [[
            'field' => 'messages',
            'value' => [
                'messaging_product' => 'whatsapp',
                'metadata' => [
                    'display_phone_number' => '+1555DEMOWA',
                    'phone_number_id' => 'DEMO_PHONE_ID_111222333',
                ],
                'statuses' => [[
                    'id' => 'wamid.REPLACE_WITH_REAL_PROVIDER_MESSAGE_ID',
                    'status' => 'read',
                    'timestamp' => time(),
                    'recipient_id' => '8801700000001',
                ]],
            ],
        ]],
    ]],
]);
```

> Replace `wamid.REPLACE_WITH_REAL_PROVIDER_MESSAGE_ID` with a real `provider_message_id` from your `messages` table.

---

## 4. Instagram — Incoming Text Message

```php
app(\App\Modules\Inbox\Services\InstagramDriver::class)->processWebhookPayload([
    'object' => 'instagram',
    'entry' => [[
        'id' => 'DEMO_IG_PAGE_ID_444555666',
        'messaging' => [[
            'sender' => ['id' => 'IG_SENDER_PSID_111'],
            'recipient' => ['id' => 'DEMO_IG_ACCOUNT_ID_999888777'],
            'timestamp' => time() * 1000,
            'message' => [
                'mid' => 'ig_mid_DEMO_' . time(),
                'text' => 'Hi, I saw your Instagram post and want to know more!',
            ],
        ]],
    ]],
]);
```

---

## 5. Instagram — Incoming Image Attachment

```php
app(\App\Modules\Inbox\Services\InstagramDriver::class)->processWebhookPayload([
    'object' => 'instagram',
    'entry' => [[
        'id' => 'DEMO_IG_PAGE_ID_444555666',
        'messaging' => [[
            'sender' => ['id' => 'IG_SENDER_PSID_222'],
            'recipient' => ['id' => 'DEMO_IG_ACCOUNT_ID_999888777'],
            'timestamp' => time() * 1000,
            'message' => [
                'mid' => 'ig_mid_DEMO_IMG_' . time(),
                'text' => '',
                'attachments' => [[
                    'type' => 'image',
                    'payload' => ['url' => 'https://example.com/demo-image.jpg'],
                ]],
            ],
        ]],
    ]],
]);
```

---

## 6. Messenger — Incoming Text Message

```php
app(\App\Modules\Inbox\Services\MessengerDriver::class)->processWebhookPayload([
    'object' => 'page',
    'entry' => [[
        'id' => 'DEMO_MESSENGER_PAGE_ID_777888999',
        'messaging' => [[
            'sender' => ['id' => 'MESSENGER_SENDER_PSID_333'],
            'recipient' => ['id' => 'DEMO_MESSENGER_PAGE_ID_777888999'],
            'timestamp' => time() * 1000,
            'message' => [
                'mid' => 'msg_DEMO_' . time(),
                'text' => 'Hey! Can you help me with pricing info?',
            ],
        ]],
    ]],
]);
```

---

## 7. Messenger — Quick Reply Button Tap

```php
app(\App\Modules\Inbox\Services\MessengerDriver::class)->processWebhookPayload([
    'object' => 'page',
    'entry' => [[
        'id' => 'DEMO_MESSENGER_PAGE_ID_777888999',
        'messaging' => [[
            'sender' => ['id' => 'MESSENGER_SENDER_PSID_444'],
            'recipient' => ['id' => 'DEMO_MESSENGER_PAGE_ID_777888999'],
            'timestamp' => time() * 1000,
            'message' => [
                'mid' => 'msg_DEMO_QR_' . time(),
                'text' => 'Yes',
                'quick_reply' => ['payload' => 'YES_PAYLOAD'],
            ],
        ]],
    ]],
]);
```

---

## 8. Outgoing Reply (any channel) — from the DB side

Send an outgoing message from an existing conversation (simulates the agent reply without hitting the real API):

```php
// Get the first open conversation
$conv = \App\Modules\Shared\Models\Conversation::where('status', 'open')->first();

// Create the outbound message row
$msg = \App\Modules\Shared\Models\Message::create([
    'conversation_id' => $conv->id,
    'direction' => 'out',
    'channel' => $conv->channelAccount->channel,
    'type' => 'text',
    'body' => 'Hello! Thanks for reaching out. How can we help you today?',
    'status' => 'sent',
    'sent_by' => 'human',
    'sent_at' => now(),
    'payload' => [],
]);

// Fire the real-time broadcast so the UI updates
\App\Events\MessageReceived::dispatch($msg);

echo "Outbound message created: ID {$msg->id} on conversation {$conv->uuid}";
```

---

## 9. Check What Got Created

```php
// List all channel accounts
\App\Modules\Shared\Models\ChannelAccount::all(['id','workspace_id','channel','display_name','phone_number_id','status'])->toArray();

// List all conversations
\App\Modules\Shared\Models\Conversation::with('contact','channelAccount')->get()->map(fn($c) => [
    'id' => $c->id,
    'uuid' => $c->uuid,
    'channel' => $c->channelAccount?->channel,
    'contact' => $c->contact?->first_name . ' ' . $c->contact?->last_name,
    'status' => $c->status,
    'unread' => $c->unread_count,
    'last_msg' => $c->last_message_at,
])->toArray();

// List all messages for a conversation UUID
$uuid = 'PASTE_CONVERSATION_UUID_HERE';
\App\Modules\Shared\Models\Conversation::where('uuid', $uuid)->first()
    ->messages()->orderBy('sent_at')->get(['id','direction','channel','type','body','status','sent_at'])->toArray();
```

---

## 10. Reset / Clean Up Demo Data

```php
// Delete all messages and conversations created during testing
\App\Modules\Shared\Models\Message::whereIn(
    'conversation_id',
    \App\Modules\Shared\Models\Conversation::where('workspace_id', 1)->pluck('id')
)->delete();

\App\Modules\Shared\Models\Conversation::where('workspace_id', 1)->delete();

echo "All test conversations and messages deleted.";
```

---

## Tips

- Each test uses a unique `mid`/`id` via `time()` to bypass idempotency checks. If you re-run the same command within the same second, the duplicate will be silently skipped.
- Queue worker is **not needed** — `processWebhookPayload()` is called directly (synchronous).
- To test the full webhook pipeline including the queue, use the curl approach instead (see README).
- The `send()` method on each driver makes real HTTP calls to Meta's API — use the DB-side approach (test #8) to simulate outgoing without hitting the API.
