<!DOCTYPE html>
<html>
<head>
    <title>Web Analytics Report</title>
    <style>
        body {
            font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
            color: #333333;
            margin: 0;
            padding: 10px;
            font-size: 13px;
            line-height: 1.5;
        }
        .header {
            border-bottom: 2px solid #2563eb;
            padding-bottom: 15px;
            margin-bottom: 25px;
        }
        .header-title {
            font-size: 24px;
            font-weight: bold;
            color: #1e293b;
            margin: 0 0 5px 0;
        }
        .header-subtitle {
            font-size: 12px;
            color: #64748b;
            margin: 0;
        }
        .date-range {
            font-size: 13px;
            font-weight: bold;
            color: #2563eb;
            margin-top: 5px;
        }
        .metrics-grid {
            width: 100%;
            margin-bottom: 30px;
            border-collapse: collapse;
        }
        .metric-card {
            background-color: #f8fafc;
            border: 1px solid #e2e8f0;
            border-radius: 8px;
            padding: 15px;
            text-align: center;
            width: 23%;
        }
        .metric-value {
            font-size: 20px;
            font-weight: bold;
            color: #0f172a;
            margin-bottom: 5px;
        }
        .metric-label {
            font-size: 10px;
            color: #64748b;
            text-transform: uppercase;
            letter-spacing: 0.5px;
        }
        .section-title {
            font-size: 16px;
            font-weight: bold;
            color: #1e293b;
            border-bottom: 1px solid #e2e8f0;
            padding-bottom: 5px;
            margin-top: 30px;
            margin-bottom: 15px;
        }
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-bottom: 25px;
        }
        .data-table th {
            background-color: #f1f5f9;
            color: #475569;
            font-weight: bold;
            font-size: 11px;
            text-transform: uppercase;
            border-bottom: 2px solid #cbd5e1;
            padding: 8px 10px;
            text-align: left;
        }
        .data-table td {
            padding: 8px 10px;
            border-bottom: 1px solid #e2e8f0;
            color: #334155;
        }
        .source-pill {
            display: inline-block;
            padding: 3px 8px;
            border-radius: 12px;
            font-size: 9px;
            font-weight: bold;
            text-transform: uppercase;
        }
        .source-direct { background-color: #e0f2fe; color: #0369a1; }
        .source-searchengine { background-color: #dcfce7; color: #15803d; }
        .source-social { background-color: #fef9c3; color: #a16207; }
        .source-referral { background-color: #f3e8ff; color: #7e22ce; }
        .source-video { background-color: #fee2e2; color: #b91c1c; }
        .source-ad { background-color: #ffedd5; color: #c2410c; }
        .footer {
            margin-top: 50px;
            border-top: 1px solid #e2e8f0;
            padding-top: 10px;
            text-align: center;
            font-size: 10px;
            color: #94a3b8;
        }
    </style>
</head>
<body>
    <div class="header">
        <table style="width: 100%;">
            <tr>
                <td>
                    <h1 class="header-title">American Plastics</h1>
                    <p class="header-subtitle">Web Analytics & Traffic Performance Report</p>
                </td>
                <td style="text-align: right; vertical-align: top;">
                    <div class="date-range">
                        {{ \Carbon\Carbon::parse($start_date)->format('M d, Y') }} - {{ \Carbon\Carbon::parse($end_date)->format('M d, Y') }}
                    </div>
                    <p style="font-size: 10px; color: #64748b; margin: 5px 0 0 0;">Generated: {{ now()->format('Y-m-d H:i:s') }}</p>
                </td>
            </tr>
        </table>
    </div>

    <!-- Core Metrics -->
    <table style="width: 100%; margin-bottom: 20px;">
        <tr>
            <td class="metric-card">
                <div class="metric-value">{{ number_format($visitors) }}</div>
                <div class="metric-label">Unique Visitors</div>
            </td>
            <td style="width: 2%;"></td>
            <td class="metric-card">
                <div class="metric-value">{{ number_format($page_views) }}</div>
                <div class="metric-label">Page Views</div>
            </td>
            <td style="width: 2%;"></td>
            <td class="metric-card">
                <div class="metric-value">{{ $bounce_rate }}%</div>
                <div class="metric-label">Bounce Rate</div>
            </td>
            <td style="width: 2%;"></td>
            <td class="metric-card">
                <div class="metric-value">{{ $avg_duration }}</div>
                <div class="metric-label">Avg. Duration</div>
            </td>
        </tr>
    </table>

    <table style="width: 100%; margin-top: 20px;">
        <tr>
            <!-- Traffic Sources -->
            <td style="width: 48%; vertical-align: top; padding-right: 15px;">
                <div class="section-title">Traffic Sources Breakdown</div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Source</th>
                            <th style="text-align: right;">Sessions</th>
                            <th style="text-align: right;">Share</th>
                        </tr>
                    </thead>
                    <tbody>
                        @php
                            $sourceLabels = [
                                'direct' => 'Direct Traffic',
                                'searchengine' => 'Search Engine',
                                'social' => 'Social Media',
                                'referral' => 'Referrals',
                                'video' => 'Video Traffic',
                                'ad' => 'Paid Advertising'
                            ];
                        @endphp
                        @forelse($traffic_sources as $src)
                            @php
                                $pct = $visitors > 0 ? round(($src->count / $visitors) * 100, 1) : 0;
                            @endphp
                            <tr>
                                <td>
                                    <span class="source-pill source-{{ $src->source }}">
                                        {{ $sourceLabels[$src->source] ?? ucfirst($src->source) }}
                                    </span>
                                </td>
                                <td style="text-align: right; font-weight: bold;">{{ number_format($src->count) }}</td>
                                <td style="text-align: right;">{{ $pct }}%</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="3" style="text-align: center; color: #94a3b8;">No traffic source data available</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </td>

            <!-- Top Referrers -->
            <td style="width: 48%; vertical-align: top; padding-left: 15px;">
                <div class="section-title">Top Referring Domains</div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Referring Website</th>
                            <th style="text-align: right;">Referrals</th>
                        </tr>
                    </thead>
                    <tbody>
                        @forelse($top_referrers as $ref)
                            <tr>
                                <td style="font-weight: bold; color: #1e293b;">{{ $ref['host'] }}</td>
                                <td style="text-align: right; color: #15803d; font-weight: bold;">{{ number_format($ref['count']) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="2" style="text-align: center; color: #94a3b8;">No referral traffic logged</td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </td>
        </tr>
    </table>

    <!-- Traffic Timeline -->
    <div class="section-title">Traffic Timeline Overview ({{ ucfirst($filter) }})</div>
    <table class="data-table">
        <thead>
            <tr>
                <th>Date / Period</th>
                <th style="text-align: right;">Unique Visitors</th>
                <th style="text-align: right;">Page Views</th>
                <th style="text-align: right;">Views per Visitor</th>
            </tr>
        </thead>
        <tbody>
            @forelse($timeline->take(15) as $item)
                <tr>
                    <td style="font-weight: bold;">{{ $item['label'] }}</td>
                    <td style="text-align: right;">{{ number_format($item['visitors']) }}</td>
                    <td style="text-align: right;">{{ number_format($item['page_views']) }}</td>
                    <td style="text-align: right;">
                        {{ $item['visitors'] > 0 ? round($item['page_views'] / $item['visitors'], 1) : 0 }}
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="4" style="text-align: center; color: #94a3b8;">No timeline data available</td>
                </tr>
            @endforelse
            @if($timeline->count() > 15)
                <tr>
                    <td colspan="4" style="text-align: center; font-style: italic; color: #64748b; font-size: 11px;">
                        Showing top 15 records. Download full report for complete set.
                    </td>
                </tr>
            @endif
        </tbody>
    </table>

    <div class="footer">
        This is an automated system generated report. &copy; {{ date('Y') }} American Plastics. All rights reserved.
    </div>
</body>
</html>
