<?php

session_start();

header("Access-Control-Allow-Origin: https://enerliths.com");
header("Access-Control-Allow-Credentials: true");
header("Access-Control-Allow-Headers: Content-Type");
header("Access-Control-Allow-Methods: POST, OPTIONS");
header("Content-Type: application/json");

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(200);
    exit();
}
require "../config/db.php";

$user_id = $_SESSION["user_id"];

/* GET DEPOSITS */

$deposits = [];

$stmt = $conn->prepare("
SELECT 
amount_usd as amount,
status,
created_at
FROM deposits
WHERE user_id=?
ORDER BY created_at DESC
LIMIT 20
");

$stmt->bind_param("i",$user_id);
$stmt->execute();

$res = $stmt->get_result();

while($row = $res->fetch_assoc()){
    $row["type"] = "MPESA";
    $deposits[] = $row;
}


/* GET WITHDRAWALS */

$withdrawals = [];

$stmt = $conn->prepare("
SELECT 
amount,
status,
created_at
FROM mpesa_withdrawals
WHERE user_id=?
ORDER BY created_at DESC
LIMIT 20
");

$stmt->bind_param("i",$user_id);
$stmt->execute();

$res = $stmt->get_result();

while($row = $res->fetch_assoc()){
    $row["type"] = "MPESA";
    $withdrawals[] = $row;
}


echo json_encode([
"deposits"=>$deposits,
"withdrawals"=>$withdrawals
]);