<?php

use Illuminate\Support\Facades\Route;
use Modules\WhatsApp\Http\Controllers\Api\AgentController;
use Modules\WhatsApp\Http\Controllers\WebhookController;

/*
| The RouteServiceProvider applies the `api` prefix + `api` middleware, so these
| resolve under /api/whatsapp/...
*/
Route::prefix('whatsapp')->group(function () {

    /*
    | Meta Cloud API webhook (inbound messages + delivery/read status callbacks).
    | GET  = Meta verification handshake (hub.challenge).
    | POST = event payloads, signed with X-Hub-Signature-256.
    */
    Route::get('webhook',  [WebhookController::class, 'verify']);
    Route::post('webhook', [WebhookController::class, 'handle']);

    /*
    | WhatsApp-Web bridge API. The standalone Node service (one process per QR
    | channel) authenticates with the channel's per-channel bearer token
    | (wa_channels.api_token), exactly like the LabCore LabBridge agent.
    */
    Route::prefix('agent')->group(function () {
        Route::post('register',   [AgentController::class, 'register']);
        Route::post('heartbeat',  [AgentController::class, 'heartbeat']);
        Route::post('qr',         [AgentController::class, 'qr']);          // bridge pushes a fresh QR string
        Route::post('state',      [AgentController::class, 'state']);       // connected | disconnected | scanning
        Route::get('outbox',      [AgentController::class, 'outbox']);      // bridge pulls queued messages to send
        Route::post('ack',        [AgentController::class, 'ack']);         // bridge reports send result/status
        Route::post('inbound',    [AgentController::class, 'inbound']);     // bridge forwards a received message
    });
});
