<?php
/**
 * Utility script to pre-generate/download voice message files
 * Run this once via browser: http://[your-server]/generate_sounds.php
 */

$soundsDir = __DIR__ . '/assets/sounds/';

// Create directory if not exists
if (!file_exists($soundsDir)) {
    mkdir($soundsDir, 0777, true);
}

$messages = [
    'step1' => 'Zidentyfikowano. Uruchamiam weryfikację.',
    'step2' => 'Odpowiedź poprawna. Uruchamiam skan biometryczny.',
    'step4' => 'Dostęp przyznany. Witamy na imprezie.'
];

echo "<h1>Generating Sound Files...</h1><hr>";

foreach ($messages as $key => $text) {
    $filename = $key . '.mp3';
    $filepath = $soundsDir . $filename;

    // Google Translate TTS URL
    $url = "https://translate.google.com/translate_tts?ie=UTF-8&q=" . urlencode($text) . "&tl=pl&client=tw-ob";

    echo "Downloading: <strong>$filename</strong> for message: \"$text\"... ";

    $content = file_get_contents($url);
    if ($content !== false) {
        if (file_put_contents($filepath, $content)) {
            echo "<span style='color:green'>SUCCESS</span><br>";
        } else {
            echo "<span style='color:red'>FAILED TO SAVE</span><br>";
        }
    } else {
        echo "<span style='color:red'>FAILED TO DOWNLOAD (Check server internet connection)</span><br>";
    }
}

echo "<hr><p>Sound generation complete. You can now close this page and use the system offline.</p>";
?>