#!/usr/bin/env nu
# Run All Extension System Tests

# Run all test suites
export def main [
    --verbose (-v)      # Verbose output
    --suite: string = "all"  # Test suite to run (all, oci, cache, versions, discovery)
] {
    print "🧪 Extension Loading System Test Suite"
    print ""
    print $"Running tests: ($suite)"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print ""

    let test_dir = ($env.FILE_PWD)

    mut passed = 0
    mut failed = 0
    mut skipped = 0

    # OCI Client Tests
    if $suite == "all" or $suite == "oci" {
        print "📦 Running OCI Client Tests"
        print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

        let result = (do { nu $"($test_dir)/test_oci_client.nu" } | complete)

        if $result.exit_code == 0 {
            $passed = $passed + 1
            print $result.stdout
        } else {
            $failed = $failed + 1
            print "❌ OCI Client tests failed"
            print $result.stderr
        }

        print ""
    }

    # Cache Tests
    if $suite == "all" or $suite == "cache" {
        print "💾 Running Cache Tests"
        print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

        let result = (do { nu $"($test_dir)/test_cache.nu" } | complete)

        if $result.exit_code == 0 {
            $passed = $passed + 1
            print $result.stdout
        } else {
            $failed = $failed + 1
            print "❌ Cache tests failed"
            print $result.stderr
        }

        print ""
    }

    # Version Tests
    if $suite == "all" or $suite == "versions" {
        print "🏷️  Running Version Resolution Tests"
        print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

        let result = (do { nu $"($test_dir)/test_versions.nu" } | complete)

        if $result.exit_code == 0 {
            $passed = $passed + 1
            print $result.stdout
        } else {
            $failed = $failed + 1
            print "❌ Version tests failed"
            print $result.stderr
        }

        print ""
    }

    # Discovery Tests
    if $suite == "all" or $suite == "discovery" {
        print "🔍 Running Discovery Tests"
        print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

        let result = (do { nu $"($test_dir)/test_discovery.nu" } | complete)

        if $result.exit_code == 0 {
            $passed = $passed + 1
            print $result.stdout
        } else {
            $failed = $failed + 1
            print "❌ Discovery tests failed"
            print $result.stderr
        }

        print ""
    }

    # Summary
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print "📊 Test Summary"
    print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    print $"   ✅ Passed: ($passed)"
    print $"   ❌ Failed: ($failed)"
    print $"   ⏭️  Skipped: ($skipped)"
    print ""

    if $failed == 0 {
        print "🎉 All tests passed!"
        exit 0
    } else {
        print $"⚠️  ($failed) test suite\(s) failed"
        exit 1
    }
}
