<?php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: *");
header("Access-Control-Allow-Methods: POST");
header("Content-Type: application/json");

// Read POST data
$data = json_decode(file_get_contents("php://input"), true);

require '../includes/db.php';
require '../includes/auth.php';

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

// Utility function to format numbers safely
function nf($v, $d = 2) {
    return number_format((float)($v ?? 0), $d);
}

// Load user
$stmt = $pdo->prepare("SELECT * FROM users WHERE id = ?");
$stmt->execute([$_SESSION['user_id']]);
$user = $stmt->fetch();
if (!$user) {
    http_response_code(404);
    echo json_encode(['error' => 'User not found']);
    exit;
}

// Recent transactions
$transactionsStmt = $pdo->prepare("SELECT * FROM transactions WHERE user_id = ? ORDER BY id DESC LIMIT 10");
$transactionsStmt->execute([$user['id']]);
$transactions = $transactionsStmt->fetchAll(PDO::FETCH_ASSOC);

// Total Pay-In
$stmt = $pdo->prepare("SELECT SUM(amount) FROM transactions WHERE user_id = ? AND type = 'payin' AND status = 'success'");
$stmt->execute([$user['id']]);
$total_payin = (float)$stmt->fetchColumn();

// Total Pay-Out
$stmt = $pdo->prepare("SELECT SUM(total_deducted) FROM withdrawal_requests WHERE user_id = ? AND status = 'success'");
$stmt->execute([$user['id']]);
$total_payout = (float)$stmt->fetchColumn();

// Today Pay-In
$stmt = $pdo->prepare("SELECT SUM(amount) FROM transactions WHERE user_id = ? AND type = 'payin' AND status = 'success' AND DATE(created_at) = CURDATE()");
$stmt->execute([$user['id']]);
$today_payin = (float)$stmt->fetchColumn();

// Yesterday Pay-In
$stmt = $pdo->prepare("SELECT SUM(amount) FROM transactions WHERE user_id = ? AND type = 'payin' AND status = 'success' AND DATE(created_at) = CURDATE() - INTERVAL 1 DAY");
$stmt->execute([$user['id']]);
$yesterday_payin = (float)$stmt->fetchColumn();

// Today Pay-Out
$stmt = $pdo->prepare("SELECT SUM(total_deducted) FROM withdrawal_requests WHERE user_id = ? AND status = 'success' AND DATE(created_at) = CURDATE()");
$stmt->execute([$user['id']]);
$today_payout = (float)$stmt->fetchColumn();

// Yesterday Pay-Out
$stmt = $pdo->prepare("SELECT SUM(total_deducted) FROM withdrawal_requests WHERE user_id = ? AND status = 'success' AND DATE(created_at) = CURDATE() - INTERVAL 1 DAY");
$stmt->execute([$user['id']]);
$yesterday_payout = (float)$stmt->fetchColumn();

echo json_encode([
    'user' => [
        'name' => $user['name'],
        'currency' => $user['currency'],
        'balance' => (float)$user['balance'],
        'merchant_key' => $user['merchant_key'],
        'secret_key' => $user['secret_key'],
    ],
    'stats' => [
        'total_payin' => $total_payin,
        'total_payout' => $total_payout,
        'today_payin' => $today_payin,
        'yesterday_payin' => $yesterday_payin,
        'today_payout' => $today_payout,
        'yesterday_payout' => $yesterday_payout,
    ],
    'transactions' => $transactions,
]);
