<?php
/**
 * get_vendor_games.php
 * "Slot" Category-তে কোনো Vendor (JILI, PG ইত্যাদি)-এ Click করলে,
 * সেই Vendor-এর আসল, সম্পূর্ণ Game-তালিকা (data/*.json ফাইল থেকে) দেয়।
 */
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');

$dataDir = __DIR__ . '/deepanshu/api/webapi/data/';

// ✅ Vendor-Code (যেমন JILI, PG) থেকে আসল JSON File-নামে যাওয়ার Map
$vendorFileMap = [
    'CQ9' => 'cq9.json',
    'JDB' => 'jdb.json',
    'JILI' => 'jili.json',
    'PG' => 'pgsoft.json',
    'KM' => 'km.json',
    'EG' => 'eg.json',
    'IDG' => 'idg.json',
    'V8' => 'v8.json',
    'MG' => 'mg.json',
    'MG_FISH' => 'mg_fish.json',
    'EVOLIVE' => 'evolive.json',
    'EVO' => 'evo.json',
    'EVO_ELECTRONIC' => 'evo.json',
    'G9' => 'g9.json',
    '5G' => '5g.json',
    '2J' => '2j.json',
    'BNG' => 'bng.json',
    'BTG' => 'btg.json',
    '100HP' => '100hp.json',
    'EAZYGAME' => 'eazygame.json',
    'FASTSPIN' => 'fastspin.json',
    'INOUT' => 'inout.json',
    'BGAMING' => 'bgaming.json',
    'TURBO' => 'turbo.json',
    '5GGAMING' => '5g.json',
    'CARD365' => 'card365.json',
    'PP' => 'pp.json',
    'PRAGMATIC' => 'PRAGMATIC.json',
];

$requestedVendor = strtoupper(trim($_GET['vendor'] ?? ''));

if ($requestedVendor === '' || !isset($vendorFileMap[$requestedVendor])) {
    echo json_encode(['code' => 1, 'msg' => 'Vendor খুঁজে পাওয়া যায়নি', 'games' => []]);
    exit;
}

$filePath = $dataDir . $vendorFileMap[$requestedVendor];

if (!file_exists($filePath)) {
    echo json_encode(['code' => 1, 'msg' => 'Data File পাওয়া যায়নি', 'games' => []]);
    exit;
}

$jsonData = json_decode(file_get_contents($filePath), true);
// ✅ ঠিক করা হলো — bgaming.json-এর মতো "data" Wrapper-এর
// ভেতরে থাকা gameLists-ও Support করা হচ্ছে
$games = $jsonData['data']['gameLists'] ?? $jsonData['gameLists'] ?? $jsonData ?? [];

// ✅ Pagination
$page = intval($_GET['page'] ?? 1);
$limit = intval($_GET['limit'] ?? 30);
$offset = ($page - 1) * $limit;
$pagedGames = array_slice($games, $offset, $limit);

echo json_encode([
    'code' => 0,
    'games' => $pagedGames,
    'total' => count($games),
    'page' => $page,
    'has_more' => ($offset + $limit) < count($games)
]);
