#!/bin/bash

# ╔══════════════════════════════════════════════════════════════╗
# ║   🎵  MetaMuLate v8.1 - Quick Start                          ║
# ║   Starts proxy server and opens the application              ║
# ╚══════════════════════════════════════════════════════════════╝

set -e

# Get script directory (works even if called from elsewhere)
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

echo ""
echo "╔══════════════════════════════════════════════════════════════╗"
echo "║   🎵  MetaMuLate v8.1 - Music Metadata Aggregator            ║"
echo "╚══════════════════════════════════════════════════════════════╝"
echo ""

# Detect platform and select correct binary
detect_binary() {
    if [[ "$OSTYPE" == "darwin"* ]]; then
        # macOS - check architecture
        if [[ $(uname -m) == "arm64" ]]; then
            echo "proxy/bin/metamulate-proxy-mac-arm"
        else
            echo "proxy/bin/metamulate-proxy-mac-intel"
        fi
    elif [[ "$OSTYPE" == "linux"* ]]; then
        echo "proxy/bin/metamulate-proxy-linux"
    else
        echo "proxy/bin/metamulate-proxy"
    fi
}

BINARY=$(detect_binary)

# Check if binary exists
if [ ! -f "$BINARY" ]; then
    echo "❌ Proxy binary not found: $BINARY"
    echo ""
    echo "   Build it first by running:"
    echo "   ./proxy/build/build-proxy.sh"
    echo ""
    exit 1
fi

# Make sure it's executable
chmod +x "$BINARY"

# Check if config exists, copy example if not
if [ ! -f "proxy/proxy-config.json" ]; then
    if [ -f "proxy/proxy-config.example.json" ]; then
        echo "📋 Creating default configuration..."
        cp proxy/proxy-config.example.json proxy/proxy-config.json
    fi
fi

echo "🚀 Starting MetaMuLate Proxy Server..."
echo "   Binary: $BINARY"
echo "   Config: proxy/proxy-config.json"
echo ""

# Start proxy in background with auto mode (no prompts)
cd proxy
"../$BINARY" --config proxy-config.json &
PROXY_PID=$!
cd ..

# Wait a moment for proxy to start
sleep 2

# Check if proxy started successfully
if ! kill -0 $PROXY_PID 2>/dev/null; then
    echo "❌ Proxy failed to start. Check the output above for errors."
    exit 1
fi

echo ""
echo "✅ Proxy server running (PID: $PROXY_PID)"
echo ""

# Open the HTML file in default browser
HTML_FILE="metamulate_v8.1.html"

if [ -f "$HTML_FILE" ]; then
    echo "🌐 Opening MetaMuLate in your browser..."
    
    if [[ "$OSTYPE" == "darwin"* ]]; then
        open "$HTML_FILE"
    elif [[ "$OSTYPE" == "linux"* ]]; then
        xdg-open "$HTML_FILE" 2>/dev/null || sensible-browser "$HTML_FILE" 2>/dev/null || echo "Please open $HTML_FILE manually in your browser"
    else
        echo "Please open $HTML_FILE manually in your browser"
    fi
else
    echo "⚠️  HTML file not found: $HTML_FILE"
fi

echo ""
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "  MetaMuLate is running!"
echo ""
echo "  📡 CORS Proxy:  http://localhost:8080"
echo "  🌐 Application: $HTML_FILE (should be open in browser)"
echo ""
echo "  Press Ctrl+C to stop the proxy server"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo ""

# Wait for proxy process (keeps script running until Ctrl+C)
wait $PROXY_PID
