<?php

$today = strtotime('today');
$tomorrow = strtotime('+1 day', $today);
$yesterday = strtotime('-1 day', $today);
$bugun = date('Y-m-d');
$dun = date('Y-m-d', time()-24*60*60);
$suan = time();

function yuzdeHesaplama($sayi,$yuzde){ 
    return ($sayi*$yuzde)/100;
} 

// Kullanıcı sayılarını TEK SORGU ile çek (aktif kullanıcı dahil)
$suan5dk = time() - 300;
$userStats = $db->query("SELECT 
    SUM(CASE WHEN user_bot = 0 THEN 1 ELSE 0 END) 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_sehir <> '' AND user_kayit >= $yesterday AND user_kayit <= $today THEN 1 ELSE 0 END) as dun,
    SUM(CASE WHEN user_bot = 1 THEN 1 ELSE 0 END) as bot,
    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")->fetch(PDO::FETCH_ASSOC);

$toplamUser = $userStats['toplam'] ?? 0;
$bugunUser = $userStats['bugun'] ?? 0;
$dunUser = $userStats['dun'] ?? 0;
$botUser = $userStats['bot'] ?? 0;
$randomsayisi = $userStats['aktif'] ?? 0;

$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;
$dunKazanc = 0;
$buAyKazanc = 0;
$gecenAyKazanc = 0;

// Bugünün tarihi ve UNIX timestamp'i
$bugun = date('Y-m-d');
$bugunTimestamp = strtotime($bugun);

// Ayın ilk gününün UNIX timestamp'i
$buAyinIlkGun = strtotime(date('Y-m-01', $bugunTimestamp));

// Geçen ayın ilk ve son gününün UNIX timestamp'i
$gecenAyinIlkGun = strtotime(date('Y-m-01 00:00:00', strtotime('-1 month', $bugunTimestamp)));
$gecenAyinSonGun = strtotime(date('Y-m-t 23:59:59', strtotime('-1 month', $bugunTimestamp)));

// TÜM SATIŞ VERİLERİNİ TEK SORGU İLE ÇEK (96 sorgu yerine 1 sorgu!)
$satisQuery = $db->query("SELECT 
    urun,
    SUM(CASE WHEN tarih >= $today AND tarih <= $tomorrow THEN 1 ELSE 0 END) as bugun_adet,
    SUM(CASE WHEN tarih >= $yesterday AND tarih <= $today THEN 1 ELSE 0 END) as dun_adet,
    SUM(CASE WHEN tarih >= $buAyinIlkGun AND tarih <= ($bugunTimestamp + 86400) THEN 1 ELSE 0 END) as buay_adet,
    SUM(CASE WHEN tarih >= $gecenAyinIlkGun AND tarih <= $gecenAyinSonGun THEN 1 ELSE 0 END) as gecenay_adet
    FROM odemeler 
    WHERE status = 1 
    GROUP BY urun");

$satisVerileri = [];
while ($row = $satisQuery->fetch(PDO::FETCH_ASSOC)) {
    $satisVerileri[$row['urun']] = $row;
}

// Kazançları hesapla
foreach ($urunFiyatlari as $urun => $fiyat) {
    $bugunAdet = $satisVerileri[$urun]['bugun_adet'] ?? 0;
    $dunAdet = $satisVerileri[$urun]['dun_adet'] ?? 0;
    $buAyAdet = $satisVerileri[$urun]['buay_adet'] ?? 0;
    $gecenAyAdet = $satisVerileri[$urun]['gecenay_adet'] ?? 0;

    $bugunKazanc += yuzdeHesaplama($bugunAdet * $fiyat, 70.83);
    $dunKazanc += yuzdeHesaplama($dunAdet * $fiyat, 70.83);
    $buAyKazanc += yuzdeHesaplama($buAyAdet * $fiyat, 70.83);
    $gecenAyKazanc += yuzdeHesaplama($gecenAyAdet * $fiyat, 70.83);
}

?>
