#!/bin/bash
# Test single route navigation
#
# Usage: ./test-single-route.sh <path>
# Example: ./test-single-route.sh /services

set -e

ROUTE_PATH="${1:-/}"
BASE_URL="${BASE_URL:-http://localhost:3030}"

echo "🧭 Testing navigation to: $ROUTE_PATH"
echo "🌐 Base URL: $BASE_URL"

# Check if server is running
if ! curl -f -s "$BASE_URL/health" > /dev/null 2>&1; then
    echo "❌ Server not running at $BASE_URL"
    echo "💡 Start server with: just dev::with-nav-test"
    exit 1
fi

# Test route resolution
echo "🔍 Testing route resolution..."
response=$(curl -s "$BASE_URL/api/nav-test/resolve?path=$ROUTE_PATH")

if [ $? -eq 0 ]; then
    echo "✅ Route test successful"

    # Parse and display key information
    if command -v jq &> /dev/null; then
        echo "📊 Route Information:"
        echo "$response" | jq -r '
        "  Path: " + .path +
        "\n  Component: " + (.component // "None") +
        "\n  Language: " + .language +
        "\n  Valid: " + (.valid | tostring) +
        "\n  Resolution Time: " + (.resolution_time_ms | tostring) + "ms"
        '

        # Check if route is valid
        is_valid=$(echo "$response" | jq -r '.valid')
        if [ "$is_valid" = "false" ]; then
            echo "⚠️  Warning: Route is not valid"
            exit 1
        fi
    else
        echo "📊 Raw Response:"
        echo "$response"
        echo ""
        echo "💡 Install jq for better output formatting: brew install jq"
    fi
else
    echo "❌ Route test failed"
    exit 1
fi

echo "🎉 Navigation test completed successfully"
