#!/usr/bin/env nu

# Validate all configured routes
#
# Usage: nu validate-all-routes.nu

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

    print "🧭 Validating all configured routes..."
    print $"🌐 Base URL: ($base_url)"

    # 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
    }

    # Validate all routes
    print "🔍 Running route validation..."
    let response = try {
        http get $"($base_url)/api/nav-test/validate-all"
    } catch { |e|
        print $"❌ Route validation failed: ($e.msg)"
        exit 1
    }

    print "✅ Route validation completed"

    # Display validation summary
    print "📊 Validation Summary:"
    print $"  Total Routes: ($response.total_routes)"
    print $"  Valid Routes: ($response.valid_routes)"
    print $"  Invalid Routes: ($response.invalid_routes)"
    print $"  Validation Time: ($response.validation_time_ms)ms"

    # Show invalid routes if any
    if $response.invalid_routes > 0 {
        print ""
        print "❌ Invalid Routes:"
        $response.details
        | where valid == false
        | each { |route| print $"  - ($route.path) \(($route.language)\): ($route.error | default 'Unknown error')" }
        print ""
        exit 1
    }

    # Show valid routes summary
    if $response.valid_routes > 0 {
        print ""
        print "✅ Valid Routes:"
        $response.details
        | where valid == true
        | each { |route| print $"  - ($route.path) → ($route.component | default 'None') \(($route.language)\)" }
    }

    # Calculate success rate
    if $response.total_routes > 0 {
        let success_rate = ($response.valid_routes * 100 // $response.total_routes)
        print ""
        print $"🎯 Success Rate: ($success_rate)%"

        if $success_rate == 100 {
            print "🎉 All routes are valid!"
        } else if $success_rate >= 90 {
            print "✅ Most routes are valid"
        } else {
            print "⚠️  Many routes need attention"
        }
    }

    print ""
    print "🎉 Route validation completed successfully"
}
