@php $phone = is_array($health['phone'] ?? null) ? $health['phone'] : []; $wabaN = is_array($health['waba'] ?? null) ? $health['waba'] : []; $token = is_array($health['token'] ?? null) ? $health['token'] : []; $webhook = is_array($health['webhook'] ?? null) ? $health['webhook'] : []; $tpls = is_array($health['templates'] ?? null) ? $health['templates'] : []; $issues = (array) ($health['issues'] ?? []); // Show ONLY hard blockers (red / critical). Amber warnings + neutral notices // are noise for the operator, so they're hidden from the Issues list. $issues = array_values(array_filter($issues, fn ($i) => (($i['severity'] ?? '') === 'critical'))); $errors = (array) ($health['errors'] ?? []); $ids = (array) ($health['ids'] ?? []); $overall = (string) ($health['overall'] ?? 'healthy'); $title = $phone['verified_name'] ?? ($waba->display_label ?: ($waba->phone_number ?: 'WABA number')); // Overall status — drives the first KPI card + the eyebrow live-dot. $status = match ($overall) { 'blocked' => ['label' => __('Blocked'), 'sub' => __('Sending is blocked or restricted'), 'dot' => 'bg-accent-coral', 'text' => 'text-accent-coral'], 'attention' => ['label' => __('Attention'), 'sub' => __('Some checks need a look'), 'dot' => 'bg-accent-amber', 'text' => 'text-accent-amber'], default => ['label' => __('Healthy'), 'sub' => __('This number can send messages'), 'dot' => 'bg-wa-green', 'text' => 'text-wa-deep'], }; $sevStyle = fn ($s) => match ($s) { 'critical' => ['bg-accent-coral/10 border-accent-coral/40', 'bg-accent-coral', 'text-accent-coral'], 'warning' => ['bg-accent-amber/10 border-accent-amber/40', 'bg-accent-amber', 'text-accent-amber'], default => ['bg-paper-100 border-paper-200', 'bg-ink-400', 'text-ink-600'], }; // Generic value pill: good / warn / bad / neutral. $tone = function ($val, array $good = [], array $warn = []) { $u = strtoupper((string) $val); if ($u === '') return 'bg-paper-100 text-ink-600 border-paper-200'; if (in_array($u, array_map('strtoupper', $good), true)) return 'bg-wa-mint text-wa-deep border-wa-green/40'; if (in_array($u, array_map('strtoupper', $warn), true)) return 'bg-accent-amber/10 text-accent-amber border-accent-amber/40'; return 'bg-accent-coral/10 text-accent-coral border-accent-coral/40'; }; $quality = strtoupper((string) ($phone['quality_rating'] ?? '')); $qMeta = match ($quality) { 'GREEN' => ['label' => __('Green'), 'text' => 'text-wa-deep', 'dot' => 'bg-wa-green'], 'YELLOW' => ['label' => __('Yellow'), 'text' => 'text-accent-amber', 'dot' => 'bg-accent-amber'], 'RED' => ['label' => __('Red'), 'text' => 'text-accent-coral', 'dot' => 'bg-accent-coral'], default => ['label' => __('Unrated'),'text' => 'text-ink-500', 'dot' => 'bg-ink-300'], }; $tierRaw = (string) ($phone['messaging_limit_tier'] ?? ''); $tier = $tierRaw ? str_replace(['TIER_', 'UNLIMITED'], ['', '∞'], $tierRaw) : '—'; $thr = $phone['throughput']['level'] ?? null; $tplTotal = (int) ($tpls['total'] ?? 0); $tplApproved = (int) ($tpls['by_status']['APPROVED'] ?? 0); $fetched = \Illuminate\Support\Carbon::parse($health['fetched_at'] ?? now()); // KPI card shell — identical tokens to the operator dashboard stat cards. $kpi = 'col-span-12 md:col-span-6 xl:col-span-3 bg-paper-0 hairline border border-paper-200 rounded-2xl p-5 shadow-card relative overflow-hidden'; @endphp {{-- ========== PAGE HEADER (operator-dashboard style) ========== --}}
{{ __('Devices') }} {{ __('Meta (WABA) health') }} {{ __('checked') }} {{ $fetched->diffForHumans() }}

{{ $title }}

{{ $phone['display_phone_number'] ?? ($waba->phone_number ?: '+— unknown') }} · {{ $status['sub'] }} · {{ __('Graph') }} {{ $health['version'] ?? 'v23.0' }}

{{ __('Re-check') }} @if (!empty($ids['waba_id'])) {{ __('Open in Meta') }} @endif
{{-- ========== VERIFIED BADGE (BLUE TICK) + USERNAME ========== --}} @php $isOba = filter_var($phone['is_official_business_account'] ?? false, FILTER_VALIDATE_BOOLEAN); $bizVerif = strtolower((string) ($wabaN['business_verification_status'] ?? '')); $nameStatus = strtoupper((string) ($phone['name_status'] ?? '')); $pinOn = filter_var($phone['is_pin_enabled'] ?? false, FILTER_VALIDATE_BOOLEAN); // Username we claimed via the Cloud API is stored on the config's meta_json // (falls back to whatever the phone-number node reports). $wcMeta = is_array($waba->meta_json) ? $waba->meta_json : []; $waUsername = (string) ($wcMeta['wa_username'] ?? ($phone['username'] ?? '')); $waUsernameStatus = strtolower((string) ($wcMeta['wa_username_status'] ?? 'reserved')); $unApproved = $waUsernameStatus === 'approved'; $vErrors = session('errors') ? session('errors')->getBag('default') : null; // Readable prerequisites. Meta grants the badge on brand notability OR a // Meta Verified subscription — those two can't be read via the API, so // they're shown as static guidance below. $checks = [ ['ok' => $bizVerif === 'verified', 'label' => __('Business verified with Meta')], ['ok' => $nameStatus === 'APPROVED', 'label' => __('Display name approved')], ['ok' => $quality !== 'RED', 'label' => __('Message quality not low')], ['ok' => $pinOn, 'label' => __('Two-step verification (PIN) on')], ]; $metaMgr = !empty($ids['waba_id']) ? 'https://business.facebook.com/wa/manage/phone-numbers/?waba_id=' . $ids['waba_id'] : 'https://business.facebook.com/wa/manage/'; @endphp
{{-- Verified badge (blue tick) --}}
{{ __('Verified badge (blue tick)') }}
{{ $isOba ? __('Active — your number shows the verified badge') : __('Not verified yet') }}
    @foreach ($checks as $c)
  • @if ($c['ok'])@else@endif {{ $c['label'] }}
  • @endforeach
  • {{ __('Brand notability (press coverage) OR a Meta Verified subscription') }}
{{ __('Meta grants the badge manually — there is no in-app apply. Submit the request from WhatsApp Manager:') }}
{{ __('Apply on Meta (WhatsApp Manager)') }}
{{-- Business username (@handle) --}}
{{ __('Business username') }}
{{ __('A public @handle so customers can find you without your number. Claiming does not hide your phone number.') }}
@if (session('status'))
{{ session('status') }}
@endif @if ($vErrors && $vErrors->has('username'))
{{ $vErrors->first('username') }}
@endif @if ($waUsername !== '')
{{ '@' . $waUsername }} {{ $unApproved ? __('Approved — visible to customers') : __('Reserved — activates when WhatsApp rolls out usernames') }}
@csrf @method('DELETE')
@else
@csrf
{{ '@' }}
@if (!empty($usernameSuggestions ?? []))
{{ __('Suggested') }}: @foreach ($usernameSuggestions as $sug)
@csrf
@endforeach
@endif
{{ __('3–24 characters · lowercase letters, numbers, . and _ · must contain a letter') }}
@endif
{{-- ========== WHATSAPP CALLING ========== --}} @php $callingOn = (bool) ($waba->calling_enabled ?? false); @endphp
{{ __('WhatsApp calling') }}
{{ $callingOn ? __('Enabled — the green Call button now shows on this number\'s chats in the inbox.') : __('Off — turn it on so the Call button appears on chats with this number.') }}
{{-- ========== KPI STATS ROW ========== --}}
{{-- Overall status --}}
{{ __('Overall status') }}
{{ $status['label'] }}
{{ $status['sub'] }}
{{-- Quality rating --}}
{{ __('Quality rating') }}
{{ $qMeta['label'] }}
{{ __('Meta message quality (last rolling window)') }}
{{-- Messaging limit --}}
{{ __('Messaging limit') }}
{{ $tier }} @if ($tier !== '—')/ 24h@endif
{{ __('Unique customers you can start per day') }}{{ $thr ? ' · ' . $thr : '' }}
{{-- Free conversations this month — Meta's free monthly allowance --}} @php $conv = $health['conversations'] ?? null; @endphp @if ($conv)
{{ __('Free this month') }}
{{ number_format($conv['free_left']) }} / {{ number_format($conv['free_total']) }}
{{ __('Free service conversations left') }} · {{ number_format($conv['free_used']) }} {{ __('used') }}@if (($conv['paid'] ?? 0) > 0) · {{ number_format($conv['paid']) }} {{ __('paid') }}@endif
@endif {{-- Templates --}}
{{ __('Message templates') }}
{{ $tplTotal }}
{{ $tplApproved }} {{ __('approved') }} @if ($tplTotal - $tplApproved > 0) {{ $tplTotal - $tplApproved }} {{ __('other') }} @endif
{{-- Issues / blocks --}} @if (!empty($issues))
{{ __('Issues & blocks') }} ({{ count($issues) }})
@foreach ($issues as $iss) @php [$ibg, $idot, $itext] = $sevStyle($iss['severity']); @endphp
{{ $iss['title'] }} {{ $iss['area'] }} @if (!empty($iss['code'])) #{{ $iss['code'] }} @endif
@if (!empty($iss['detail']))
{{ $iss['detail'] }}
@endif @if (!empty($iss['solution']))
{{ __('Fix') }}: {{ $iss['solution'] }}
@endif
@endforeach
@endif {{-- Detail grid --}}
{{-- Number status --}}
{{ __('Phone number') }}
{{ __('Verified name') }}
{{ $phone['verified_name'] ?? '—' }}
{{ __('Connection') }}
{{ $phone['status'] ?? '—' }}
{{ __('Quality rating') }}
{{ $quality ?: __('Unrated') }}
{{ __('Messaging limit') }}
{{ $tier !== '—' ? $tier . ' / 24h' : '—' }}
{{ __('Throughput') }}
{{ $thr ?: '—' }}
{{ __('Number verification') }}
{{ $phone['code_verification_status'] ?? '—' }}
{{ __('Display name status') }}
{{ $phone['name_status'] ?? '—' }}
{{ __('Account mode') }}
{{ $phone['account_mode'] ?? '—' }}
{{ __('Platform') }}
{{ $phone['platform_type'] ?? '—' }}
{{ __('Official business') }}
{{ ($phone['is_official_business_account'] ?? false) ? __('Yes (blue tick)') : __('No') }}
{{ __('Two-step PIN') }}
{{ ($phone['is_pin_enabled'] ?? false) ? __('Enabled') : __('Off') }}
{{-- WABA account --}}
{{ __('Business account') }}
{{ __('WABA name') }}
{{ $wabaN['name'] ?? '—' }}
{{ __('Account review') }}
{{ $wabaN['account_review_status'] ?? '—' }}
{{ __('Business verification') }}
{{ $wabaN['business_verification_status'] ?? '—' }}
{{ __('Owner business') }}
{{ $wabaN['owner_business_info']['name'] ?? '—' }}
{{ __('Currency') }}
{{ $wabaN['currency'] ?? '—' }}
{{ __('Timezone') }}
{{ $wabaN['timezone_id'] ?? '—' }}
{{ __('Country') }}
{{ $wabaN['country'] ?? '—' }}
{{ __('Ownership') }}
{{ $wabaN['ownership_type'] ?? '—' }}
{{-- Permissions / token --}}
{{ __('Access & permissions') }}
{{ __('Token valid') }}
{{ ($token['is_valid'] ?? false) ? __('Valid') : __('Invalid') }}
{{ __('Token type') }}
{{ $token['type'] ?? '—' }}
{{ __('App') }}
{{ $token['application'] ?? ($token['app_id'] ?? '—') }}
{{ __('Expires') }}
@if ($token['expires_never'] ?? false) {{ __('Never (permanent)') }} @elseif (!empty($token['expires_at'])) {{ \Illuminate\Support\Carbon::createFromTimestamp($token['expires_at'])->format('M j, Y') }} @else — @endif
{{-- The number's actual Meta access token. Masked by default; a native
reveals the full value (no JS). The reveal block is `select-all` so one click + Ctrl+C copies it. --}} @php $accessToken = (string) ($accessToken ?? ''); $atMasked = $accessToken === '' ? '' : (strlen($accessToken) > 22 ? substr($accessToken, 0, 12) . '…' . substr($accessToken, -6) : $accessToken); @endphp
{{ __('Access token') }}
@if ($accessToken !== '') {{ strlen($accessToken) }} {{ __('chars') }} @endif
@if ($accessToken !== '')
{{ $atMasked }}
{{ __('Reveal full token') }}
{{ $accessToken }}

{{ __('Click the token to select it, then copy. Treat it like a password — anyone with it can send from this number.') }}

@else
{{ __('No token stored — template sync + sending will fail. Paste this number\'s access token below.') }}
@endif @if ($accessToken !== '') {{-- One-click FIX: exchange the current token with Meta (fb_exchange_token) for a fresh long-lived one and save it IN PLACE — repairs an invalid/expiring token WITHOUT reconnecting the device. Sending keeps working. --}}
@csrf {{ __('Renews with Meta in place — no reconnect needed.') }} @if ($vErrors && $vErrors->has('token'))
{{ $vErrors->first('token') }}
@endif
@endif {{-- Paste / replace the access token directly — a fast fix when a connect left it empty, without re-running embedded signup. Verified against Meta before it's saved. Opens automatically when there's no token yet. --}}
{{ $accessToken === '' ? __('Add access token') : __('Replace access token') }}
@csrf @if ($vErrors && $vErrors->has('access_token'))
{{ $vErrors->first('access_token') }}
@endif
{{ __('Checked against Meta before saving.') }}
{{ __('Granted permissions') }}
@forelse (($token['scopes'] ?? []) as $sc) {{ $sc }} @empty {{ __('No permissions reported') }} @endforelse
{{-- Webhook + templates + IDs --}}
{{ __('Delivery & content') }}
{{ __('Webhook subscribed') }}
@php $sub = $webhook['subscribed'] ?? null; @endphp {{ $sub === true ? __('Yes') : ($sub === false ? __('No') : '—') }}
{{ __('Inbound to this platform') }}
@include('user.devices._inbound_badge', ['wired' => (is_array($waba->meta_json ?? null) ? ($waba->meta_json['inbound_wired'] ?? null) : null)])
{{ __('Templates total') }}
{{ $tpls['total'] ?? '—' }}
{{-- Fix inbound = re-apply the webhook override + verify that incoming messages for this number are routed to this platform. --}}
@csrf
{{-- Live inbound-webhook check — asks Meta which apps this WABA is subscribed to, the EXACT delivery (override) URL it holds vs THIS server, and whether an App Secret is stored (the signature-reject cause of "subscribed but no inbound"). Renders inline; no reload. --}} {{-- OWN-APP WEBHOOK SETUP — shown whenever inbound is NOT CONFIRMED wired (false OR null/unverified). A number connected with the customer's OWN Meta app (not a Tech Provider) can't be override- routed to us — Meta answers the override with (#100) and the plain-subscribe falls back to the app's DEFAULT callback, which the Graph API never exposes, so verifyInboundWired() can only ever return null for it (never a confirming true). So "Fix inbound" alone can't confirm it; the customer must point THEIR app's webhook here, and the badge flips to wired only when a real inbound message actually arrives (stamped by WaWebhookController). These are the exact values to paste, shown persistently so they're always copyable. --}} @php $__inboundWired = is_array($waba->meta_json ?? null) ? ($waba->meta_json['inbound_wired'] ?? null) : null; $__cbUrl = url('/webhooks/whatsapp/inbound'); $__verifyTok = (string) \App\Models\SystemSetting::get('waba_webhook_verify_token', ''); @endphp @if ($__inboundWired !== true)
{{ __('Using your own Meta app? Point its webhook here') }}

{{ __('This number is subscribed to your own Meta app, so Meta won\'t auto-route its messages to us. In your Meta App Dashboard → WhatsApp → Configuration, set the Callback URL and Verify token below, subscribe the "messages" field, then click "Fix inbound" above. (Connecting via Embedded Signup avoids all this.)') }}

{{ __('Callback URL') }}
{{ $__cbUrl }}
@if ($__verifyTok !== '')
{{ __('Verify token') }}
{{ $__verifyTok }}
@endif
@endif {{-- Sync chat history = manual fallback for the Coexistence 6-month history import. Meta has no history-PULL API — it only PUSHES history once after a Coexistence onboard. Re-subscribing is the only lever that can prompt that push again. --}}
@csrf

{{ __('Only Coexistence numbers have history to sync — Meta pushes ~6 months once after connecting. A standard Cloud API number has none.') }}

@if (!empty($tpls['by_status']))
@foreach ($tpls['by_status'] as $st => $n) {{ $st }} · {{ $n }} @endforeach
@endif
{{ __('Phone ID') }}
{{ $ids['phone_number_id'] ?: '—' }}
{{ __('WABA ID') }}
{{ $ids['waba_id'] ?: '—' }}
{{ __('Business ID') }}
{{ $ids['business_id'] ?: '—' }}
{{-- Fetch errors (a Graph node we couldn't read) --}} @if (!empty($errors))
{{ __('Could not read some data from Meta') }}
    @foreach ($errors as $e)
  • {{ $e }}
  • @endforeach
@endif