<?php
class NhisApiHelper {
    private $accountId;
    private $password;
    private $apiUrl;            // Fixed: Explicitly declared for PHP 8.2+ strict standards
    private $isSimulationMode;  // Fixed: Explicitly declared

    public function __construct() {
        $this->accountId = 'AP1258'; 
        $this->password  = 'b?q5JGlWBc8y3hZomFS26tgrP';
        
        // 🟢 SET THIS TO FALSE WHEN YOU GET THE REAL NHIA URL
        $this->isSimulationMode = true; 
        $this->apiUrl = 'PENDING_REAL_URL_FROM_NHIA'; 
    }

    public function fetchCccCode($patientNhisNumber) {
        
        // ==========================================
        // SIMULATION MODE (For Development)
        // ==========================================
        if ($this->isSimulationMode) {
            // Simulate network latency (1.5 seconds)
            usleep(1500000); 
            
            // Validate the 8-digit rule
            if (!preg_match('/^\d{8}$/', $patientNhisNumber)) {
                return [
                    'success' => false,
                    'message' => 'NHIA API Error (HTTP 400): Invalid NHIS number format. Must be 8 digits.'
                ];
            }

            // Generate a strictly 5-digit realistic CCC Code
            $mockCcc = (string) mt_rand(10000, 99999);
            
            return [
                'success' => true,
                'ccc_code' => $mockCcc,
                'message' => 'SIMULATION: CCC generated successfully.'
            ];
        }

        // ==========================================
        // LIVE PRODUCTION MODE (Use later)
        // ==========================================
        $payload = json_encode([
            'nhis_number' => $patientNhisNumber,
            'timestamp'   => date('Y-m-d\TH:i:sP')
        ]);

        $ch = curl_init($this->apiUrl);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/json',
            'Accept: application/json',
            'Authorization: Basic ' . base64_encode($this->accountId . ':' . $this->password)
        ]);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);

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

        if ($response === false) {
            return ['success' => false, 'message' => 'cURL Network Error: ' . $curlError];
        }

        $data = json_decode($response, true);

        if ($httpCode >= 200 && $httpCode < 300) {
            return [
                'success' => true,
                'ccc_code' => $data['ccc_code'] ?? $data['CCC'] ?? null, 
                'message' => 'CCC generated successfully.'
            ];
        } else {
            $apiMessage = $data['message'] ?? 'Unknown API Error';
            return ['success' => false, 'message' => "NHIA API Error (HTTP {$httpCode}): {$apiMessage}"];
        }
    }
}
?>