<?php
session_start();
header('Content-Type: application/json');
require_once "../includes/functions.php"; 
$user_id = $_SESSION['user_id'] ?? 0;

$bank = trim($_POST['bank_name'] ?? '');
$account_name   = trim($_POST['account_name'] ?? '');

$amount = trim($_POST['amount'] ?? '');
$account_number   = trim($_POST['account_number'] ?? '');

if (empty($bank) || empty($account_name)|| empty($amount)|| empty($account_number)) {
    echo json_encode(["status" => "error", "message" => "all field required"]);
    exit;
}


if(!$user_id){
    echo json_encode(["status"=>"error","message"=>"Unauthorized"]);
    exit;
}

if($amount < 1000){
   echo json_encode(["status"=>"error","message"=>"Minmum amount for withdrawal is 1000"]);
    exit; 
}

$response = requestWithdrawal($user_id, $amount, $bank, $account_name, $account_number);
if ($response["status"] === "success") {
    echo json_encode($response);
} else {
    echo json_encode($response);
}
