<?php

namespace Sipago\WooCommerce\SipagoCheckout\ClientSipago;
use Sipago\WooCommerce\SipagoCheckout\Helpers\LogHandler;

class SipagoOrderRest{

    const GET_ORDER = 'https://api.sipago.coop/api/v2/orders/';

    /**
     * @var LogHandler
     */
    protected $log;

    public function getOrder($uuid){
        $this->log = new LogHandler();
        $this->log->logInfo('getOrder ');
        
        $url = static::GET_ORDER . $uuid;
        $this->log->logInfo('Request URL: ' . $url);
        
        // Initialize cURL session
        $ch = curl_init();
        
        try {
            // Set cURL options
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HEADER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 30);
            curl_setopt($ch, CURLOPT_HTTPGET, true);
            
            // Execute the request
            $response = curl_exec($ch);
            
            // Check for errors
            if ($response === false) {
                $error = curl_error($ch);
                $this->log->logInfo('cURL error: ' . $error);
                $result = [
                    'error' => true,
                    'status' => null,
                    'message' => $error
                ];
                curl_close($ch);
                return $result;
            }
            
            // Get HTTP status code
            $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            $this->log->logInfo('$status ' . $status);
            
            curl_close($ch);
            
            if ($status == 200) {
                $this->log->logInfo('$status 200 ' . $status);
                $dataRaw = $response;
                $responseData = json_decode($dataRaw, true);
                
                // Validate JSON decoding
                if (json_last_error() !== JSON_ERROR_NONE) {
                    $this->log->logInfo('JSON decode error: ' . json_last_error_msg());
                    $result = [
                        'error' => true,
                        'status' => $status,
                        'message' => 'Invalid JSON response'
                    ];
                    return $result;
                }
                
                $result = [
                    'error' => false,
                    'status' => $status,
                    'link' => $responseData['data']['attributes']['links']['checkout'] ?? null,
                    'uuid' => $responseData['data']['attributes']['uuid'] ?? null,
                    'amount' => $responseData['data']['attributes']['price']['amount'] ?? null,
                    'orderNumber' => $responseData['data']['attributes']['orderNumber'] ?? null,
                    'voucherNumber' => $responseData['data']['attributes']['voucherNumber'] ?? null,
                    'statusSipago' => $responseData['data']['attributes']['status'] ?? null,
                    'externalData' => $responseData['data']['attributes']['externalData'] ?? null,
                    'subsidiaryId' => $responseData['data']['attributes']['subsidiaryId'] ?? null,
                    'raw' => $dataRaw
                ];
                
                $this->log->logInfo('dat ' . ($result['link'] ?? 'no link'));
                
                return $result;
            }
            
            $result = [
                'error' => true,
                'status' => $status,
                'message' => 'HTTP request failed with status ' . $status
            ];
            return $result;
            
        } catch (Exception $e) {
            $this->log->logInfo('error' . $e->getMessage());
            if (is_resource($ch)) {
                curl_close($ch);
            }
            $result = [
                'error' => true,
                'status' => null,
                'message' => $e->getMessage()
            ];
            return $result;
        }
    }
}