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

// 1. Input Lena (GET request se)
$aadhaar = $_GET['aadhaar_number'] ?? '';
$otp     = $_GET['otp'] ?? '';
$txn_id  = $_GET['txn_id'] ?? '';

if (empty($aadhaar) || empty($otp) || empty($txn_id)) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Missing required parameters (aadhaar_number, otp, txn_id)'
    ], JSON_PRETTY_PRINT);
    exit;
}

// 2. UPDATED API URL
$api_url = 'https://sso.hp.gov.in/api/ahdRPV';

// 3. Payload (same structure)
$payload = json_encode([
    'aadhaarNumber' => $aadhaar,
    'otp' => $otp,
    'tnxID' => $txn_id
]);

// 4. UPDATED Headers (as per new network log)
$headers = [
    'User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36',
    'Accept: */*',
    'Accept-Language: en-GB,en-US;q=0.9,en;q=0.8',
    'Accept-Encoding: gzip, deflate',
    'Content-Type: application/json',
    'Origin: https://sso.hp.gov.in',
    'Referer: https://sso.hp.gov.in/registration',
    'Cookie: i18next=en-GB',
    'Sec-Fetch-Dest: empty',
    'Sec-Fetch-Mode: cors',
    'Sec-Fetch-Site: same-origin',
    'Priority: u=1, i'
];

// 5. cURL Request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');

$response   = curl_exec($ch);
$http_code  = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curl_error = curl_error($ch);
curl_close($ch);

// 6. Response Handling
if ($curl_error) {
    echo json_encode([
        'status' => 'error',
        'message' => 'cURL Error: ' . $curl_error
    ], JSON_PRETTY_PRINT);
    exit;
}

if ($http_code !== 200) {
    echo json_encode([
        'status' => 'failed',
        'message' => 'Failed: ' . $http_code,
        'raw_response' => $response
    ], JSON_PRETTY_PRINT);
    exit;
}

// 7. Success (HTTP 200)
$decoded = json_decode($response);

if (json_last_error() !== JSON_ERROR_NONE || !isset($decoded->uidData)) {
    echo json_encode([
        'status' => 'raw_response_error',
        'message' => 'API response was not valid JSON or missing uidData.',
        'data' => $response
    ], JSON_PRETTY_PRINT);
    exit;
}

$uidData = $decoded->uidData;

// POI
$poi = $uidData->Poi->{'$'} ?? null;
$name = $poi->name ?? '';
$dob = $poi->dob ?? '';
$gender_code = $poi->gender ?? '';

// POA
$poa = $uidData->Poa->{'$'} ?? null;
$address = implode(', ', array_filter([
    $poa->vtc ?? '',
    $poa->po ?? '',
    $poa->subdist ?? '',
    $poa->dist ?? '',
    $poa->state ?? '',
    $poa->pc ?? ''
]));

// Gender map
$gender = $gender_code;
if ($gender_code === 'M') $gender = 'Male';
elseif ($gender_code === 'F') $gender = 'Female';
elseif ($gender_code === 'T') $gender = 'Transgender';

// Photo
$image_src = '';
if (!empty($uidData->Pht)) {
    $clean = str_replace('\/', '/', $uidData->Pht);
    $image_src = 'data:image/jpeg;base64,' . $clean;
}

// Final Output
echo json_encode([
    'status' => 'success',
    'message' => 'OTP Validate Successfully',
    'name' => $name,
    'dob' => $dob,
    'gender' => $gender,
    'address' => $address,
    'image_src' => $image_src
], JSON_PRETTY_PRINT);
?>
