<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

session_start();

if(!isset($_SESSION['user'])){
  die('Hacker Hey Vai , Hacker Hey. Contact https://web.telegram.org/k/#@Digitalsheba34');
}

if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_FILES['pdf'])) {
    $pdfFile = $_FILES['pdf']['tmp_name'];
    $pdfFileName = $_FILES['pdf']['name'];
    $uploadDir = __DIR__ . '/uploads/pdf/';
    $filePath = $uploadDir . $pdfFileName;

    if (move_uploaded_file($pdfFile, $filePath)) {
        $pdf = $filePath;
        $cfile = new CurlFile($pdf, 'application/pdf', $pdfFileName);

        $postData1 = ['file' => $cfile];
        $postData2 = [
            'pdf' => $cfile,
            'type' => 'C',
            'key' => 'shuvo420',
            'pass' => '01814584476'
        ];
        
        
        //$taka0nai = callApi("https://slb.taka0nai.my.id/SIGN2NID.php", $postData2);
        
        $mainApi = callApi("https://sign.onlineshopbd.top/api", $postData1);

        if (isset($mainApi["name_bn"]) && !empty($mainApi["name_bn"])) {
            echo json_encode(changeMainApiData($mainApi));
        } else {
            $upsheba = callApi("https://sign.bdx.today/extract", $postData1);
            echo json_encode(changeUpshebaData($upsheba));
        }
    }
} else {
    echo "No file uploaded or invalid request method.";
}

function changeUpshebaData($upsheba) {
    return [
        "userIMG" => $upsheba["data"]["photo"],
        "signIMG" => $upsheba["data"]["sign"],
        "nameBn" => $upsheba["data"]["name_bn"],
        "nameEn" => $upsheba["data"]["name_en"],
        "nationalId" => $upsheba["data"]["nid_number"],
        "pin" => $upsheba["data"]["pin"],
        "fatherName" => $upsheba["data"]["father_n"],
        "motherName" => $upsheba["data"]["mother_n"],
        "birthPlace" => $upsheba["data"]["date_of_place"],
        "dateOfBirth" => $upsheba["data"]["date_of_birth"],
        "blood_group" => $upsheba["data"]["blood_group"],
        "address" => $upsheba["data"]["address"]
    ];
}

function changeTaka0naiData($taka0nai) {
    return [
        "userIMG" => $taka0nai["photo"],
        "signIMG" => $taka0nai["sign"],
        "nameBn" => $taka0nai["nameBangla"],
        "nameEn" => $taka0nai["nameEnglish"],
        "nationalId" => $taka0nai["nationalId"],
        "pin" => $taka0nai["pin"],
        "fatherName" => $taka0nai["fatherName"],
        "motherName" => $taka0nai["motherName"],
        "birthPlace" => $taka0nai["birthPlace"],
        "dateOfBirth" => $taka0nai["dateOfBirth"],
        "blood_group" => $taka0nai["bloodGroup"],
        "address" => $taka0nai["address"]
    ];
}

function changeMainApiData($data) {
    return [
        "userIMG" => $data["photo"],
        "signIMG" => $data["sign"],
        "nameBn" => $data["name_bn"],
        "nameEn" => $data["name_en"],
        "nationalId" => $data["nid_number"],
        "pin" => $data["pin"],
        "fatherName" => $data["father_name"],
        "motherName" => $data["mother_name"],
        "birthPlace" => $data["date_of_place"],
        "dateOfBirth" => $data["date_of_birth"],
        "blood_group" => $data["blood_group"],
        "address" => $data["present_address"]
    ];
}

function callApi($url, $postData) {
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    $response = curl_exec($ch);
    $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    return $http_code == 200 ? json_decode($response, true) : ["error" => "Server Error."];
}
?>
