#!/bin/bash

# WiseTLP Demo Configuration Script
# This script demonstrates WiseTLP configuration with Groq API

set -e

echo "WiseTLP Demo Configuration"
echo "=========================="
echo ""

# Check if running as root
if [[ $EUID -eq 0 ]]; then
   echo "This demo should not be run as root initially."
   echo "WiseTLP will request elevation when needed."
   exit 1
fi

# Check if autotlp binary exists
if [[ ! -f "./autotlp" ]]; then
    echo "Building WiseTLP..."
    go build -o autotlp ./cmd/autotlp
fi

echo "Demo Configuration Settings:"
echo "- AI Provider: Groq"
echo "- API Endpoint: https://api.groq.com/openai/v1/chat/completions"
echo "- Model: openai/gpt-oss-20b"
echo "- Power Profile: Balanced"
echo "- Use Case: Development"
echo ""

# Create a demo configuration file
cat > demo_responses.txt << 'EOF'
1
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
a
b
a
a
h
y
y
EOF

echo "Starting WiseTLP demo with pre-configured responses..."
echo "Note: This demo uses automated responses for demonstration purposes."
echo ""

# Run WiseTLP with demo responses
# Note: In a real scenario, this would be interactive
echo "In a real scenario, WiseTLP would:"
echo "1. Detect your system specifications"
echo "2. Check TLP installation status"
echo "3. Configure AI service (Groq in this case)"
echo "4. Gather your power management preferences"
echo "5. Generate optimized TLP configuration using AI"
echo "6. Present the configuration for your approval"
echo "7. Apply the configuration to your system"
echo ""

echo "Example system detection output:"
echo "--------------------------------"
echo "Distribution: $(lsb_release -si 2>/dev/null || echo "Unknown")"
echo "Architecture: $(uname -m)"
echo "Kernel: $(uname -r)"
echo "CPU: $(grep -m1 'model name' /proc/cpuinfo 2>/dev/null | cut -d: -f2 | xargs || echo "Unknown")"
echo "Memory: $(free -h | grep '^Mem:' | awk '{print $2}' || echo "Unknown")"

if [[ -d "/sys/class/power_supply/BAT0" ]]; then
    echo "Battery: Present"
else
    echo "Battery: Not detected (desktop system)"
fi

echo ""
echo "Example TLP configuration that would be generated:"
echo "------------------------------------------------"
cat << 'EOF'
# WiseTLP Generated Configuration
# Generated for Development workstation
# Power Profile: Balanced, Use Case: Development

TLP_ENABLE=1
TLP_WARN_LEVEL=3

# CPU scaling for development workloads
CPU_SCALING_GOVERNOR_ON_AC=ondemand
CPU_SCALING_GOVERNOR_ON_BAT=powersave

# Platform profiles
PLATFORM_PROFILE_ON_AC=balanced
PLATFORM_PROFILE_ON_BAT=low-power

# Disk management for development I/O
DISK_APM_LEVEL_ON_AC=254
DISK_APM_LEVEL_ON_BAT=192

# Network optimization
WIFI_PWR_ON_AC=off
WIFI_PWR_ON_BAT=on

# USB device management
USB_AUTOSUSPEND=1
USB_EXCLUDE_AUDIO=1

# Audio power management
SOUND_POWER_SAVE_ON_AC=1
SOUND_POWER_SAVE_ON_BAT=10
EOF

echo ""
echo "To run the actual WiseTLP application:"
echo "  ./autotlp"
echo ""
echo "For testing without system changes:"
echo "  ./autotlp --dry-run"
echo ""
echo "Demo completed! The actual WiseTLP provides:"
echo "- Interactive system detection"
echo "- AI-powered configuration generation"
echo "- Secure API key management"
echo "- Safe configuration validation"
echo "- Automatic TLP installation and setup"

# Cleanup
rm -f demo_responses.txt

echo ""
echo "Visit the examples/ directory for more configuration samples:"
echo "- examples/gaming-laptop.md"
echo "- examples/development-workstation.md"
echo ""
echo "For full documentation, see README.md and docs/API.md"
