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

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

$user_id = $_SESSION['user_id'] ?? null;
if (!$user_id) {
    http_response_code(401);
    echo json_encode(['error' => 'Unauthorized']);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $stmt = $pdo->prepare("SELECT * FROM user_withdrawal_details WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $details = $stmt->fetch(PDO::FETCH_ASSOC) ?: [];
    echo json_encode(['details' => $details]);
    exit;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $input = json_decode(file_get_contents('php://input'), true);
    if (!$input) {
        http_response_code(400);
        echo json_encode(['error' => 'Invalid JSON']);
        exit;
    }

    // Fetch existing details
    $stmt = $pdo->prepare("SELECT * FROM user_withdrawal_details WHERE user_id = ?");
    $stmt->execute([$user_id]);
    $details = $stmt->fetch(PDO::FETCH_ASSOC);

    // Which details are being updated?
    if (isset($input['bank'])) {
        $bank = $input['bank'];
        $account_holder = trim($bank['account_holder'] ?? '');
        $bank_account = trim($bank['bank_account'] ?? '');
        $ifsc_code = trim($bank['ifsc_code'] ?? '');

        if ($details) {
            $stmt = $pdo->prepare("UPDATE user_withdrawal_details SET account_holder=?, bank_account=?, ifsc_code=? WHERE user_id=?");
            $stmt->execute([$account_holder, $bank_account, $ifsc_code, $user_id]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO user_withdrawal_details (user_id, account_holder, bank_account, ifsc_code) VALUES (?, ?, ?, ?)");
            $stmt->execute([$user_id, $account_holder, $bank_account, $ifsc_code]);
        }
        echo json_encode(['success' => 'Bank details saved successfully']);
        exit;
    }

    if (isset($input['usdt'])) {
        $usdt = $input['usdt'];
        $usdt_address = trim($usdt['usdt_address'] ?? '');

        if ($details) {
            $stmt = $pdo->prepare("UPDATE user_withdrawal_details SET usdt_address=? WHERE user_id=?");
            $stmt->execute([$usdt_address, $user_id]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO user_withdrawal_details (user_id, usdt_address) VALUES (?, ?)");
            $stmt->execute([$user_id, $usdt_address]);
        }
        echo json_encode(['success' => 'USDT address saved successfully']);
        exit;
    }

    if (isset($input['pkr'])) {
        $pkr = $input['pkr'];
        $jazzcash_cnic = trim($pkr['jazzcash_cnic'] ?? '');
        $jazzcash_account = trim($pkr['jazzcash_account'] ?? '');
        $easypaisa_account = trim($pkr['easypaisa_account'] ?? '');

        if ($details) {
            $stmt = $pdo->prepare("UPDATE user_withdrawal_details SET jazzcash_cnic=?, jazzcash_account=?, easypaisa_account=? WHERE user_id=?");
            $stmt->execute([$jazzcash_cnic, $jazzcash_account, $easypaisa_account, $user_id]);
        } else {
            $stmt = $pdo->prepare("INSERT INTO user_withdrawal_details (user_id, jazzcash_cnic, jazzcash_account, easypaisa_account) VALUES (?, ?, ?, ?)");
            $stmt->execute([$user_id, $jazzcash_cnic, $jazzcash_account, $easypaisa_account]);
        }
        echo json_encode(['success' => 'PKR withdrawal details saved successfully']);
        exit;
    }

    http_response_code(400);
    echo json_encode(['error' => 'No valid data provided']);
}
