<?php
include "../../conn.php";
include "../../functions2.php";

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

$shonusign = $_POST['signature'] ?? '';

if ($shonusign == $signature) {
    $bearer = explode(" ", $_SERVER['HTTP_AUTHORIZATION']);
    $author = $bearer[1];
    $is_jwt_valid = is_jwt_valid($author);
    $data_auth = json_decode($is_jwt_valid, true);

    if ($data_auth['status'] === 'Success') {
        $user_token = mysqli_real_escape_string($conn, $author);

        // Step 1: Get user id from shonu_subjects
        $query1 = "SELECT id FROM shonu_subjects WHERE akshinak = '$user_token' LIMIT 1";
        $result1 = mysqli_query($conn, $query1);
        $user_data = mysqli_fetch_assoc($result1);

        if (!$user_data) {
            echo json_encode(["code" => 1, "msg" => "User not found"]);
            exit();
        }

        $user_id = mysqli_real_escape_string($conn, $user_data['id']);

        // Step 2: Check recharge status from thevani table using motta
        $recharge_query = "SELECT motta, sthiti FROM thevani WHERE balakedara = '$user_id' ORDER BY dinankavannuracisi DESC LIMIT 1";
        $recharge_result = mysqli_query($conn, $recharge_query);
        $recharge_data = mysqli_fetch_assoc($recharge_result);

        // Check recharge conditions
        $min_recharge = 300; // Minimum required recharge amount
        $current_amount = $recharge_data ? floatval($recharge_data['motta']) : 0;
		$left_amount = $min_recharge - $current_amount;
        
        if (!$recharge_data || $recharge_data['sthiti'] != 1 || $current_amount < $min_recharge) {
            echo json_encode([
                "code" => 1,
                "msg" => "You need to Recharge ৳$left_amount to use SAFE WALLET",
                "recharge_status" => $recharge_data ? $recharge_data['sthiti'] : "No recharge found",
                "recharge_amount" => $current_amount
            ]);
            exit();
        }

        // Step 3: Fetch balance (motta) from shonu_kaichila
        $query2 = "SELECT motta FROM shonu_kaichila WHERE balakedara = '$user_id' LIMIT 1";
        $result2 = mysqli_query($conn, $query2);
        $balance_data = mysqli_fetch_assoc($result2);
        $balance = ($balance_data) ? floatval($balance_data['motta']) : 0.00;

        // Step 4: Fetch total transferred to safe box
        $safe_query = "SELECT SUM(amount) as transferredAmount FROM safe_box WHERE user_id = '$user_id'";
        $safe_result = mysqli_query($conn, $safe_query);
        $transferredAmount = mysqli_fetch_assoc($safe_result)['transferredAmount'] ?? 0;

        // Successful response
        $response = [
            "data" => [
                "balance" => $balance,
                "amountOfCode" => $transferredAmount,
                "last_recharge" => $recharge_data['motta'] // Updated to motta
            ],
            "code" => 0,
            "msg" => "Succeed",
            "msgCode" => 0,
            "serviceNowTime" => date("Y-m-d H:i:s")
        ];

        echo json_encode($response);
    } else {
        echo json_encode(["code" => 1, "msg" => "Unauthorized"]);
    }
} else {
    echo json_encode(["code" => 1, "msg" => "Invalid signature"]);
}
?>