<?php
// Sabse upar ye lines add karein taaki errors JSON ko kharab na karein
error_reporting(0); 
header('Content-Type: application/json');

require_once '../config/db.php';

$response = [];

try {
    // 1. Total Users
    $u_res = mysqli_query($conn, "SELECT COUNT(*) as count FROM users");
    $response['total_users'] = mysqli_fetch_assoc($u_res)['count'] ?? 0;

    // 2. Total USDT (Deposits)
    $d_res = mysqli_query($conn, "SELECT SUM(amount) as total FROM deposits WHERE status = 'success'");
    $response['total_usdt'] = mysqli_fetch_assoc($d_res)['total'] ?? 0;
// 3. Active Users — jin ke pass successful deposit hai
$a_res = mysqli_query($conn, "
    SELECT COUNT(DISTINCT u.id) as count 
    FROM users u
    INNER JOIN deposits d ON d.wallet = u.wallet AND d.status = 'success'
    WHERE u.status != 'blocked'
");
$response['active_users'] = mysqli_fetch_assoc($a_res)['count'] ?? 0;

// 4. Inactive Users — jin ke pass koi successful deposit nahi
$inactive_res = mysqli_query($conn, "
    SELECT COUNT(*) as count 
    FROM users u
    WHERE u.status != 'blocked'
    AND u.wallet NOT IN (
        SELECT DISTINCT wallet FROM deposits WHERE status = 'success'
    )
");
$response['inactive_users'] = mysqli_fetch_assoc($inactive_res)['count'] ?? 0;

// 4b. Blocked Users
$blocked_res = mysqli_query($conn, "SELECT COUNT(*) as count FROM users WHERE status = 'blocked'");
$response['blocked_users'] = mysqli_fetch_assoc($blocked_res)['count'] ?? 0;

    // 5. Total Deposit Amount (All successful deposits)
    $total_deposit_res = mysqli_query($conn, "SELECT SUM(amount) as total FROM deposits WHERE status = 'success'");
    $response['total_deposit'] = mysqli_fetch_assoc($total_deposit_res)['total'] ?? 0;

    // 6. Total Withdrawal Amount (All successful withdrawals)
  $total_withdrawal_res = mysqli_query($conn, "SELECT SUM(amount) as total FROM withdrawals WHERE status IN ('success','completed')");
    $response['total_withdrawal'] = mysqli_fetch_assoc($total_withdrawal_res)['total'] ?? 0;

    // 7. Total Investment (Sum of all deposits - same as total_deposit)
    $response['total_investment'] = $response['total_deposit'];

    // 8. Today's Investment
    $today_investment_res = mysqli_query($conn, "SELECT SUM(amount) as total FROM deposits WHERE DATE(created_at) = CURDATE() AND status = 'success'");
    $response['today_investment'] = mysqli_fetch_assoc($today_investment_res)['total'] ?? 0;

    // 9. Yesterday's Investment
    $yesterday_investment_res = mysqli_query($conn, "SELECT SUM(amount) as total FROM deposits WHERE DATE(created_at) = DATE_SUB(CURDATE(), INTERVAL 1 DAY) AND status = 'success'");
    $response['yesterday_investment'] = mysqli_fetch_assoc($yesterday_investment_res)['total'] ?? 0;

    // 10. Total Income Distribution
    $total_income_res = mysqli_query($conn, "SELECT SUM(level_income + direct_income + roi_income) as total FROM incomes");
    $response['total_income'] = mysqli_fetch_assoc($total_income_res)['total'] ?? 0;

    // 11. Today's Income Distribution
    $today_income_res = mysqli_query($conn, "SELECT SUM(level_income + direct_income + roi_income) as total FROM incomes WHERE DATE(updated_at) = CURDATE()");
    $response['today_income'] = mysqli_fetch_assoc($today_income_res)['total'] ?? 0;

    // 12. Average Daily Income (last 30 days)
    $avg_income_res = mysqli_query($conn, "
        SELECT AVG(daily_total) as avg_daily 
        FROM (
            SELECT SUM(level_income + direct_income + roi_income) as daily_total, DATE(updated_at) as date 
            FROM incomes 
            WHERE updated_at >= DATE_SUB(CURDATE(), INTERVAL 30 DAY)
            GROUP BY DATE(updated_at)
        ) as daily_sums
    ");
    $response['avg_daily_income'] = round(mysqli_fetch_assoc($avg_income_res)['avg_daily'] ?? 0, 2);

    echo json_encode([
        'status' => 'success',
        'data' => $response
    ]);

} catch (Exception $e) {
    echo json_encode([
        'status' => 'error',
        'message' => $e->getMessage(),
        'data' => [
            'total_users' => 0,
            'total_usdt' => 0,
            'active_users' => 0,
            'inactive_users' => 0,
            'total_deposit' => 0,
            'total_withdrawal' => 0,
            'total_investment' => 0,
            'today_investment' => 0,
            'yesterday_investment' => 0,
            'total_income' => 0,
            'today_income' => 0,
            'avg_daily_income' => 0
        ]
    ]);
}
exit;
?>