<?php

require_once 'pdo.php';

// https://bc.game/api/home/provider/list/?categoryId=1

$casino_id = 2;
$name = 'bcgame';

$isLocal = getenv('LOCAL_ENV') === 'true';
$directory = $isLocal ? "/Users/serhiipozynich" : "/var/www";

if ($isLocal) {

    $url = 'https://bc.game/api/home/provider/list/?categoryId=1';

    // Make a cURL request to get the list of providers
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $providerList = curl_exec($ch);
    curl_close($ch);

    // Check for cURL errors
    if (!$providerList) {
        echo 'Failed to retrieve provider list.';
        exit;
    }

    $providers = json_decode($providerList, true)['data'];

    // Create an array to hold data for all providers
    $allProviderData = array();

    // Iterate through each provider and make a cURL request
    foreach ($providers as $provider) {
        $providerName = $provider['providerName'];

        $url = 'https://bc.game/api/home/rec/entry/';
        $data = array(
            'areaCode' => 'UNKOWN',
            'distinctId' => '18a459f7d4a11bd-07aaaaaaaaaaaac-19525634-1764000-18a459f7d4b1e80',
            'pageSize' => 1000,
            'page' => 1,
            'sectionId' => 'casino_slots',
            'gameUnique' => '',
            'providerName' => $providerName, // Use the current providerName
            'sortBy' => 'popular',
            'showingBlocked' => 0
        );

        $data_string = json_encode($data);

        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
            'Content-Type: application/json',
            'Origin: https://bc.game',
            'Content-Length: ' . strlen($data_string)
        ));

        $result = curl_exec($ch);
        curl_close($ch);

        // Store the result in the array with the provider name as the key
        $allProviderData[$providerName] = json_decode($result, true);
    }

    // Save all provider data in a single JSON file
    $file = $directory . '/gamble-free.com.parsing/casinos/'.$name.'.json';
    file_put_contents($file, json_encode($allProviderData));

} else {
    $file = $directory . '/gamble-free.com.parsing/casinos/'.$name.'.json';
}

$games = json_decode(file_get_contents($file), true);

$insertedGames = [];

foreach ($games as $providerName => $providerData) {
    $gamesData = $providerData['data']['pageList']['list'];

    // Iterate through games for each provider
    foreach ($gamesData as $game) {
        $provider_name = $game['providerName'];
        $game_name = $game['fullName'];

        $insertQuery = "INSERT INTO casinos_games (casino_id, game_name, provider_name)
            VALUES (:casino_id, :game_name, :provider_name) ON DUPLICATE KEY UPDATE casino_id = VALUES(casino_id)";

        $stmt = $db->prepare($insertQuery);
        $stmt->bindParam(':casino_id', $casino_id, PDO::PARAM_INT);
        $stmt->bindParam(':game_name', $game_name, PDO::PARAM_STR);
        $stmt->bindParam(':provider_name', $provider_name, PDO::PARAM_STR);
        $stmt->execute();

        $insertedGames[] = array(
            'game_name' => $game_name,
            'provider_name' => $provider_name
        );

        $count = count($insertedGames);
    }
}

// curl -X POST 'https://bc.game/api/home/rec/entry/' -d '{"areaCode":"UNKOWN","distinctId":"18a459f7d4a11bd-07aaaaaaaaaaaac-19525634-1764000-18a459f7d4b1e80","pageSize":100,"page":1,"sectionId":"casino_slots","gameUnique":"","providerName":"PG Soft","sortBy":"popular","showingBlocked":0}' -H 'content-type: application/json' -H 'Origin: https://bc.game'

echo json_encode(['insertedGames' => $insertedGames, 'count' => $count], JSON_PRETTY_PRINT);
?>
