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

require 'koneksi.php';

/* ===============================
   CONFIG NEXUSGGR ONLY
=============================== */
$NX_URL    = 'https://api.nexusggr.com';
$NX_AGENT  = 'zullhayker';
$NX_TOKEN  = 'cd89fda2b27dfa5f5df63030d358a000';

/* ===============================
   CURL JSON HELPER
=============================== */
function postJson(string $url, array $payload): array
{
    $ch = curl_init($url);
    curl_setopt_array($ch, [
        CURLOPT_POST           => true,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_HTTPHEADER     => ['Content-Type: application/json'],
        CURLOPT_POSTFIELDS     => json_encode($payload),
        CURLOPT_TIMEOUT        => 60
    ]);

    $res = curl_exec($ch);
    if ($res === false) {
        die('CURL ERROR: ' . curl_error($ch) . PHP_EOL);
    }
    curl_close($ch);

    return json_decode($res, true) ?? [];
}

/* ===============================
   INSERT / UPDATE GAME
=============================== */
function insertOrUpdateGame(mysqli $db, array $g): bool
{
    $code   = mysqli_real_escape_string($db, $g['game_code']);
    $name   = mysqli_real_escape_string($db, $g['game_name']);
    $prov   = mysqli_real_escape_string($db, $g['game_provider']);
    $type   = mysqli_real_escape_string($db, strtoupper($g['game_type']));
    $img    = mysqli_real_escape_string($db, $g['game_image'] ?? '');
    $status = (int)$g['game_status'];

    // cek existing game (NEXUSGGR)
    $cek = mysqli_query(
        $db,
        "SELECT id FROM tb_game
         WHERE game_code = '$code'
           AND provider_api = 'NEXUSGGR'
         LIMIT 1"
    );

    if (mysqli_num_rows($cek) > 0) {

        // 🔁 UPDATE provider jadi REELKINGDOM
        mysqli_query(
            $db,
            "UPDATE tb_game
             SET game_provider = '$prov'
             WHERE game_code = '$code'
               AND provider_api = 'NEXUSGGR'"
        );

        return false; // SKIPPED (but updated)
    }

    // INSERT baru
    mysqli_query(
        $db,
        "INSERT INTO tb_game
         (game_code, game_name, game_provider, game_type, game_image, game_status, provider_api)
         VALUES
         ('$code','$name','$prov','$type','$img',$status,'NEXUSGGR')"
    );

    return true; // INSERTED
}

/* ===============================
   SYNC NEXUSGGR GAME LIST
=============================== */
echo "SYNC GAME NEXUSGGR (REELKINGDOM)\n";

$providers = ['REELKINGDOM'];

$inserted = $skipped = 0;

foreach ($providers as $prov) {

    $nx = postJson($NX_URL, [
        'method'        => 'game_list',
        'agent_code'    => $NX_AGENT,
        'agent_token'   => $NX_TOKEN,
        'provider_code' => $prov
    ]);

    if (($nx['status'] ?? 0) != 1) {
        continue;
    }

    foreach ($nx['games'] as $g) {

        $mapped = [
            'game_code'     => $g['game_code'],
            'game_name'     => $g['game_name'],
            'game_provider' => 'REELKINGDOM',
            'game_type'     => 'SL',
            'game_image'    => $g['banner'] ?? '',
            'game_status'   => (int)$g['status']
        ];

        insertOrUpdateGame($call, $mapped)
            ? $inserted++
            : $skipped++;
    }
}

echo "NEXUSGGR INSERTED : $inserted\n";
echo "NEXUSGGR UPDATED  : $skipped (provider fixed)\n";
echo "DONE SYNC REELKINGDOM\n";
