<?php

$casino_id = 3;
$name = 'm88';

$isLocal = getenv('LOCAL_ENV') === 'true';
$isParsing = getenv('PARSING_ENV') === 'true';
$isProd = getenv('PROD_ENV') === 'true';

$directory = $isLocal ? "/Users/serhiipozynich" : "/var/www";

$proxyHost = '';
$proxyPort = '';

// Parse command line arguments
$args = getopt(null, ["proxy:"]);
if ($isParsing && isset($args['proxy'])) {
    list($proxyHost, $proxyPort) = explode(':', $args['proxy']);
}

if ($isParsing || $isLocal) {
    $url = 'https://www.m88.com/casino/data/22/1/en-us';

    $ch = curl_init($url);
    if (!empty($proxyHost) && !empty($proxyPort)) {
        curl_setopt($ch, CURLOPT_PROXY, $proxyHost);
        curl_setopt($ch, CURLOPT_PROXYPORT, $proxyPort);
        curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);
    }
    $userAgent = "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Mobile Safari/537.36";
    curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'
    ));

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

    $file = $directory . '/gamble-free.com.parsing/casinos/' . $name . '.json';
    file_put_contents($file, $result);
} else {
    $file = $directory . '/gamble-free.com.parsing/casinos/' . $name . '.json';
}

$games = json_decode(file_get_contents($file), true)['LobbyData']['Games'];

$insertedGames = [];

if ($isLocal || $isProd) {
    require_once 'pdo.php'; // Include your database connection settings

    // Prepare the INSERT query outside the loop
    $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);

    foreach ($games as $game) {
        $provider_name = $game['ProductName'];
        $game_name = str_replace("™", "", $game['Name']);

        // Execute the prepared statement in the loop
        $stmt->execute();

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

    // Count the number of inserted games
    $count = count($insertedGames);
} else {
    // No database operations in non-local and non-production environments
    foreach ($games as $game) {
        $provider_name = $game["ProductName"];
        $game_name = $game['Name'];
        $game_name = str_replace("™", "", $game_name);

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

// Output the result as JSON
echo json_encode(['insertedGames' => $insertedGames, 'count' => $count], JSON_PRETTY_PRINT);
?>
