#!/usr/bin/env nu

# Run complete navigation test suite
#
# Usage: nu full-test-suite.nu

def main [] {
    let base_url = $env.BASE_URL? | default "http://localhost:3030"
    let script_dir = $env.FILE_PWD

    print "🧭 Running complete navigation test suite..."
    print $"🌐 Base URL: ($base_url)"
    print ""

    # Check if server is running
    let health_check = try { http get $"($base_url)/health" } catch { null }
    if ($health_check | is-empty) {
        print "❌ Server not running at ($base_url)"
        print "💡 Start server with: just dev::with-nav-test"
        exit 1
    }

    # Test tracking
    mut tests_passed = 0
    mut tests_failed = 0
    mut total_tests = 0

    # Function to run test and track results
    def run_test [test_name: string, test_command: closure] {
        print $"🧪 Running: ($test_name)"
        $total_tests += 1

        let result = try {
            do $test_command
            true
        } catch {
            false
        }

        if $result {
            $tests_passed += 1
            print $"✅ PASSED: ($test_name)"
        } else {
            $tests_failed += 1
            print $"❌ FAILED: ($test_name)"
        }
        print ""
    }

    # 1. Validate all routes first
    run_test "Route Validation" {
        nu ($script_dir + "/validate-all-routes.nu")
    }

    # 2. Test individual route resolution
    print "📋 Testing individual routes..."
    run_test "Home Route (/)" {
        nu ($script_dir + "/test-single-route.nu") "/"
    }

    run_test "Services Route (/services)" {
        nu ($script_dir + "/test-single-route.nu") "/services"
    }

    # Check if Spanish routes exist
    let spanish_check = try {
        let resp = http get $"($base_url)/api/nav-test/resolve?path=/es"
        $resp.valid
    } catch { false }

    if $spanish_check {
        run_test "Spanish Home (/es)" {
            nu ($script_dir + "/test-single-route.nu") "/es"
        }
        run_test "Spanish Contact (/es/contactar)" {
            nu ($script_dir + "/test-single-route.nu") "/es/contactar"
        }
    }

    # 3. Test navigation sequences
    print "📋 Testing navigation sequences..."
    run_test "Home to Services" {
        nu ($script_dir + "/test-navigation-sequence.nu") "/" "/services"
    }

    run_test "Services to Contact" {
        nu ($script_dir + "/test-navigation-sequence.nu") "/services" "/contact"
    }

    # Test cross-language navigation if Spanish routes exist
    if $spanish_check {
        run_test "English to Spanish" {
            nu ($script_dir + "/test-navigation-sequence.nu") "/" "/es"
        }
    }

    # 4. Performance benchmarks
    print "📋 Running performance benchmarks..."
    run_test "Home Route Benchmark" {
        nu ($script_dir + "/benchmark-route.nu") "/" 100
    }

    let services_check = try {
        let resp = http get $"($base_url)/api/nav-test/resolve?path=/services"
        $resp.valid
    } catch { false }

    if $services_check {
        run_test "Services Route Benchmark" {
            nu ($script_dir + "/benchmark-route.nu") "/services" 100
        }
    }

    # 5. Final summary
    print "================================"
    print "🧭 Navigation Test Suite Results"
    print "================================"
    print $"Total Tests: ($total_tests)"
    print $"Passed: ($tests_passed)"
    print $"Failed: ($tests_failed)"

    if $tests_failed == 0 {
        print "🎉 All tests passed!"
        exit 0
    } else {
        print $"❌ ($tests_failed) tests failed"
        print ""
        print "💡 Check individual test output above for details"
        print "💡 Verify your route configuration in site/config/routes/"
        exit 1
    }
}
