<?php
require_once __DIR__ . '/../db/config.php';
session_start();

header('Content-Type: application/json');

if (!isset($_SESSION['admin_id'])) {
    echo json_encode(['success' => false, 'error' => 'Unauthorized']);
    exit;
}

$db = db();
$admin_id = $_SESSION['admin_id'];

// Get admin info for agent check
$stmt = $db->prepare("SELECT is_agent FROM admins WHERE id = ?");
$stmt->execute([$admin_id]);
$admin = $stmt->fetch();

$action = $_GET['action'] ?? '';
if ($action === 'clear') {
    $type = $_GET['type'] ?? '';
    if ($type) {
        $_SESSION['admin_cleared_' . $type] = time();
    }
    echo json_encode(['success' => true]);
    exit;
}

$pending_recharge = 0;
$pending_withdrawal = 0;
$pending_kyc = 0;
$active_binary = 0;
$active_spot = 0;
$active_contract = 0;
$new_messages = 0;
$new_registrations = 0;

$cleared_recharge = $_SESSION['admin_cleared_finance'] ?? 0;
$cleared_kyc = $_SESSION['admin_cleared_kyc'] ?? 0;
$cleared_binary = $_SESSION['admin_cleared_binary'] ?? 0;
$cleared_spot = $_SESSION['admin_cleared_spot'] ?? 0;
$cleared_contract = $_SESSION['admin_cleared_contract'] ?? 0;
$cleared_messages = $_SESSION['admin_cleared_messages'] ?? 0;
$cleared_users = $_SESSION['admin_cleared_users'] ?? 0;

function getCount($db, $sql, $params) {
    $stmt = $db->prepare($sql);
    $stmt->execute($params);
    return (int)$stmt->fetchColumn();
}

if ($admin['is_agent']) {
    $agent_id = $admin_id;
    $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'recharge' AND r.status = '0' AND u.agent_id = ? AND UNIX_TIMESTAMP(r.created_at) > ?", [$agent_id, $cleared_recharge]);
    $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests r JOIN users u ON r.user_id = u.id WHERE r.type = 'withdrawal' AND r.status = '0' AND u.agent_id = ? AND UNIX_TIMESTAMP(r.created_at) > ?", [$agent_id, $cleared_recharge]);
    $pending_kyc = getCount($db, "SELECT COUNT(*) FROM users WHERE kyc_status = 1 AND agent_id = ? AND UNIX_TIMESTAMP(created_at) > ?", [$agent_id, $cleared_kyc]);
    $active_binary = getCount($db, "SELECT COUNT(*) FROM binary_orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'pending' AND u.agent_id = ? AND UNIX_TIMESTAMP(o.created_at) > ?", [$agent_id, $cleared_binary]);
    $active_spot = getCount($db, "SELECT COUNT(*) FROM spot_orders o JOIN users u ON o.user_id = u.id WHERE o.status = 0 AND u.agent_id = ? AND UNIX_TIMESTAMP(o.created_at) > ?", [$agent_id, $cleared_spot]);
    $active_contract = getCount($db, "SELECT COUNT(*) FROM contract_orders o JOIN users u ON o.user_id = u.id WHERE o.status = 'open' AND u.agent_id = ? AND UNIX_TIMESTAMP(o.created_at) > ?", [$agent_id, $cleared_contract]);
    $new_messages = getCount($db, "SELECT COUNT(*) FROM messages m JOIN users u ON m.user_id = u.id WHERE m.sender = 'user' AND u.agent_id = ? AND UNIX_TIMESTAMP(m.created_at) > ?", [$agent_id, $cleared_messages]);
    $new_registrations = getCount($db, "SELECT COUNT(*) FROM users WHERE agent_id = ? AND UNIX_TIMESTAMP(created_at) > ?", [$agent_id, $cleared_users]);
} else {
    $pending_recharge = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'recharge' AND status = '0' AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_recharge]);
    $pending_withdrawal = getCount($db, "SELECT COUNT(*) FROM finance_requests WHERE type = 'withdrawal' AND status = '0' AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_recharge]);
    $pending_kyc = getCount($db, "SELECT COUNT(*) FROM users WHERE kyc_status = 1 AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_kyc]);
    $active_binary = getCount($db, "SELECT COUNT(*) FROM binary_orders WHERE status = 'pending' AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_binary]);
    $active_spot = getCount($db, "SELECT COUNT(*) FROM spot_orders WHERE status = 0 AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_spot]);
    $active_contract = getCount($db, "SELECT COUNT(*) FROM contract_orders WHERE status = 'open' AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_contract]);
    $new_messages = getCount($db, "SELECT COUNT(*) FROM messages WHERE sender = 'user' AND UNIX_TIMESTAMP(created_at) > ?", [$cleared_messages]);
    $new_registrations = getCount($db, "SELECT COUNT(*) FROM users WHERE UNIX_TIMESTAMP(created_at) > ?", [$cleared_users]);
}

$total = $pending_recharge + $pending_withdrawal + $pending_kyc + $active_binary + $active_spot + $active_contract + $new_messages + $new_registrations;
$sound_trigger_count = $pending_recharge + $pending_withdrawal + $new_messages + $new_registrations;

echo json_encode([
    'success' => true,
    'counts' => [
        'recharge' => $pending_recharge,
        'withdrawal' => $pending_withdrawal,
        'kyc' => $pending_kyc,
        'binary' => $active_binary,
        'spot' => $active_spot,
        'contract' => $active_contract,
        'messages' => $new_messages,
        'users' => $new_registrations,
        'total' => $total,
        'sound_total' => $sound_trigger_count
    ]
]);
