@php
    $sections = $sections ?? [];
    $tableLimit = $tableLimit ?? 20;
    $sliceRows = fn ($rows) => array_slice($rows ?? [], 0, $tableLimit);
@endphp

@if(($sections['channel_comparison'] ?? true))
    <div class="section">
        <h2>Kanal Karşılaştırması</h2>
        <table class="table" border="1"><thead><tr><th>Kanal</th><th>Harcama</th><th>Tıklama/Oturum</th><th>Dönüşüm</th><th>Ek Metrik</th></tr></thead><tbody>
            @if(!empty($dataset['google_ads']))<tr><td>Google Ads</td><td>{{ $fmt($dataset['google_ads']['totals']['cost'] ?? 0) }}</td><td>{{ $int($dataset['google_ads']['totals']['clicks'] ?? 0) }}</td><td>{{ $fmt($dataset['google_ads']['totals']['conversions'] ?? 0) }}</td><td>CTR: {{ $fmt($dataset['google_ads']['totals']['ctr'] ?? 0) }}%</td></tr>@endif
            @if(!empty($dataset['meta_ads']))<tr><td>Meta Ads</td><td>{{ $fmt($dataset['meta_ads']['totals']['spend'] ?? 0) }}</td><td>{{ $int($dataset['meta_ads']['totals']['clicks'] ?? 0) }}</td><td>{{ $fmt($dataset['meta_ads']['totals']['conversions'] ?? 0) }}</td><td>Lead: {{ $fmt($dataset['meta_ads']['totals']['leads'] ?? 0) }}</td></tr>@endif
            @if(!empty($dataset['search_console']))<tr><td>Search Console</td><td>-</td><td>{{ $int($dataset['search_console']['totals']['clicks'] ?? 0) }}</td><td>-</td><td>Pozisyon: {{ $fmt($dataset['search_console']['totals']['position'] ?? 0) }}</td></tr>@endif
            @if(!empty($dataset['ga4']))<tr><td>GA4</td><td>{{ $fmt($dataset['ga4']['totals']['total_revenue'] ?? 0) }}</td><td>{{ $int($dataset['ga4']['totals']['sessions'] ?? 0) }}</td><td>{{ $fmt($dataset['ga4']['totals']['conversions'] ?? 0) }}</td><td>Kullanıcı: {{ $int($dataset['ga4']['totals']['total_users'] ?? 0) }}</td></tr>@endif
        </tbody></table>
    </div>
@endif

@if(!empty($dataset['google_ads']) && ($sections['google_campaigns'] ?? true))
    <div class="section"><h2>Google Ads Kampanyalar</h2><table class="table" border="1"><thead><tr><th>Kampanya</th><th>Gösterim</th><th>Tıklama</th><th>Harcama</th><th>Dönüşüm</th><th>CPC</th></tr></thead><tbody>@forelse($sliceRows($dataset['google_ads']['campaigns'] ?? []) as $row)<tr><td>{{ $row['campaign_name'] ?? '-' }}</td><td>{{ $int($row['impressions'] ?? 0) }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['cost'] ?? 0) }}</td><td>{{ $fmt($row['conversions'] ?? 0) }}</td><td>{{ $fmt(($row['clicks'] ?? 0) > 0 ? (($row['cost'] ?? 0) / ($row['clicks'] ?? 1)) : 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">Google Ads kampanya verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['google_ads']) && ($sections['google_keywords'] ?? false))
    <div class="section"><h2>Google Ads Anahtar Kelimeler</h2><table class="table" border="1"><thead><tr><th>Kelime</th><th>Kampanya</th><th>Eşleme</th><th>Tıklama</th><th>Harcama</th><th>Dönüşüm</th></tr></thead><tbody>@forelse($sliceRows($dataset['google_ads']['keywords'] ?? []) as $row)<tr><td>{{ $row['keyword_text'] ?? '-' }}</td><td>{{ $row['campaign_name'] ?? '-' }}</td><td>{{ $row['match_type'] ?? '-' }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['cost'] ?? 0) }}</td><td>{{ $fmt($row['conversions'] ?? 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">Anahtar kelime verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['google_ads']) && ($sections['google_search_terms'] ?? false))
    <div class="section"><h2>Google Ads Arama Terimleri</h2><table class="table" border="1"><thead><tr><th>Arama Terimi</th><th>Kampanya</th><th>Tıklama</th><th>Harcama</th><th>Dönüşüm</th></tr></thead><tbody>@forelse($sliceRows($dataset['google_ads']['search_terms'] ?? []) as $row)<tr><td>{{ $row['search_term'] ?? '-' }}</td><td>{{ $row['campaign_name'] ?? '-' }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['cost'] ?? 0) }}</td><td>{{ $fmt($row['conversions'] ?? 0) }}</td></tr>@empty<tr><td colspan="5" class="muted">Arama terimi verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['meta_ads']) && ($sections['meta_campaigns'] ?? true))
    <div class="section"><h2>Meta Ads Kampanyalar</h2><table class="table" border="1"><thead><tr><th>Kampanya</th><th>Gösterim</th><th>Erişim</th><th>Tıklama</th><th>Harcama</th><th>Lead</th></tr></thead><tbody>@forelse($sliceRows($dataset['meta_ads']['campaigns'] ?? []) as $row)<tr><td>{{ $row['campaign_name'] ?? '-' }}</td><td>{{ $int($row['impressions'] ?? 0) }}</td><td>{{ $int($row['reach'] ?? 0) }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['spend'] ?? 0) }}</td><td>{{ $fmt($row['leads'] ?? 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">Meta Ads kampanya verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['meta_ads']) && ($sections['meta_adsets'] ?? false))
    <div class="section"><h2>Meta Ads Reklam Setleri</h2><table class="table" border="1"><thead><tr><th>Reklam Seti</th><th>Kampanya</th><th>Gösterim</th><th>Tıklama</th><th>Harcama</th><th>Lead</th></tr></thead><tbody>@forelse($sliceRows($dataset['meta_ads']['adsets'] ?? []) as $row)<tr><td>{{ $row['adset_name'] ?? '-' }}</td><td>{{ $row['campaign_name'] ?? '-' }}</td><td>{{ $int($row['impressions'] ?? 0) }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['spend'] ?? 0) }}</td><td>{{ $fmt($row['leads'] ?? 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">Reklam seti verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['meta_ads']) && ($sections['meta_ads'] ?? false))
    <div class="section"><h2>Meta Ads Reklamlar</h2><table class="table" border="1"><thead><tr><th>Reklam</th><th>Reklam Seti</th><th>Tıklama</th><th>Harcama</th><th>Lead</th><th>Dönüşüm</th></tr></thead><tbody>@forelse($sliceRows($dataset['meta_ads']['ads'] ?? []) as $row)<tr><td>{{ $row['ad_name'] ?? '-' }}</td><td>{{ $row['adset_name'] ?? '-' }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $fmt($row['spend'] ?? 0) }}</td><td>{{ $fmt($row['leads'] ?? 0) }}</td><td>{{ $fmt($row['conversions'] ?? 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">Reklam verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['search_console']) && ($sections['gsc_queries'] ?? true))
    <div class="section"><h2>Search Console Sorgular</h2><table class="table" border="1"><thead><tr><th>Sorgu</th><th>Tıklama</th><th>Gösterim</th><th>CTR</th><th>Pozisyon</th></tr></thead><tbody>@forelse($sliceRows($dataset['search_console']['queries'] ?? []) as $row)<tr><td>{{ $row['query_text'] ?? '-' }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $int($row['impressions'] ?? 0) }}</td><td>{{ $fmt(($row['ctr'] ?? 0) * 100) }}%</td><td>{{ $fmt($row['position'] ?? 0) }}</td></tr>@empty<tr><td colspan="5" class="muted">Search Console sorgu verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['search_console']) && ($sections['gsc_pages'] ?? false))
    <div class="section"><h2>Search Console Sayfalar</h2><table class="table" border="1"><thead><tr><th>Sayfa</th><th>Tıklama</th><th>Gösterim</th><th>CTR</th><th>Pozisyon</th></tr></thead><tbody>@forelse($sliceRows($dataset['search_console']['pages'] ?? []) as $row)<tr><td>{{ $row['page_url'] ?? '-' }}</td><td>{{ $int($row['clicks'] ?? 0) }}</td><td>{{ $int($row['impressions'] ?? 0) }}</td><td>{{ $fmt(($row['ctr'] ?? 0) * 100) }}%</td><td>{{ $fmt($row['position'] ?? 0) }}</td></tr>@empty<tr><td colspan="5" class="muted">Search Console sayfa verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif

@if(!empty($dataset['ga4']) && ($sections['ga4_channels'] ?? true))
    <div class="section"><h2>GA4 Trafik</h2><table class="table" border="1"><thead><tr><th>Kırılım</th><th>Oturum</th><th>Kullanıcı</th><th>Sayfa Görüntüleme</th><th>Dönüşüm</th><th>Gelir</th></tr></thead><tbody>@forelse($sliceRows($dataset['ga4']['channels'] ?? []) as $row)<tr><td>{{ $row['dimension_value'] ?? '-' }}</td><td>{{ $int($row['sessions'] ?? 0) }}</td><td>{{ $int($row['total_users'] ?? 0) }}</td><td>{{ $int($row['screen_page_views'] ?? 0) }}</td><td>{{ $fmt($row['conversions'] ?? 0) }}</td><td>{{ $fmt($row['total_revenue'] ?? 0) }}</td></tr>@empty<tr><td colspan="6" class="muted">GA4 trafik verisi yok.</td></tr>@endforelse</tbody></table></div>
@endif
