<?php

// Include your database connection file
include 'pdo.php';

// Determine the environment based on the LOCAL_ENV environment variable
$isLocal = getenv('LOCAL_ENV') === 'true';

// Provider details
$name = "Backseatgaming";
$translit = str_replace(" ", "-", $name);

// Define the base directory path based on the environment
$baseDirectory = $isLocal ? "/Users/serhiipozynich" : "/var/www";

// Directory path to create
$directoryPath = $baseDirectory . '/gamble-free.com/images/' . $translit;

try {
    // Execute the SQL query to insert data into the "providers" table
    $db->exec("INSERT INTO providers (name, translit) VALUES ('$name', '$translit')");

    // Get the ID of the inserted provider
    $lastInsertId = $db->lastInsertId();

    // Create the directory if it doesn't exist
    if (!file_exists($directoryPath)) {
        mkdir($directoryPath, 0777, true);
    }

    // Copy the placeholder image to the new provider directory
    $sourceImagePath = $baseDirectory . '/gamble-free.com/images/placeholder.webp';
    $destinationImagePath = $directoryPath . '/'.$translit.'.webp';

    if (copy($sourceImagePath, $destinationImagePath)) {
        // Echo a success message
        echo "$name (id=$lastInsertId) created successfully.\n";
    } else {
        // Handle the case where the copy operation fails
        echo "Error copying placeholder image to $name directory.\n";
    }
} catch (PDOException $e) {
    // Handle any errors if necessary
    // You can add error logging or custom error messages here
    echo "Error creating provider: " . $e->getMessage() . "\n";
    exit(1);
}
?>
