<?php
// Enable all errors for diagnostic testing
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// Ensure the path correctly points to where you saved the helper class
require_once 'dripend/includes/NhisApiHelper.php';

$result = null;
$execution_time = 0;
$test_nhis_number = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $test_nhis_number = trim($_POST['nhis_number'] ?? '');
    
    // Validate it is exactly 8 digits
    if (preg_match('/^\d{8}$/', $test_nhis_number)) {
        $api = new NhisApiHelper();
        
        $start_time = microtime(true);
        $result = $api->fetchCccCode($test_nhis_number);
        $end_time = microtime(true);
        $execution_time = round(($end_time - $start_time), 3);
    } else {
        $result = [
            'success' => false, 
            'message' => 'Validation Error: Please enter exactly 8 digits.'
        ];
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NHIA API Diagnostic</title>
    <style>
        body { font-family: 'Segoe UI', system-ui, sans-serif; background: #f8fafc; color: #0f172a; padding: 40px; }
        .container { max-width: 700px; margin: 0 auto; background: #fff; padding: 30px; border-radius: 12px; box-shadow: 0 4px 15px rgba(0,0,0,0.05); }
        h2 { color: #1e3a8a; margin-top: 0; }
        .form-group { margin-bottom: 20px; }
        input[type="text"] { width: 100%; padding: 15px; font-size: 18px; border: 2px solid #cbd5e1; border-radius: 8px; box-sizing: border-box; outline: none; }
        input[type="text"]:focus { border-color: #3b82f6; }
        button { background: #1e3a8a; color: #fff; border: none; padding: 15px 30px; font-size: 16px; font-weight: bold; border-radius: 8px; cursor: pointer; transition: 0.2s; }
        button:hover { background: #1e293b; }
        .result-box { margin-top: 30px; padding: 20px; border-radius: 8px; }
        .success { background: #dcfce7; border-left: 5px solid #16a34a; color: #166534; }
        .error { background: #fee2e2; border-left: 5px solid #ef4444; color: #991b1b; }
        pre { background: #f1f5f9; padding: 15px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
    </style>
</head>
<body>

<div class="container">
    <h2>NHIA API Diagnostic Tool</h2>
    <p style="color: #64748b; margin-bottom: 30px;">Enter a valid 8-digit NHIS number to ping the national database and generate a Claim Check Code (CCC).</p>

    <form method="POST">
        <div class="form-group">
            <input type="text" name="nhis_number" placeholder="e.g. 63183875" value="<?php echo htmlspecialchars($test_nhis_number); ?>" required maxlength="8" pattern="\d{8}">
        </div>
        <button type="submit">Ping NHIA Server</button>
    </form>

    <?php if ($result !== null): ?>
        <div class="result-box <?php echo $result['success'] ? 'success' : 'error'; ?>">
            <h3 style="margin-top:0;"><?php echo $result['success'] ? '✅ Connection Successful' : '❌ Connection Failed'; ?></h3>
            
            <?php if ($result['success']): ?>
                <p style="font-size: 18px;">Generated CCC: <strong><?php echo htmlspecialchars($result['ccc_code']); ?></strong></p>
            <?php else: ?>
                <p><strong>Reason:</strong> <?php echo htmlspecialchars($result['message']); ?></p>
            <?php endif; ?>
            
            <p style="font-size: 13px; color: #475569; margin-bottom: 0;">Response Time: <?php echo $execution_time; ?> seconds.</p>
        </div>

        <h4 style="margin-top: 30px;">Raw Server Payload</h4>
        <pre><?php print_r($result); ?></pre>
    <?php endif; ?>
</div>

</body>
</html>