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

if (!isset($_GET['nhis'])) {
    echo json_encode(['status' => 'error', 'message' => 'Missing NHIS number']);
    exit;
}

$nhis = $_GET['nhis'];

// Credentials given by NHIS
$accountId = "AP1258";
$clientPassword = "b?q5JGlWBc8y3hZomFS26tgrP";

// API URL (example from NHIS documentation)
$url = "https://nhis.gov.gh/api/verify";

// Request payload
$payload = json_encode([
    "accountId" => $accountId,
    "clientPassword" => $clientPassword,
    "nhisNumber" => $nhis
]);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
    "Content-Type: application/json",
    "Content-Length: " . strlen($payload)
]);

$response = curl_exec($ch);

if (curl_errno($ch)) {
    echo json_encode(["status" => "error", "message" => curl_error($ch)]);
    curl_close($ch);
    exit;
}

curl_close($ch);

// Return NHIS API result
echo $response;
