<?php

$casino_id = 39;
$name = 'zotabet';

$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.zotabet.com/api/games/allowed_desktop';

    $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);
    }
    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'cookie: referral_params=eJxLSS3LTE61TUktzi7JLwAAKbQFqA%3D%3D; _ga=GA1.1.1870039292.1661777214; _user_visited_app=true; _hjSessionUser_2826603=eyJpZCI6ImY2MjllY2Y5LWYwZTYtNTE3YS04M2M3LTA2YzZjZDQ5OTBlYSIsImNyZWF0ZWQiOjE2NjE3NzcyMTQwMjQsImV4aXN0aW5nIjp0cnVlfQ==; locale=ImVuIg%3D%3D--7afc96cdc8f9acc7f6504777fcf3905308eee208; _cookies_accepted=true; _hjIncludedInSessionSample=0; _hjSession_2826603=eyJpZCI6IjdkMTNiM2FhLTQwMjEtNGRhZS1hMGM2LWNlYjU1MTg5YWNkNyIsImNyZWF0ZWQiOjE2NjIwMzI5Nzc3MTIsImluU2FtcGxlIjpmYWxzZX0=; _hjIncludedInPageviewSample=1; _hjAbsoluteSessionInProgress=0; _ga_BKYZGY1PB5=GS1.1.1662032974.4.1.1662032985.0.0.0; _dd_s=rum=2&id=2d4e1dad-8ff0-436d-987d-d1a48775118c&created=1662032960831&expire=1662033887327'
    ));

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

$insertedGames = [];

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

    foreach ($games as $game) {
        $provider_name = $game["provider"];
        $game_name = $game['title'];

        $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);
} else {

    foreach ($games as $game) {
        $provider_name = $game["provider"];
        $game_name = $game['title'];

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

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