# Script to record test audio clips for testing
# Requires ffmpeg to be installed

param(
    [string]$OutputDir = "test-audio",
    [int]$Duration = 5
)

Write-Host "=== Test Audio Recorder ===" -ForegroundColor Cyan
Write-Host ""

# Check for ffmpeg
$ffmpeg = Get-Command ffmpeg -ErrorAction SilentlyContinue
if (!$ffmpeg) {
    Write-Host "[ERROR] ffmpeg not found!" -ForegroundColor Red
    Write-Host "Install ffmpeg from: https://ffmpeg.org/download.html" -ForegroundColor Yellow
    exit 1
}

# Create output directory
if (!(Test-Path $OutputDir)) {
    New-Item -ItemType Directory -Path $OutputDir | Out-Null
    Write-Host "[OK] Created directory: $OutputDir" -ForegroundColor Green
}

# List available audio devices
Write-Host "Available audio devices:" -ForegroundColor Yellow
ffmpeg -list_devices true -f dshow -i dummy 2>&1 | Select-String "DirectShow audio devices"
ffmpeg -list_devices true -f dshow -i dummy 2>&1 | Select-String "\[dshow"

Write-Host ""
Write-Host "====================================" -ForegroundColor Cyan
Write-Host "Recording Test Audio Clips" -ForegroundColor White
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""

# Test cases with expected transcriptions
$testCases = @(
    @{Name="test1"; Text="Hello world"; Prompt="Say: Hello world"},
    @{Name="test2"; Text="One two three four five"; Prompt="Say: One two three four five"},
    @{Name="test3"; Text="The quick brown fox jumps over the lazy dog"; Prompt="Say: The quick brown fox jumps over the lazy dog"},
    @{Name="test4"; Text="Testing microphone switching"; Prompt="Say: Testing microphone switching"},
    @{Name="test5"; Text="This is a longer sentence for testing real time transcription"; Prompt="Say: This is a longer sentence for testing real time transcription"}
)

Write-Host "Enter your microphone name (from list above):" -ForegroundColor Yellow
Write-Host "Example: Microphone (Realtek High Definition Audio)" -ForegroundColor Gray
$MicName = Read-Host "Microphone"

if (!$MicName) {
    Write-Host "[ERROR] No microphone specified!" -ForegroundColor Red
    exit 1
}

Write-Host ""
Write-Host "Recording $($testCases.Count) test clips..." -ForegroundColor Cyan
Write-Host "Duration: $Duration seconds each" -ForegroundColor Gray
Write-Host ""

foreach ($test in $testCases) {
    $outputFile = Join-Path $OutputDir "$($test.Name).wav"
    
    Write-Host "-----------------------------------" -ForegroundColor DarkGray
    Write-Host "Recording: $($test.Name)" -ForegroundColor Yellow
    Write-Host "Expected: $($test.Text)" -ForegroundColor White
    Write-Host $test.Prompt -ForegroundColor Green
    Write-Host ""
    Write-Host "Press ENTER when ready..." -ForegroundColor Yellow
    Read-Host
    
    Write-Host "Recording in 3..." -ForegroundColor Red
    Start-Sleep -Seconds 1
    Write-Host "Recording in 2..." -ForegroundColor Yellow
    Start-Sleep -Seconds 1
    Write-Host "Recording in 1..." -ForegroundColor Green
    Start-Sleep -Seconds 1
    Write-Host "RECORDING NOW! Speak clearly..." -ForegroundColor Green -BackgroundColor Black
    
    # Record audio: 16kHz, mono, WAV format
    $ffmpegArgs = @(
        "-f", "dshow",
        "-i", "audio=`"$MicName`"",
        "-t", "$Duration",
        "-ar", "16000",
        "-ac", "1",
        "-y",
        "`"$outputFile`""
    )
    
    Start-Process -FilePath "ffmpeg" -ArgumentList $ffmpegArgs -Wait -NoNewWindow
    
    if (Test-Path $outputFile) {
        Write-Host "[OK] Saved: $outputFile" -ForegroundColor Green
        
        # Save expected text to companion file
        $textFile = "$outputFile.txt"
        $test.Text | Out-File -FilePath $textFile -Encoding UTF8
    } else {
        Write-Host "[ERROR] Failed to record!" -ForegroundColor Red
    }
    
    Write-Host ""
}

Write-Host "====================================" -ForegroundColor Cyan
Write-Host "Recording Complete!" -ForegroundColor Green
Write-Host "====================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "Test files saved to: $OutputDir" -ForegroundColor White
Write-Host ""
Write-Host "Run tests with:" -ForegroundColor Yellow
Write-Host "  .\build\bin\Release\test-audio.exe" -ForegroundColor Cyan
Write-Host ""







