<?php
error_reporting(E_ALL);
ini_set('display_errors', 0);

header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');

$endpoint = 'result';
if (isset($_GET['tab']) && $_GET['tab'] != '') {
    $endpoint = $_GET['tab']; 
} elseif (isset($_GET['type']) && $_GET['type'] === 'prediksi') {
    $endpoint = 'prediksi'; 
}

$query_string = http_build_query($_GET);

$base_url = "https://blog9.inatips.com/client/blog/";
$url = $base_url . $endpoint . "/json?" . $query_string;

// 4. Eksekusi cURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36');

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

if ($response === false || $http_code != 200) {
    echo json_encode([
        "error" => true, 
        "message" => "Gagal mengambil data dari server asli.", 
        "http_code" => $http_code,
        "curl_error" => $curl_error,
        "url_target" => $url
    ]);
    exit;
}

$response = str_replace('\/storage\/', 'https:\/\/blog9.inatips.com\/storage\/', $response);

$response = str_replace('http:\/\/blog9.inatips.com\/client\/blog\/bukuMimpi\/json', 'http:\/\/localhost\/TOTO\/2\/proxy.php?tab=bukuMimpi', $response);
$response = str_replace('https:\/\/blog9.inatips.com\/client\/blog\/bukuMimpi\/json', 'http:\/\/localhost\/TOTO\/2\/proxy.php?tab=bukuMimpi', $response);

echo $response;
?>