<?php
include "../../conn.php";
include "../../functions2.php";

header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
date_default_timezone_set("Asia/Kolkata");

// Base URL
$BASE = "https://draw.ar-lottery01.com/WinGo";

// typeId → remote file map
$REMOTE = [
    4 => "WinGo_30S.json",
    1 => "WinGo_1M.json",
    2 => "WinGo_3M.json",
    3 => "WinGo_5M.json"
];

$body = json_decode(file_get_contents("php://input"), true);

// Only POST
if ($_SERVER["REQUEST_METHOD"] === "GET") {
    echo json_encode([
        "code" => 405,
        "msg"  => "Method Not Allowed"
    ]);
    exit;
}

// Validate input
if (!isset($body["typeId"]) || !isset($body["language"]) || !isset($body["random"]) || !isset($body["signature"])) {
    echo json_encode([
        "code" => 7,
        "msg"  => "Param Invalid"
    ]);
    exit;
}

// Vars
$typeId = intval($body["typeId"]);
$language = $body["language"];
$random = $body["random"];
$signature = strtoupper($body["signature"]);

// Signature check
$signStr = '{"language":'.$language.',"random":"'.$random.'","typeId":'.$typeId.'}';
$mySign = strtoupper(md5($signStr));

if ($mySign !== $signature) {
    echo json_encode([
        "code" => 5,
        "msg" => "Signature error"
    ]);
    exit;
}

// JWT check
$authHeader = $_SERVER["HTTP_AUTHORIZATION"] ?? "";
$bearer = explode(" ", $authHeader);
$token = $bearer[1] ?? "";

$jwt = json_decode(is_jwt_valid($token), true);
if (!$jwt || $jwt["status"] !== "Success") {
    echo json_encode([
        "code" => 403,
        "msg" => "No permission"
    ]);
    exit;
}

// Validate type
if (!isset($REMOTE[$typeId])) {
    $typeId = 2; // default 1-minute
}

// Build Remote URL
$realUrl = $BASE . "/" . $REMOTE[$typeId] . "?ts=" . (microtime(true) * 1000);

// --- PROXY CONFIG ---
// CHANGE THIS TO YOUR VPS IP WHERE YOU HOSTED vps_proxy.php
$VPS_PROXY = "http://club44.us.cc/wingoapi/vps_proxy.php"; 
// --------------------

$proxyUrl = $VPS_PROXY . "?url=" . urlencode($realUrl);

$ch = curl_init($proxyUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
// No need for strict SSL or UserAgent here since it's connecting to your own VPS
$remote = curl_exec($ch);
curl_close($ch);

if (!$remote) {
    echo json_encode([
        "code" => 500,
        "msg" => "Remote API Down"
    ]);
    exit;
}

$json = json_decode($remote, true);

// Extract
$curr = $json["current"];
$prev = $json["previous"];
$next = $json["next"];

$data = [
    "issueNumber" => $prev["issueNumber"],
    "startTime"   => date("Y-m-d H:i:s", $curr["startTime"] / 1000),
    "endTime"     => date("Y-m-d H:i:s", $curr["endTime"] / 1000),
    "serviceTime" => date("Y-m-d H:i:s"),
    "intervalM"   => ($typeId == 1 ? 0.5 : ($typeId == 2 ? 1 : ($typeId == 3 ? 3 : 5))),  
];

// Response
echo json_encode([
    "code" => 0,
    "msg"  => "Succeed",
    "msgCode" => 0,
    "serviceNowTime" => date("Y-m-d H:i:s"),
    "data" => $data
], JSON_PRETTY_PRINT);

exit;
?>
