<?php
include '../config.php';
header('Content-Type: application/json');

// Zaman değişkenleri
$today = strtotime('today');
$tomorrow = strtotime('+1 day', $today);
$suan5dk = time() - 300;

// TEK SORGU: Tüm kullanıcı istatistiklerini çek
$stats = $db->query("SELECT 
    COUNT(*) as toplam,
    SUM(CASE WHEN user_bot = 0 AND user_sehir <> '' AND user_kayit >= $today AND user_kayit <= $tomorrow THEN 1 ELSE 0 END) as bugun,
    SUM(CASE WHEN user_bot = 0 AND user_songiris >= $suan5dk AND user_sehir <> '' AND user_cinsiyet = 'erkek' THEN 1 ELSE 0 END) as aktif
    FROM kullanicilar WHERE user_bot = 0")->fetch(PDO::FETCH_ASSOC);

// Bugünkü kazancı hesapla - basit ve hızlı sorgu
$urunFiyatlari = [
    '1paket' => 49.9, '2paket' => 99.9, '3paket' => 199.9, '4paket' => 299.9,
    '5paket' => 399.9, '6paket' => 699.9, 'visitor' => 29.9, 'callunlock' => 199.9,
    'visitorunlock' => 59.9, '100paket' => 199.9, '100paketindirim' => 149.9,
    '100jetonnormal' => 149.9, '100jetonindirim' => 99.9, '250paket' => 299.9,
    '500paket' => 399.9, '1000paket' => 499.9, '2500paket' => 799.9,
    '5000paket' => 1399.9, 'sinirsizpaket' => 2599.9, 'sinpaket' => 699.9,
    'aramapaket' => 249.9, 'iletisimpaket' => 249.9, 'onaylihesap' => 249.9,
    'boostpaket' => 499.9, 'konumpaket' => 249.9
];

$bugunKazanc = 0;
$satisQuery = $db->query("SELECT urun, COUNT(*) as adet FROM odemeler 
    WHERE status = 1 AND tarih >= $today AND tarih <= $tomorrow 
    GROUP BY urun");

while ($row = $satisQuery->fetch(PDO::FETCH_ASSOC)) {
    if (isset($urunFiyatlari[$row['urun']])) {
        $bugunKazanc += ($row['adet'] * $urunFiyatlari[$row['urun']] * 0.7083);
    }
}

// JSON döndür
echo json_encode([
    'bugunKazanc' => number_format($bugunKazanc, 2, '.', ''),
    'bugunUye' => (int)$stats['bugun'],
    'genelUye' => (int)$stats['toplam'],
    'randomAktifSayi' => (int)$stats['aktif']
]);
?>
