# ╔══════════════════════════════════════════════════════════════════════╗
# ║                      DEVELOPMENT UTILITIES                           ║
# ║                   Watch, format, lint, docs                          ║
# ╚══════════════════════════════════════════════════════════════════════╝

# === WORKSPACE ===
WORKSPACE_ROOT := justfile_directory()

# Help for dev module
help:
    @echo "DEVELOPMENT MODULE"
    @echo ""
    @echo "Code quality:"
    @echo "  just dev::fmt           Format code (Rust + TOML)"
    @echo "  just dev::fmt-toml      Format TOML files only"
    @echo "  just dev::fmt-check     Check format (no changes)"
    @echo "  just dev::lint          Run clippy linter"
    @echo "  just dev::lint-toml     Lint TOML files"
    @echo "  just dev::audit         Audit dependencies"
    @echo ""
    @echo "Testing & Coverage:"
    @echo "  just dev::coverage      Generate HTML coverage report"
    @echo "  just dev::coverage-ci   Generate lcov coverage"
    @echo "  just dev::coverage-open Open coverage report"
    @echo ""
    @echo "Benchmarks:"
    @echo "  just dev::bench         Run benchmarks"
    @echo "  just dev::bench-open    Run and open benchmark report"
    @echo ""
    @echo "Documentation:"
    @echo "  just dev::docs          Generate and open docs"
    @echo "  just dev::docs-gen      Generate docs only"
    @echo ""
    @echo "Common tasks:"
    @echo "  just dev::build         Build default features"
    @echo "  just dev::watch         Watch and rebuild on changes"
    @echo "  just dev::check         Check + fmt + lint"
    @echo ""
    @echo "Inspect:"
    @echo "  just dev::info          Show workspace info"
    @echo "  just dev::tree          Show dependency tree"

# === CODE FORMATTING ===

# Format all code (Rust + TOML)
[doc("Format Rust and TOML code")]
fmt:
    @echo "=== Formatting code ==="
    cargo fmt --all
    just dev::fmt-toml
    @echo "✓ Formatting complete"

# Format TOML files with taplo
[doc("Format TOML files with taplo")]
fmt-toml:
    @echo "=== Formatting TOML files ==="
    @command -v taplo >/dev/null || (echo "taplo not installed: cargo install taplo-cli"; exit 1)
    taplo format
    @echo "✓ TOML formatting complete"

# Check format without modifying (Rust + TOML)
[doc("Check format without changes")]
fmt-check:
    @echo "=== Checking format ==="
    cargo fmt --all -- --check
    just dev::fmt-toml-check
    @echo "✓ Format check passed"

# Check TOML format without modifying
[doc("Check TOML format without changes")]
fmt-toml-check:
    @echo "=== Checking TOML format ==="
    @command -v taplo >/dev/null || (echo "taplo not installed: cargo install taplo-cli"; exit 1)
    taplo format --check
    @echo "✓ TOML format check passed"

# === LINTING ===

# Run clippy on all targets (alias for lint-rust)
[doc("Lint code with clippy (Rust only)")]
lint:
    just dev::lint-rust

# Auto-fix clippy warnings
[doc("Auto-fix clippy warnings")]
lint-fix:
    #!/bin/bash
    set -e
    cd "{{ WORKSPACE_ROOT }}"
    echo "=== Auto-fixing clippy warnings ==="
    if [ -d "target/debug" ]; then
        echo "   → Using: target/debug (exists)"
        cargo clippy --all-targets --all-features --fix --allow-dirty
    else
        echo "   → Using: /tmp/typedialog-ci (target/debug does not exist)"
        cargo clippy --target-dir=/tmp/typedialog-ci --all-targets --all-features --fix --allow-dirty
    fi
    echo "✓ Auto-fix complete"

# === MULTI-LANGUAGE LINTING ===

# Lint Rust code (clippy) - respects existing target/debug
[doc("Lint Rust with clippy")]
lint-rust:
    #!/bin/bash
    set -e
    cd "{{ WORKSPACE_ROOT }}"
    echo "=== Linting Rust ==="
    if [ -d "target/debug" ]; then
        echo "   → Using: target/debug (exists)"
        cargo clippy --all-targets --all-features -- -D warnings
    else
        echo "   → Using: /tmp/typedialog-ci (target/debug does not exist)"
        cargo clippy --target-dir=/tmp/typedialog-ci --all-targets --all-features -- -D warnings
    fi
    echo "✓ Rust linting complete"

# Lint bash scripts (shellcheck)
[doc("Lint bash scripts with shellcheck")]
lint-bash:
    @echo "=== Linting bash scripts ==="
    @command -v shellcheck >/dev/null || (echo "shellcheck not installed: brew install shellcheck"; exit 1)
    @find . -name "*.sh" -not -path "*/node_modules/*" -not -path "*/target/*" | xargs shellcheck
    @echo "✓ Bash linting complete"

# Lint Nickel files (typecheck)
[doc("Lint Nickel files")]
lint-nickel:
    @echo "=== Linting Nickel files ==="
    @command -v nickel >/dev/null || (echo "nickel not installed: brew install nickel"; exit 1)
    @find . -name "*.ncl" -not -path "*/node_modules/*" -not -path "*/target/*" | while read f; do \
        echo "Checking $$f"; \
        nickel typecheck "$$f" || exit 1; \
    done
    @echo "✓ Nickel linting complete"

# Lint Nushell scripts
[doc("Lint Nushell scripts")]
lint-nushell:
    @echo "=== Linting Nushell scripts ==="
    @command -v nu >/dev/null || (echo "nushell not installed: brew install nushell"; exit 1)
    @find . -name "*.nu" -not -path "*/node_modules/*" -not -path "*/target/*" | while read f; do \
        echo "Checking $$f"; \
        nu --check "$$f" || exit 1; \
    done
    @echo "✓ Nushell linting complete"

# Lint TOML files with taplo
[doc("Lint TOML files with taplo")]
lint-toml:
    @echo "=== Linting TOML files ==="
    @command -v taplo >/dev/null || (echo "taplo not installed: cargo install taplo-cli"; exit 1)
    taplo lint
    @echo "✓ TOML linting complete"

# Lint Markdown syntax (markdownlint-cli2)
# Covers: docs/**/*.md + root *.md (excludes .claude, .coder, CLAUDE.md)
[doc("Lint Markdown syntax with markdownlint-cli2")]
lint-markdown:
    @echo "=== Linting Markdown syntax (docs/ + root) ==="
    @command -v markdownlint-cli2 >/dev/null || (echo "markdownlint-cli2 not installed: npm install -g markdownlint-cli2"; exit 1)
    @markdownlint-cli2 "docs/**/*.md" "*.md"
    @echo "✓ Markdown syntax linting complete"

# Auto-fix Markdown lint errors
# Covers: docs/**/*.md + root *.md (excludes .claude, .coder, CLAUDE.md)
[doc("Auto-fix Markdown lint errors")]
lint-markdown-fix:
    @echo "=== Auto-fixing Markdown syntax (docs/ + root) ==="
    @command -v markdownlint-cli2 >/dev/null || (echo "markdownlint-cli2 not installed: npm install -g markdownlint-cli2"; exit 1)
    @markdownlint-cli2 --fix "docs/**/*.md" "*.md"
    @echo "✓ Markdown auto-fix complete"

# Lint Markdown prose with Vale (quality, style, grammar)
# Covers: docs/**/*.md only
[doc("Lint Markdown prose quality with Vale")]
lint-markdown-prose:
    @echo "=== Linting Markdown prose quality (docs/ only) ==="
    @command -v vale >/dev/null || (echo "Vale not installed: brew install vale (macOS) or see https://vale.sh"; exit 1)
    @vale docs/
    @echo "✓ Prose linting complete"

# Lint Markdown: syntax + prose
# Complete linting with markdownlint-cli2 (syntax) + Vale (prose quality)
[doc("Lint Markdown: syntax (markdownlint-cli2) + prose (Vale)")]
lint-markdown-full:
    @echo "╔═══════════════════════════════════════════════════════════╗"
    @echo "║         MARKDOWN LINTING (SYNTAX + PROSE)                 ║"
    @echo "╚═══════════════════════════════════════════════════════════╝"
    @echo ""
    just dev::lint-markdown
    @echo ""
    just dev::lint-markdown-prose

# Lint all languages
[doc("Lint all: Rust + TOML + bash + Nickel + Nushell + Markdown")]
lint-all:
    @echo "╔═══════════════════════════════════════════════════════════╗"
    @echo "║              MULTI-LANGUAGE LINTING                       ║"
    @echo "╚═══════════════════════════════════════════════════════════╝"
    @echo ""
    just dev::lint-rust
    @echo ""
    just dev::lint-toml
    @echo ""
    just dev::lint-bash
    @echo ""
    just dev::lint-nickel
    @echo ""
    just dev::lint-nushell
    @echo ""
    just dev::lint-markdown
    @echo ""
    @echo "✓ All linting complete"

# === DEPENDENCY AUDITING ===

# Audit dependencies for security
[doc("Audit dependencies")]
audit:
    @echo "=== Auditing dependencies ==="
    @command -v cargo-audit >/dev/null || (echo "cargo-audit not installed"; exit 1)
    cargo audit
    @echo "✓ Audit complete"

# === DOCUMENTATION ===

# Generate documentation
[doc("Generate rustdoc")]
docs-gen:
    @echo "=== Generating documentation ==="
    cargo doc --no-deps --all-features
    @echo "✓ Documentation generated"

# Generate and open documentation
[doc("Generate docs and open browser")]
docs:
    @echo "=== Opening documentation ==="
    cargo doc --no-deps --all-features --open
    @echo "✓ Documentation open"

# === BUILD & WATCH ===

# Quick build with default features
[doc("Build default features")]
build:
    @echo "=== Quick build ==="
    cargo build
    @echo "✓ Build complete"

# Watch for changes and rebuild
[doc("Watch and rebuild on changes")]
watch:
    @echo "=== Watching for changes... ==="
    @command -v cargo-watch >/dev/null || (echo "cargo-watch not installed"; exit 1)
    cargo watch -x build -x test --clear

# === COMPREHENSIVE CHECKS ===

# Run check + format + lint (all languages)
[doc("Check: format + lint (all languages) + compile")]
check:
    @echo "=== Running comprehensive check ==="
    just dev::fmt-check
    just dev::lint-all
    cargo check --all-features
    @echo "✓ Check complete"

# === CODE COVERAGE ===

[doc("Generate coverage report (HTML)")]
coverage:
    @echo "=== Generating code coverage ==="
    cargo llvm-cov --all-features --workspace --html
    @echo "✓ Coverage report: target/llvm-cov/html/index.html"

[doc("Generate coverage (lcov for CI)")]
coverage-ci:
    @echo "=== Generating coverage (CI) ==="
    cargo llvm-cov --all-features --workspace --lcov --output-path lcov.info
    @echo "✓ Coverage file: lcov.info"

[doc("Open coverage report")]
coverage-open:
    @echo "=== Opening coverage report ==="
    cargo llvm-cov --all-features --workspace --open

# === BENCHMARKS ===

[doc("Run benchmarks")]
bench:
    @echo "=== Running benchmarks ==="
    cargo bench --workspace
    @echo "✓ Benchmarks complete"
    @echo "  Reports: target/criterion/report/index.html"

[doc("Run benchmarks and open report")]
bench-open:
    cargo bench --workspace
    @echo "Opening benchmark report..."
    open target/criterion/report/index.html || xdg-open target/criterion/report/index.html

# === WORKSPACE INFO ===

# Show workspace information
[doc("Display workspace info")]
info:
    @echo "WORKSPACE INFORMATION"
    @echo ""
    @echo "Root: {{ WORKSPACE_ROOT }}"
    @echo ""
    @echo "Crates:"
    @cargo tree --depth 0 | grep -v "^typedialog" || cargo build --message-format=short 2>&1 | grep "Compiling" || true
    @echo ""
    @echo "Features:"
    @echo "  Default: CLI + i18n + templates"
    @echo "  Available: cli, tui, web, i18n, templates"
    @echo ""
    @echo "Binaries:"
    @ls -1 target/*/typedialog* 2>/dev/null | head -6 || echo "  (run 'just build' first)"

# Show dependency tree
[doc("Display dependency tree")]
tree:
    @echo "=== Dependency tree ==="
    cargo tree --workspace
