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

require_once __DIR__ . '/../main/API/functions.php';
require_once __DIR__ . '/../function/connect.php';

/*
|--------------------------------------------------------------------------
| LIST PROVIDER
|--------------------------------------------------------------------------
*/
$providers = [
    'CMDSPORTSBOOK',
    '',
    '',
    '',
    ''
];

echo "<h2>🚀 IMPORT SEMUA GAME API</h2>";

foreach ($providers as $provider) {

    echo "<h3>📦 Provider: $provider</h3>";

    $res = $SGX->gamelist($provider);

    // ===============================
    // CEK RESPONSE
    // ===============================
    if (!$res || !isset($res['status']) || $res['status'] != 1) {
        echo "❌ Gagal ambil data dari API<br>";
        continue;
    }

    // ===============================
    // FLEXIBLE ARRAY (SEMUA KEMUNGKINAN)
    // ===============================
    $games = 
        $res['games'] 
        ?? $res['data'] 
        ?? $res['game_list'] 
        ?? $res['provider_list']
        ?? $res['sports']
        ?? $res['list']
        ?? [];

    if (empty($games)) {
        echo "⚠️ Tidak ada data game<br>";
        continue;
    }

    /*
    |--------------------------------------------------------------------------
    | DEBUG (AKTIFKAN JIKA MAU CEK STRUKTUR)
    |--------------------------------------------------------------------------
    */
    /*
    if ($provider == 'IBC') {
        echo "<pre>";
        print_r($res);
        die;
    }
    */

    foreach ($games as $game) {

        // ===============================
        // SUPPORT SPORTBOOK (NO GAME CODE)
        // ===============================
        $game_code = $game['game_code'] 
            ?? $game['code'] 
            ?? $game['provider_code'] 
            ?? $provider;

        $game_name = $game['game_name'] 
            ?? $game['name'] 
            ?? $game['provider_name'] 
            ?? $provider;

        if (empty($game_code)) {
            $game_code = $provider;
        }

        if (empty($game_name)) {
            $game_name = $provider;
        }

        $game_code = mysqli_real_escape_string($koneksi, $game_code);
        $game_name = mysqli_real_escape_string($koneksi, $game_name);

        // ===============================
        // GAMBAR
        // ===============================
        $image = '';

        $possibleKeys = ['image', 'img', 'icon', 'game_icon', 'thumbnail'];

        foreach ($possibleKeys as $key) {
            if (isset($game[$key])) {

                if (is_string($game[$key]) && !empty($game[$key])) {
                    $image = $game[$key];
                    break;
                }

                if (is_array($game[$key])) {
                    foreach ($game[$key] as $imgVal) {
                        if (is_string($imgVal) && !empty($imgVal)) {
                            $image = $imgVal;
                            break 2;
                        }
                    }
                }
            }
        }

        // fallback scan semua field
        if (empty($image)) {
            foreach ($game as $val) {

                if (is_array($val)) {
                    foreach ($val as $subVal) {
                        if (is_string($subVal) && strpos($subVal, 'http') !== false) {
                            $image = $subVal;
                            break 2;
                        }
                    }
                }

                if (is_string($val) && strpos($val, 'http') !== false) {
                    $image = $val;
                    break;
                }
            }
        }

        $image = mysqli_real_escape_string($koneksi, $image);

        // ===============================
        // GAME TYPE
        // ===============================
        if (in_array($provider, ['M9BET','IBC','FBSPORT','DIGITAIN','CMDSPORTSBOOK'])) {
            $game_type = 'sportbook';
        } else {
            $game_type = $game['game_type'] 
                ?? $game['gameType'] 
                ?? $game['type'] 
                ?? 'slot';
        }

        $game_type = mysqli_real_escape_string($koneksi, $game_type);

        // ===============================
        // STATUS
        // ===============================
        $status = 'active';

        // ===============================
        // CEK DUPLIKAT
        // ===============================
        $cek = mysqli_query($koneksi, "SELECT id FROM games WHERE game_code='$game_code'");

        if (mysqli_num_rows($cek) > 0) {

            mysqli_query($koneksi, "
                UPDATE games SET 
                    game_name='$game_name',
                    images='$image',
                    provider='$provider',
                    game_type='$game_type',
                    status_game='$status'
                WHERE game_code='$game_code'
            ");

            echo "♻️ Update: $game_name <br>";

        } else {

            mysqli_query($koneksi, "
                INSERT INTO games 
                (game_code, game_name, images, provider, game_type, status_game)
                VALUES
                ('$game_code','$game_name','$image','$provider','$game_type','$status')
            ");

            echo "✅ Insert: $game_name <br>";
        }
    }
}

echo "<br><b>✅ SELESAI IMPORT GAME</b>";