#!/bin/bash

# End-to-End Dictation Test Script
# This script tests the complete dictation workflow

echo "=== Dictation Service E2E Test ==="
echo

# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

print_status() {
    if [ $1 -eq 0 ]; then
        echo -e "${GREEN}✓ $2${NC}"
    else
        echo -e "${RED}✗ $2${NC}"
    fi
}

# Test 1: Check service status
echo "1. Checking service status..."
systemctl --user is-active dictation.service >/dev/null 2>&1
print_status $? "Dictation service is running"

systemctl --user is-active keybinding-listener.service >/dev/null 2>&1
print_status $? "Keybinding listener service is running"

# Test 2: Check lock file operations
echo
echo "2. Testing lock file operations..."
cd /mnt/storage/Development/dictation-service

# Clean state
rm -f listening.lock conversation.lock

# Test dictation toggle
/mnt/storage/Development/dictation-service/scripts/toggle-dictation.sh >/dev/null 2>&1
if [ -f listening.lock ]; then
    print_status 0 "Dictation lock file created"
else
    print_status 1 "Dictation lock file not created"
fi

# Toggle off
/mnt/storage/Development/dictation-service/scripts/toggle-dictation.sh >/dev/null 2>&1
if [ ! -f listening.lock ]; then
    print_status 0 "Dictation lock file removed"
else
    print_status 1 "Dictation lock file not removed"
fi

# Test 3: Check service response to lock files
echo
echo "3. Testing service response to lock files..."

# Create dictation lock
touch listening.lock
sleep 2

# Check logs for state change
if grep -q "\[Dictation\] STARTED" /home/universal/.gemini/tmp/428d098e581799ff7817b2001dd545f7b891975897338dd78498cc16582e004f/debug.log; then
    print_status 0 "Service detected dictation lock file"
else
    print_status 1 "Service did not detect dictation lock file"
fi

# Remove lock
rm -f listening.lock
sleep 2

# Test 4: Check keybinding functionality
echo
echo "4. Testing keybinding functionality..."

# Test toggle script directly (simulates keybinding)
touch listening.lock
sleep 1

if [ -f listening.lock ]; then
    print_status 0 "Keybinding simulation works (lock file created)"
else
    print_status 1 "Keybinding simulation failed"
fi

rm -f listening.lock

# Test 5: Check audio processing components
echo
echo "5. Testing audio processing components..."

# Check if audio libraries are available
python3 -c "import sounddevice, vosk" >/dev/null 2>&1
if [ $? -eq 0 ]; then
    print_status 0 "Audio processing libraries available"
else
    print_status 1 "Audio processing libraries not available"
fi

# Check Vosk model
if [ -d "/home/universal/.shared/models/vosk-models/vosk-model-en-us-0.22" ]; then
    print_status 0 "Vosk model directory exists"
else
    print_status 1 "Vosk model directory not found"
fi

# Test 6: Check notification system
echo
echo "6. Testing notification system..."

# Try sending a test notification
notify-send "Test" "Dictation service test notification" >/dev/null 2>&1
if [ $? -eq 0 ]; then
    print_status 0 "Notification system works"
else
    print_status 1 "Notification system failed"
fi

# Test 7: Check keyboard typing
echo
echo "7. Testing keyboard typing..."

# Try to type a test string (this will go to focused window)
/home/universal/.local/bin/uv run python3 -c "
from pynput.keyboard import Controller
import time
k = Controller()
k.type('DICTATION_TEST_STRING')
print('Test string typed')
" >/dev/null 2>&1

if [ $? -eq 0 ]; then
    print_status 0 "Keyboard typing system works"
else
    print_status 1 "Keyboard typing system failed"
fi

echo
echo "=== Test Summary ==="
echo "The dictation service should now be working. Here's how to use it:"
echo
echo "1. Make sure you have a text input field focused (like a terminal, text editor, etc.)"
echo "2. Press Alt+D to start dictation"
echo "3. You should see a notification: '🎤 Dictation Active - Speak now - text will be typed into focused app!'"
echo "4. Speak clearly into your microphone"
echo "5. Text should appear in the focused application"
echo "6. Press Alt+D again to stop dictation"
echo
echo "If text isn't appearing, make sure:"
echo "- Your microphone is working and not muted"
echo "- You have a text input field focused"
echo "- You're speaking clearly at normal volume"
echo "- The microphone isn't picking up too much background noise"
echo
echo "For AI conversation mode, press Super+Alt+D (Windows key + Alt + D)"