@php use Illuminate\Support\Carbon; $followersNum = is_null($followers) ? null : (int) $followers; $reach28 = $kpis['page_impressions_unique'] ?? null; $impr28 = $kpis['page_impressions'] ?? null; $eng28 = $kpis['page_post_engagements'] ?? null; $views28 = $kpis['page_views_total'] ?? null; $newFans28 = $kpis['page_fan_adds'] ?? null; $engRate = ($eng28 && $reach28) ? round($eng28 / max(1, $reach28) * 100, 1) : null; // KPI cards — live Graph on top row, local WaDesk analytics on the second. $cards = [ ['label' => __('Followers'), 'value' => $followersNum, 'sub' => __('total audience'), 'live' => true], ['label' => __('Reach · 28d'), 'value' => $reach28, 'sub' => __('people reached'), 'live' => true], ['label' => __('Impressions · 28d'), 'value' => $impr28, 'sub' => __('times shown'), 'live' => true], ['label' => __('Engagements · 28d'), 'value' => $eng28, 'sub' => __('interactions'), 'live' => true], ['label' => __('Conversations'), 'value' => $local['convos'], 'sub' => __(':n open', ['n' => $local['convos_open']]), 'live' => false], ['label' => __('Messages in · 28d'), 'value' => $local['msg_in'], 'sub' => __(':n sent back', ['n' => number_format($local['msg_out'])]), 'live' => false], ['label' => __('Posts published'), 'value' => $local['posts_published'], 'sub' => __(':n scheduled', ['n' => $local['posts_scheduled']]), 'live' => false], ['label' => __('Engagement rate'), 'value' => $engRate, 'suffix' => '%', 'sub' => __('eng ÷ reach'), 'live' => true], ]; $typeMeta = [ 'photo' => ['label' => __('Photo'), 'color' => '#1877F2'], 'multi_photo' => ['label' => __('Album'), 'color' => '#42A5F5'], 'link' => ['label' => __('Link'), 'color' => '#7E57C2'], 'video' => ['label' => __('Video'), 'color' => '#26A69A'], 'reel' => ['label' => __('Reel'), 'color' => '#EC407A'], ]; $contentTotal = array_sum($contentMix ?: []); $convoLabels = [ 'open' => ['label' => __('Open'), 'color' => 'bg-wa-green'], 'pending' => ['label' => __('Pending'), 'color' => 'bg-accent-amber'], 'resolved' => ['label' => __('Resolved'), 'color' => 'bg-wa-deep'], 'closed' => ['label' => __('Closed'), 'color' => 'bg-ink-400'], 'snoozed' => ['label' => __('Snoozed'), 'color' => 'bg-accent-coral'], ]; $convoTotal = array_sum($convoMix ?: []); @endphp
{{-- ===== Header ===== --}}
{{ __('Facebook') }} · {{ __('Analytics') }}

{{ __('Page') }} {{ __('insights') }}

{{ __('Reach and engagement (28-day, live from Facebook) blended with your Messenger activity and posts published from :brand.', ['brand' => brand_name()]) }}

@if ($pages->isEmpty())

{{ __('No Facebook Page connected yet.') }}

{{ __('Connect a Facebook account') }}
@else {{-- ===== Account identity + switcher (always visible) ===== --}}
@if (!empty($identity['avatar'])) @else {{ strtoupper(mb_substr($identity['name'] ?? 'F', 0, 1)) }} @endif
{{ $identity['name'] ?? $page->page_id }} {{ __('Connected') }}
{{ $identity['category'] ?? __('Facebook Page') }} @if (!empty($identity['username']))·{{ '@' . $identity['username'] }}@endif ·ID {{ \Illuminate\Support\Str::limit($identity['page_id'] ?? '', 10, '…') }}
{{-- Account switcher — dropdown when several Pages, static chip when one --}}
{{ __('Viewing account') }}
@if ($pages->count() > 1)
@else {{ $page->name ?: $page->page_id }} @endif
{{-- ===== KPI grid ===== --}}
@foreach ($cards as $c)
{{ $c['label'] }}
@if ($c['live']) @else @endif
{{ is_null($c['value']) ? '—' : number_format((float) $c['value']) . ($c['suffix'] ?? '') }}
{{ $c['sub'] }}
@endforeach
{{-- ===== Message activity chart ===== --}}
{{ __('Messenger activity · last 14 days') }}
{{ __('Received') }} {{ __('Sent') }}
{{-- ===== Content mix + Conversation status ===== --}}
{{-- Content mix --}}
{{ __('Content mix') }} · {{ __(':n posts', ['n' => (int) $contentTotal]) }}
@if ($contentTotal > 0) @foreach ($typeMeta as $key => $meta) @php $n = (int) ($contentMix[$key] ?? 0); $pct = $contentTotal ? round($n / $contentTotal * 100) : 0; @endphp @if ($n > 0)
{{ $meta['label'] }} {{ $n }} · {{ $pct }}%
@endif @endforeach @else
{{ __('No posts published from :brand yet.', ['brand' => brand_name()]) }} {{ __('Create your first post') }}
@endif
{{-- Conversation status --}}
{{ __('Conversations') }} · {{ __(':n total', ['n' => (int) $convoTotal]) }}
@if ($convoTotal > 0) @foreach ($convoLabels as $key => $meta) @php $n = (int) ($convoMix[$key] ?? 0); $pct = $convoTotal ? round($n / $convoTotal * 100) : 0; @endphp @if ($n > 0)
{{ $meta['label'] }} {{ $n }} · {{ $pct }}%
@endif @endforeach {{ __('Open Team Inbox') }} → @else
{{ __('No Facebook conversations yet. They appear here once people message your Page.') }}
@endif
{{-- ===== Recent posts performance ===== --}}
{{ __('Recent posts') }}
@forelse ($posts as $post)
@if ($post['picture']) @else @endif
{{ $post['message'] ?: __('(no caption)') }}
{{ $post['created'] ? Carbon::parse($post['created'])->diffForHumans() : '' }} @if (!empty($post['status'])){{ $post['status'] }}@endif
{{ number_format($post['stats']['impressions']) }}
{{ __('reach') }}
{{ number_format($post['stats']['engaged']) }}
{{ __('engaged') }}
@if ($post['permalink']) {{ __('View') }} @endif
@empty
{{ __('No posts yet, or insights are still warming up.') }}
@endforelse

{{ __('Live from Facebook — deprecated metrics may show as —.') }} {{ __('From :brand — Messenger + published posts.', ['brand' => brand_name()]) }}

@endif