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

require_once __DIR__ . '/../koneksi.php';
require_once __DIR__ . '/../classes/APICONFIG.php';

$SGX = new APICONFIG(); // WAJIB
/*
|--------------------------------------------------------------------------
| AUTO PROVIDER LIST
|--------------------------------------------------------------------------
*/
$providers = [ 
    'EVOPLAY'
];

echo "<h2>🚀 IMPORT GAME KE tb_gamelist</h2>";

foreach ($providers as $provider) {

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

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

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

    $games = $res['games'] ?? $res['data'] ?? $res['game_list'] ?? [];

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

    foreach ($games as $game) {

        // ===============================
        // DATA DASAR
        // ===============================
        $game_code = mysqli_real_escape_string($koneksi, $game['game_code'] ?? '');
        $game_name = mysqli_real_escape_string($koneksi, $game['game_name'] ?? '');

        if (empty($game_code)) continue;

        // ===============================
        // 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;
                        }
                    }
                }
            }
        }

        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);

        // ===============================
        // TYPE MAPPING (INTI PERUBAHAN)
        // ===============================
        if ($provider == 'PP_LIVE_PRO') {
            $gametypeid = 'LC';
            $category   = 'LC';
            $datatype   = 'LC';
        } else {
            $gametypeid = 'SL';
            $category   = 'SL';
            $datatype   = 'SL';
        }

        // ===============================
        // DEFAULT VALUE
        // ===============================
        $technology     = 'html5';
        $platform       = 'MOBILE,DOWNLOAD,WEB';
        $demogame       = '1';
        $aspectratio    = '16:9';
        $technologyid   = 'H5';
        $jurisdictions  = '1';
        $frbavailable   = '1';

        // ===============================
        // CEK DUPLIKAT
        // ===============================
        $cek = mysqli_query($koneksi, "SELECT cuid FROM tb_gamelist WHERE gameid='$game_code'");

        if (mysqli_num_rows($cek) > 0) {

            // UPDATE
            mysqli_query($koneksi, "
                UPDATE tb_gamelist SET 
                    provider='$provider',
                    image='$image',
                    gamename='$game_name',
                    gametypeid='$gametypeid',
                    category='$category',
                    technology='$technology',
                    platform='$platform',
                    demogame='$demogame',
                    aspectratio='$aspectratio',
                    technologyid='$technologyid',
                    jurisdictions='$jurisdictions',
                    frbavailable='$frbavailable',
                    datatype='$datatype'
                WHERE gameid='$game_code'
            ");

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

        } else {

            // INSERT
            mysqli_query($koneksi, "
                INSERT INTO tb_gamelist (
                    provider,
                    image,
                    gameidnumeric,
                    gameid,
                    gamename,
                    gametypeid,
                    category,
                    technology,
                    platform,
                    demogame,
                    aspectratio,
                    technologyid,
                    jurisdictions,
                    frbavailable,
                    datatype,
                    features
                ) VALUES (
                    '$provider',
                    '$image',
                    '$game_code',
                    '$game_code',
                    '$game_name',
                    '$gametypeid',
                    '$category',
                    '$technology',
                    '$platform',
                    '$demogame',
                    '$aspectratio',
                    '$technologyid',
                    '$jurisdictions',
                    '$frbavailable',
                    '$datatype',
                    ''
                )
            ");

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

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