#!/usr/bin/env nu

# Plugin Test Runner
# Executes all plugin integration tests and generates comprehensive report

# Color codes for output
const COLOR_RESET = "\e[0m"
const COLOR_GREEN = "\e[32m"
const COLOR_YELLOW = "\e[33m"
const COLOR_RED = "\e[31m"
const COLOR_BLUE = "\e[34m"
const COLOR_CYAN = "\e[36m"

# Print colored message
def print_color [color: string, message: string] {
    print $"($color)($message)($COLOR_RESET)"
}

# Print section header
def print_header [title: string] {
    let line = ("=" * 70)
    print ""
    print_color $COLOR_CYAN $line
    print_color $COLOR_CYAN $title
    print_color $COLOR_CYAN $line
    print ""
}

# Print test result
def print_result [test_name: string, success: bool, duration: int] {
    if $success {
        print_color $COLOR_GREEN $"  ✅ ($test_name) \(($duration)ms\)"
    } else {
        print_color $COLOR_RED $"  ❌ ($test_name) \(($duration)ms\)"
    }
}

# Run individual test file
def run_test_file [file_path: string, test_name: string] -> record {
    let start = (date now)

    print_color $COLOR_BLUE $"\n🧪 Running ($test_name)..."

    let result = (do {
        nu $file_path
    } | complete)

    let end = (date now)
    let duration = (($end - $start) | into duration | into int) / 1_000_000

    {
        name: $test_name
        file: $file_path
        success: ($result.exit_code == 0)
        exit_code: $result.exit_code
        duration_ms: $duration
        stdout: $result.stdout
        stderr: $result.stderr
    }
}

# Check prerequisites
def check_prerequisites [] {
    print_header "🔍 Checking Prerequisites"

    # Check Nushell version
    let nu_version = (version | get version)
    print $"  • Nushell version: ($nu_version)"

    # Check if plugin test files exist
    let test_files = [
        "../lib_provisioning/plugins/auth_test.nu"
        "../lib_provisioning/plugins/kms_test.nu"
        "../lib_provisioning/plugins/orchestrator_test.nu"
        "./test_plugin_integration.nu"
    ]

    mut all_exist = true
    for file in $test_files {
        let exists = ($file | path exists)
        if $exists {
            print_color $COLOR_GREEN $"  ✅ Found: ($file)"
        } else {
            print_color $COLOR_RED $"  ❌ Missing: ($file)"
            $all_exist = false
        }
    }

    # Check plugin availability
    let auth_plugin = (which auth | length) > 0
    let kms_plugin = (which kms | length) > 0
    let orch_plugin = (which orch | length) > 0

    print ""
    print "  Plugin Availability:"
    print_color (if $auth_plugin { $COLOR_GREEN } else { $COLOR_YELLOW }) $"    • Auth: ($auth_plugin)"
    print_color (if $kms_plugin { $COLOR_GREEN } else { $COLOR_YELLOW }) $"    • KMS: ($kms_plugin)"
    print_color (if $orch_plugin { $COLOR_GREEN } else { $COLOR_YELLOW }) $"    • Orchestrator: ($orch_plugin)"

    if not ($auth_plugin or $kms_plugin or $orch_plugin) {
        print_color $COLOR_YELLOW "\n  ⚠️  No plugins detected. Tests will use HTTP/SOPS fallback."
    }

    $all_exist
}

# Run all plugin tests
def run_all_tests [] -> list<record> {
    print_header "🚀 Running All Plugin Tests"

    mut results = []

    # Test 1: Authentication Plugin
    $results = ($results | append (run_test_file
        "../lib_provisioning/plugins/auth_test.nu"
        "Authentication Plugin Tests"
    ))

    # Test 2: KMS Plugin
    $results = ($results | append (run_test_file
        "../lib_provisioning/plugins/kms_test.nu"
        "KMS Plugin Tests"
    ))

    # Test 3: Orchestrator Plugin
    $results = ($results | append (run_test_file
        "../lib_provisioning/plugins/orchestrator_test.nu"
        "Orchestrator Plugin Tests"
    ))

    # Test 4: Integration Tests
    $results = ($results | append (run_test_file
        "./test_plugin_integration.nu"
        "Plugin Integration Tests"
    ))

    $results
}

# Generate test report
def generate_report [results: list<record>] {
    print_header "📊 Test Report"

    # Summary statistics
    let total = ($results | length)
    let passed = ($results | where success == true | length)
    let failed = ($results | where success == false | length)
    let total_duration = ($results | get duration_ms | math sum)
    let avg_duration = ($results | get duration_ms | math avg)

    print "Summary:"
    print $"  • Total tests: ($total)"
    print_color $COLOR_GREEN $"  • Passed: ($passed)"
    if $failed > 0 {
        print_color $COLOR_RED $"  • Failed: ($failed)"
    } else {
        print_color $COLOR_GREEN $"  • Failed: ($failed)"
    }
    print $"  • Total duration: ($total_duration)ms"
    print $"  • Average duration: ($avg_duration)ms"

    print ""
    print "Individual Test Results:"
    for result in $results {
        print_result $result.name $result.success $result.duration_ms
    }

    # Performance analysis
    print ""
    print "Performance Analysis:"
    let fastest = ($results | sort-by duration_ms | first)
    let slowest = ($results | sort-by duration_ms | last)

    print_color $COLOR_GREEN $"  • Fastest: ($fastest.name) \(($fastest.duration_ms)ms\)"
    print_color $COLOR_YELLOW $"  • Slowest: ($slowest.name) \(($slowest.duration_ms)ms\)"

    # Failed tests details
    if $failed > 0 {
        print ""
        print_color $COLOR_RED "Failed Tests Details:"
        for result in ($results | where success == false) {
            print_color $COLOR_RED $"  • ($result.name)"
            print $"    Exit code: ($result.exit_code)"
            if ($result.stderr | str length) > 0 {
                print "    Error output:"
                print $"($result.stderr)" | lines | each {|line|
                    print $"      ($line)"
                }
            }
        }
    }

    # Return overall success
    $failed == 0
}

# Save detailed report to file
def save_report [results: list<record>, output_file: string] {
    let report = {
        timestamp: (date now | format date "%Y-%m-%d %H:%M:%S")
        summary: {
            total: ($results | length)
            passed: ($results | where success == true | length)
            failed: ($results | where success == false | length)
            total_duration_ms: ($results | get duration_ms | math sum)
            average_duration_ms: ($results | get duration_ms | math avg)
        }
        tests: $results
        plugins: {
            auth: (which auth | length) > 0
            kms: (which kms | length) > 0
            orchestrator: (which orch | length) > 0
        }
        environment: {
            nushell_version: (version | get version)
            os: $nu.os-info.name
            arch: $nu.os-info.arch
        }
    }

    $report | to json | save -f $output_file
    print_color $COLOR_CYAN $"\n📄 Detailed report saved to: ($output_file)"
}

# Main function
export def main [
    --output-file (-o): string = "plugin-test-report.json"  # Output report file
    --skip-integration (-s)  # Skip integration tests
    --verbose (-v)  # Show verbose output
] {
    print_header "🧪 Plugin Integration Test Suite"

    # Check prerequisites
    if not (check_prerequisites) {
        print_color $COLOR_RED "\n❌ Prerequisites check failed. Some test files are missing."
        exit 1
    }

    # Run all tests
    let results = (run_all_tests)

    # Generate and display report
    let all_passed = (generate_report $results)

    # Save detailed report
    save_report $results $output_file

    # Final result
    if $all_passed {
        print_header "✅ All Tests Passed!"
        exit 0
    } else {
        print_header "❌ Some Tests Failed"
        exit 1
    }
}
