<?php
include '../../connect/db.php';



function randomDate($start, $end) {
    $timestamp = rand(strtotime($start), strtotime($end));
    return date("Y-m-d", $timestamp);
}

function getAmountRange($type) {
    $price1 = ["Walmart", "Amazon", "Target", "Costco", "Best Buy"];
    $price2 = ["Utilities Bill", "Credit Card Payment", "Mortgage Payment", "Car Loan", "Medical Expense", "Dining Out", "Subscription Services", "Gas Station"];
    $price3 = ["Machine Maintenance", "Machine Purchase", "Company Expenses"];

    if (in_array($type, $price1)) return [40, 700];
    if (in_array($type, $price2)) return [100, 80000];
    if (in_array($type, $price3)) return [3000, 700000];
    return [10, 100]; // fallback
}

$depositors = [
    "Salary payment", "Johnson", "Alice", "Williams", "Amazon Refund", "Payroll Inc",
    "David", "Michael", "Sandra", "Global Corp", "TransferWise", "Mariam", "Ibrahim",
    "Thomas", "Isabella", "Green Ltd", "Esther", "Omar", "Suleiman", "Tax Refunds", "Freelance Hub",
    "PayPal", "Stripe", "LinkedIn Bonus"
];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $acct_num = $_POST['acct_numx'] ?? '';
    $date_from = $_POST['date_from'] ?? '';
    $date_to = $_POST['date_to'] ?? '';
    $count = intval($_POST['status_number'] ?? 0);
    $types = json_decode($_POST['transaction_types'] ?? '[]', true);

 $sql = mysqli_query($con,"SELECT * FROM users WHERE account_num = '$acct_num' ");
           $row = mysqli_fetch_assoc($sql);

$currency = $row['usercurrency'];
    if (!$acct_num || !$date_from || !$date_to || $count <= 0 || empty($types)) {
        echo "Invalid input.";
        exit;
    }

    for ($i = 0; $i < $count; $i++) {
        $date = randomDate($date_from, $date_to);

        // Generate a random transaction ID
        $seed = str_split('ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789');
        shuffle($seed);
        $rand = '';
        foreach (array_rand($seed, 7) as $k) $rand .= $seed[$k];
        $transId = $rand;

        $status = 'Completed';

        // Randomly choose between 'Credit' or 'Debit'
        $trans_type = (rand(0, 1) === 1) ? 'Credit' : 'Debit';

        if ($trans_type === 'Credit') {
            $depositor = $depositors[array_rand($depositors)];
            $amount = rand(10000, 530000);
            $description = $depositor . ' deposited ' . formatCurrency($amount, $currency) . ' on ' . $date . ' Trans ID ' . $transId;
        } else {
            $type = $types[array_rand($types)];
            [$min, $max] = getAmountRange($type);
            $amount = rand($min, $max);
            $description = "Transaction of $type on $date with TRANS ID $transId";
        }

        $stmt = mysqli_prepare($con, "INSERT INTO transactions (user, amount, trans_id, date, status, type, description) VALUES (?, ?, ?, ?, ?, ?, ?)");
        mysqli_stmt_bind_param($stmt, "sssssss", $acct_num, $amount, $transId, $date, $status, $trans_type, $description);
        mysqli_stmt_execute($stmt);
    }

    echo "Generated $count transactions successfully.";
}
?>
