# Quality Assurance Module - Testing, Linting, and Code Quality
# Commands for ensuring code quality, running tests, and maintaining standards

# 🧪 TESTING COMMANDS

# Run cargo check on all plugins
[no-cd]
check:
    @echo "🔍 Running cargo check on all plugins..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Checking $$plugin..."; \
            cd "$$plugin" && cargo check && cd ..; \
        fi; \
    done

# Run cargo test on all plugins
[no-cd]
test:
    @echo "🧪 Running tests on all plugins..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Testing $$plugin..."; \
            cd "$$plugin" && cargo test && cd ..; \
        fi; \
    done

# Run tests with verbose output
[no-cd]
test-verbose:
    @echo "🧪 Running tests (verbose)..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Testing $$plugin..."; \
            cd "$$plugin" && cargo test -- --nocapture && cd ..; \
        fi; \
    done

# Run tests in parallel
[no-cd]
test-parallel:
    @echo "⚡ Running tests in parallel..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Testing $$plugin..."; \
            cd "$$plugin" && cargo test --jobs 8 && cd ..; \
        fi; \
    done

# Test specific plugin
[no-cd]
test-plugin PLUGIN:
    @echo "🧪 Testing {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo test

# Run integration tests
[no-cd]
test-integration:
    @echo "🔗 Running integration tests..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Integration testing $$plugin..."; \
            cd "$$plugin" && cargo test --test integration 2>/dev/null || echo "No integration tests for $$plugin" && cd ..; \
        fi; \
    done

# Run doc tests
[no-cd]
test-docs:
    @echo "📖 Running documentation tests..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Doc testing $$plugin..."; \
            cd "$$plugin" && cargo test --doc && cd ..; \
        fi; \
    done

# 📏 LINTING COMMANDS

# Run cargo clippy on all plugins
[no-cd]
lint:
    @echo "📏 Running clippy on all plugins..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Linting $$plugin..."; \
            cd "$$plugin" && cargo clippy -- -D warnings && cd ..; \
        fi; \
    done

# Run clippy with additional lints
[no-cd]
lint-pedantic:
    @echo "📏 Running pedantic clippy..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Pedantic linting $$plugin..."; \
            cd "$$plugin" && cargo clippy -- -W clippy::pedantic -D warnings && cd ..; \
        fi; \
    done

# Lint specific plugin
[no-cd]
lint-plugin PLUGIN:
    @echo "📏 Linting {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo clippy -- -D warnings

# Fix lint issues automatically
[no-cd]
lint-fix:
    @echo "🔧 Fixing lint issues automatically..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Fixing $$plugin..."; \
            cd "$$plugin" && cargo clippy --fix --allow-dirty --allow-staged && cd ..; \
        fi; \
    done

# 🎨 FORMATTING COMMANDS

# Format all Rust code
[no-cd]
fmt:
    @echo "🎨 Formatting Rust code..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Formatting $$plugin..."; \
            cd "$$plugin" && cargo fmt && cd ..; \
        fi; \
    done

# Check formatting without making changes
[no-cd]
fmt-check:
    @echo "🎨 Checking code formatting..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Checking format of $$plugin..."; \
            cd "$$plugin" && cargo fmt -- --check && cd ..; \
        fi; \
    done

# Format specific plugin
[no-cd]
fmt-plugin PLUGIN:
    @echo "🎨 Formatting {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo fmt

# Format with custom configuration
[no-cd]
fmt-custom CONFIG:
    @echo "🎨 Formatting with custom config: {{CONFIG}}..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Formatting $$plugin..."; \
            cd "$$plugin" && cargo fmt --config {{CONFIG}} && cd ..; \
        fi; \
    done

# 🔒 SECURITY COMMANDS

# Run cargo audit on all plugins
[no-cd]
audit:
    @echo "🔒 Running security audit..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Auditing $$plugin..."; \
            cd "$$plugin" && cargo audit && cd ..; \
        fi; \
    done

# Audit specific plugin
[no-cd]
audit-plugin PLUGIN:
    @echo "🔒 Auditing {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo audit

# Check for outdated dependencies
[no-cd]
audit-outdated:
    @echo "📅 Checking for outdated dependencies..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Checking $$plugin..."; \
            cd "$$plugin" && cargo outdated || echo "cargo-outdated not available" && cd ..; \
        fi; \
    done

# Generate dependency report
[no-cd]
audit-deps:
    @echo "📋 Generating dependency report..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "=== $$plugin ==="; \
            cd "$$plugin" && cargo tree --depth 1 && cd ..; \
            echo ""; \
        fi; \
    done

# 📊 BENCHMARKING COMMANDS

# Run benchmarks on all plugins
[no-cd]
bench:
    @echo "📊 Running benchmarks..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Benchmarking $$plugin..."; \
            cd "$$plugin" && cargo bench 2>/dev/null || echo "No benchmarks for $$plugin" && cd ..; \
        fi; \
    done

# Benchmark specific plugin
[no-cd]
bench-plugin PLUGIN:
    @echo "📊 Benchmarking {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo bench

# Run performance tests
[no-cd]
perf-test:
    @echo "⚡ Running performance tests..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Performance testing $$plugin..."; \
            cd "$$plugin" && cargo test --release perf 2>/dev/null || echo "No perf tests for $$plugin" && cd ..; \
        fi; \
    done

# 📚 DOCUMENTATION COMMANDS

# Generate documentation for all plugins
[no-cd]
docs:
    @echo "📚 Generating documentation..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Generating docs for $$plugin..."; \
            cd "$$plugin" && cargo doc --no-deps && cd ..; \
        fi; \
    done

# Open documentation in browser
[no-cd]
docs-open PLUGIN:
    @echo "📖 Opening documentation for {{PLUGIN}}..."
    @cd {{PLUGIN}} && cargo doc --open

# Generate documentation with private items
[no-cd]
docs-private:
    @echo "📚 Generating documentation (including private)..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Generating docs for $$plugin..."; \
            cd "$$plugin" && cargo doc --no-deps --document-private-items && cd ..; \
        fi; \
    done

# Check documentation links
[no-cd]
docs-check:
    @echo "🔗 Checking documentation links..."
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo "Checking docs for $$plugin..."; \
            cd "$$plugin" && cargo doc --no-deps 2>&1 | grep -i "warning\|error" || echo "✅ OK" && cd ..; \
        fi; \
    done

# 🔄 WORKFLOW COMMANDS

# Complete quality check workflow
[no-cd]
qa-flow:
    @echo "✨ Running complete quality check workflow..."
    @just validate-nushell
    @just fmt-check
    @just lint
    @just test
    @just audit

# Quick quality check
[no-cd]
qa-quick:
    @echo "⚡ Running quick quality checks..."
    @just fmt-check
    @just check
    @just lint

# Pre-commit checks
[no-cd]
qa-pre-commit:
    @echo "🔍 Running pre-commit checks..."
    @just fmt-check
    @just lint
    @just test

# CI simulation
[no-cd]
qa-ci:
    @echo "🤖 Simulating CI workflow..."
    @just validate-nushell
    @just fmt-check
    @just lint
    @just test
    @just audit
    @echo "✅ All CI checks passed!"

# Full quality suite
[no-cd]
qa-full:
    @echo "🎯 Running full quality suite..."
    @just fmt-check
    @just lint-pedantic
    @just test-verbose
    @just test-docs
    @just audit
    @just docs-check
    @just bench

# 📊 QUALITY INFORMATION

# Show quality metrics
[no-cd]
qa-metrics:
    @echo "📊 Quality Metrics:"
    @echo "=================="
    @echo ""
    @echo "Code Coverage:"
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo -n "  $$plugin: "; \
            cd "$$plugin" && cargo tarpaulin --skip-clean --out Stdout 2>/dev/null | grep -o "[0-9]*\.[0-9]*%" | tail -1 || echo "Not available" && cd ..; \
        fi; \
    done
    @echo ""
    @echo "Lines of Code:"
    @find nu_plugin_*/src -name "*.rs" -exec wc -l {} + | tail -1 | awk '{print "  Total: " $1 " lines"}'

# Show test statistics
[no-cd]
qa-stats:
    @echo "📊 Test Statistics:"
    @echo "=================="
    @for plugin in nu_plugin_*; do \
        if [ -d "$$plugin" ]; then \
            echo -n "$$plugin: "; \
            cd "$$plugin" && cargo test 2>&1 | grep -o "[0-9]* passed" || echo "No tests" && cd ..; \
        fi; \
    done

# 📊 QA INFORMATION

# Show QA help
[no-cd]
qa-help:
    @echo "🧪 Quality Assurance Module Help"
    @echo "================================"
    @echo ""
    @echo "TESTING:"
    @echo "  check                    - Run cargo check on all plugins"
    @echo "  test                     - Run tests on all plugins"
    @echo "  test-verbose             - Run tests with verbose output"
    @echo "  test-parallel            - Run tests in parallel"
    @echo "  test-plugin PLUGIN       - Test specific plugin"
    @echo "  test-integration         - Run integration tests"
    @echo "  test-docs                - Run documentation tests"
    @echo ""
    @echo "LINTING:"
    @echo "  lint                     - Run clippy on all plugins"
    @echo "  lint-pedantic            - Run pedantic clippy"
    @echo "  lint-plugin PLUGIN       - Lint specific plugin"
    @echo "  lint-fix                 - Fix lint issues automatically"
    @echo ""
    @echo "FORMATTING:"
    @echo "  fmt                      - Format all code"
    @echo "  fmt-check                - Check formatting"
    @echo "  fmt-plugin PLUGIN        - Format specific plugin"
    @echo "  fmt-custom CONFIG        - Format with custom config"
    @echo ""
    @echo "SECURITY:"
    @echo "  audit                    - Run security audit"
    @echo "  audit-plugin PLUGIN      - Audit specific plugin"
    @echo "  audit-outdated           - Check outdated dependencies"
    @echo "  audit-deps               - Generate dependency report"
    @echo ""
    @echo "BENCHMARKING:"
    @echo "  bench                    - Run benchmarks"
    @echo "  bench-plugin PLUGIN      - Benchmark specific plugin"
    @echo "  perf-test                - Run performance tests"
    @echo ""
    @echo "DOCUMENTATION:"
    @echo "  docs                     - Generate documentation"
    @echo "  docs-open PLUGIN         - Open docs in browser"
    @echo "  docs-private             - Generate with private items"
    @echo "  docs-check               - Check documentation links"
    @echo ""
    @echo "WORKFLOWS:"
    @echo "  qa-flow                  - Complete quality workflow"
    @echo "  qa-quick                 - Quick quality checks"
    @echo "  qa-pre-commit            - Pre-commit checks"
    @echo "  qa-ci                    - CI simulation"
    @echo "  qa-full                  - Full quality suite"
    @echo ""
    @echo "METRICS:"
    @echo "  qa-metrics               - Show quality metrics"
    @echo "  qa-stats                 - Show test statistics"
    @echo ""
    @echo "EXAMPLES:"
    @echo "  just qa-flow"
    @echo "  just test-plugin nu_plugin_clipboard"
    @echo "  just lint-fix"
    @echo "  just docs-open nu_plugin_image"
