<?php

include_once __DIR__ . '/../secrets.php';

include '../db_connect.php';

$userid   = $_POST[ 'userid' ];
$mynumber   = $_POST[ 'mynumber' ];
$number   = $_POST[ 'number' ];
$balance   = $_POST[ 'balance' ];
$time     = $_POST[ 'time' ];
$operator = $_POST[ 'operator' ];
$title  = $_POST[ 'title' ];
$transid   = $_POST[ 'transid' ];
$status     = $_POST[ 'status' ];
$charge     = $_POST[ 'charge' ];
$details     = $_POST[ 'details' ];
$type     = $_POST[ 'type' ];
$key      = $_POST[ 'key' ];

$security_key = decrypData($key);


if ($security_key === false) {
    echo 'Invalid encryption data';
    exit;
}

if ($security_key == SECURITY_KEY_PLAIN) {

    $con = dbConnect();

    /* ---------- ১ ) গ্রাহকের id ও token খুঁজে বের করি ---------- */
    $recRes = $con->query( "SELECT userid, token, balance FROM signup WHERE number = '$number' LIMIT 1" );
    if ( $recRes->num_rows === 0 ) {
        echo 'Receiver number not found in database!';
        exit();
    }
    $recRow         = $recRes->fetch_assoc();
    $receiver_id    = $recRow[ 'userid' ];
    $receiverPrev   = ( float ) $recRow[ 'balance' ];
    // গ্রাহকের কাটার‑আগের ব্যালা

    $prevBalRes = $con->query( "SELECT balance FROM signup WHERE userid = '$userid' LIMIT 1" );
    if ( !$prevBalRes || $prevBalRes->num_rows === 0 ) {
        echo 'User not found';
        exit();
    }
    $prevBal = ( float ) $prevBalRes->fetch_assoc()[ 'balance' ];
    // কাটার আগের ব্যালান্স

    $newBal = round( $prevBal - $balance - $charge, 2 );
    $sendnewBal = round( $receiverPrev + $balance, 2 );

    /* ---------- ৩ ) ইতিহাসে ইনসার্ট করুন ( prev balance সহ ) ---------- */
    $sql = "INSERT INTO history_table(userid, number, balance, profit, oldbalance, time, operator, title, transid, status, details, lastnumber, type, newbalance) VALUES
                ('$userid', '$number', '$balance', '$charge', '$prevBal', '$time', '$operator','$title', '$transid', 'Success', '$details', 'N/A', '$type', '$newBal')";
    $result = mysqli_query( $con, $sql );

    /* ---------- ৩ ) ইতিহাসে ইনসার্ট করুন ( prev balance সহ ) ---------- */
    $sql1 = "INSERT INTO history_table(userid, number, balance, profit, oldbalance, time, operator, title, transid, status, details, lastnumber, type, newbalance) VALUES
                ('$receiver_id', '$mynumber', '$balance', '0', '$receiverPrev', '$time', 'Receive Money','Receive Money', '$transid', 'Success', '$details', 'N/A', 'Received', '$sendnewBal')";
    $result1 = mysqli_query( $con, $sql1 );

    $sqlUp = "UPDATE signup SET balance = ROUND(balance - $balance - $charge, 2) WHERE userid = '$userid'";
    $result2 = mysqli_query( $con, $sqlUp );

    $sqlUp1 = "UPDATE signup SET balance = ROUND(balance + $balance, 2) WHERE userid = '$receiver_id'";
    $result3 = mysqli_query( $con, $sqlUp1 );

    if ( $result && $result1 && $result2 && $result3 ) {
        echo json_encode( [
            'status'         => 'success',
            'message'        => 'Successfully',
            'receiver_userid' => $receiver_id
        ] );
    } else {
        echo 'Query Error: ' . mysqli_error( $con );
    }

} else {
    echo 'Invalid password';
}


?>
