<?php
session_start();

require_once '../models/User.php';
require_once '../include/Helper.php';
require_once '../models/Document.php';
require_once '../models/Withdrawal.php';
require_once '../models/Invoice.php';
require_once '../include/DatabaseConn.php';
require_once '../include/Constants.php';
require_once '../models/Admin.php';
require_once '../models/Wallet.php';
require_once '../models/Referral.php';

$admin = Admin::getCurrentAdmin();

if (is_null($admin)) {
    $url = $_SERVER['REQUEST_URI'];
    header("location: ./login?" . RETURN_URL . "=" . $url);
    die();
}

$db = new DatabaseConn();
$link = $db->connect();



// Get user email
$userEmail = $_GET['user'];

// Get id and source from GET parameters
if (isset($_GET['id']) && isset($_GET['source'])) {
    $id = $_GET['id'];
    $source = $_GET['source'];

    // Initialize success flag
    $success = false;

    // Perform deletion based on source
    switch ($source) {
        case 'wallet':
            $success = Wallet::deleteWalletDeposit($link, $id, $userEmail);
            break;
        case 'referral':
            $success = Wallet::deleteReferralDeposit($link, $id, $userEmail);
            break;
        case 'invoice':
            $success = Invoice::deleteInvoice($link, $id, $userEmail);
            break;
        case 'topup':
            $success = Invoice::deleteTopup($link, $id, $userEmail);
            break;
        case 'transfer':
            $success = Withdrawal::deleteWalletTransfer($link, $id, $userEmail);
            break;
        default:
            $success = false;
    }

    if ($success) {
        $_SESSION['success'] = "Transaction deleted successfully";
        header('Location: view-user.php?email='. $userEmail);
    } else {
         $_SESSION['error'] = "Deletion failed";
        header('Location: view-user.php?email='. $userEmail);
    }
} else {
     $_SESSION['error'] = "Invalid request";
    header('Location: view-user.php?email='. $userEmail);
}
?>
