# syntaxis Justfile
# Master justfile for the entire syntaxis monorepo
# Based on rust-modular template with NuShell + Rust hybrid support

set shell := ["nu", "-c"]

# Default recipe (show help)
default:
    @just help

# ═══════════════════════════════════════════════════════════════════════════
# ⓘ HELP & STATUS
# ═══════════════════════════════════════════════════════════════════════════

# Show help with all available recipes
help:
    @print ""
    @print "🎯 syntaxis Just Recipes"
    @print ""
    @print "BUILD RECIPES:"
    @print "  just check              Check compilation (workspace)"
    @print "  just build              Build debug binaries"
    @print "  just build-release      Build optimized release binaries"
    @print "  just build-watch        Watch mode: rebuild on file changes"
    @print ""
    @print "TESTING RECIPES:"
    @print "  just test               Run all library tests"
    @print "  just test-verbose       Run tests with output"
    @print "  just test-integration   Run integration tests"
    @print "  just test-all           Run all tests (lib + integration)"
    @print "  just test-watch         Watch mode: continuous testing"
    @print "  just test-coverage      Generate test coverage reports"
    @print ""
    @print "CODE QUALITY RECIPES:"
    @print "  just fmt                Format code (cargo fmt)"
    @print "  just fmt-check          Check formatting without changes"
    @print "  just lint               Lint code (clippy)"
    @print "  just lint-pedantic      Lint with pedantic warnings"
    @print "  just audit              Security audit of dependencies"
    @print "  just check-deps         Validate dependency versions"
    @print "  just sync-deps          Sync dependencies interactively"
    @print "  just check-all          Run all checks (fmt → lint → check → test → audit → check-deps)"
    @print ""
    @print "DOCUMENTATION RECIPES:"
    @print "  just doc                Generate and open Rust documentation"
    @print ""
    @print "RUNNING & BUILDING RECIPES:"
    @print "  just run-cli [ARGS]           Run workspace CLI"
    @print "  just run-tui                  Run workspace TUI"
    @print "  just run-api                  Run workspace API with Leptos dashboard"
    @print "  just dev-dashboard            Dev server for Leptos dashboard (port 8080)"
    @print "  just build-dashboard          Build Leptos dashboard (release)"
    @print "  just build-dashboard-dev      Build Leptos dashboard (debug)"
    @print "  just check-dashboard-tools    Check if dashboard prerequisites installed"
    @print "  just install-dashboard-tools  Install missing dashboard prerequisites"
    @print ""
    @print "INSTALLATION PRESETS:"
    @print "  just install-list           List all available presets"
    @print "  just install-minimal        Install minimal (CLI only)"
    @print "  just install-local          Install local (simple dev)"
    @print "  just install-dev            Install dev (full-stack with services)"
    @print "  just install-staging        Install staging (Docker/CI-CD)"
    @print "  just install-production     Install production (Kubernetes)"
    @print "  just install-interactive    Interactive installation"
    @print "  just install-generate PRESET Generate config for preset"
    @print "  just detect-provctl         Check provctl availability"
    @print "  just services CMD PRESET    Manage services (list/deploy/start/stop)"
    @print "  just test-provisioning      Run provisioning tests"
    @print ""
    @print "SCRIPTS RECIPES:"
    @print "  just scripts-list       List available binaries"
    @print "  just scripts-install    Install all binaries"
    @print "  just scripts-status     Show installation status"
    @print ""
    @print "DEVELOPMENT RECIPES:"
    @print "  just init               Initialize development environment"
    @print "  just check-tools        Validate required development tools"
    @print "  just info               Show workspace information"
    @print "  just status             Show current build/test status"
    @print ""
    @print "CLEANUP RECIPES:"
    @print "  just clean              Clean all build artifacts"
    @print ""
    @print "Use 'just <recipe-name>' to run a recipe"
    @print ""

# Show detailed help
help-full: help
    @print "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    @print ""
    @print "EXAMPLES:"
    @print "  # Build and run all tests"
    @print "  just build && just test-all"
    @print ""
    @print "  # Continuous development: watch mode for compilation and tests"
    @print "  just build-watch      # In one terminal"
    @print "  just test-watch       # In another terminal"
    @print ""
    @print "  # Full verification before committing"
    @print "  just check-all"
    @print ""
    @print "  # Install binaries and check installation"
    @print "  just scripts-install && just scripts-status"
    @print ""
    @print "  # Generate documentation"
    @print "  just doc"
    @print ""
    @print "WORKSPACE STRUCTURE:"
    @print "  shared/                 # Shared libraries (rust-api, rust-tui, rust)"
    @print "  syntaxis/      # Main project (6+ crates)"
    @print "  scripts/                # Build and infrastructure scripts"
    @print "  .claude/                # Development guidelines"
    @print "  Justfile                # This file (root justfile)"
    @print ""
    @print "RELATED DOCUMENTATION:"
    @print "  README.md               # Project overview"
    @print "  .claude/PROJECT_RULES.md # Architecture and standards"
    @print "  .claude/CODE_STANDARDS.md # Testing and verification standards"
    @print "  scripts/README.md       # Scripts documentation"
    @print ""

# Show workspace information and tool versions
info:
    @print ""
    @print "📊 syntaxis Project Information"
    @print "────────────────────────────────────────"
    @print ""
    @print "📍 Location: /Users/Akasha/Development/syntaxis"
    @print ""
    @print "🔧 Tool Versions:"
    @cargo --version
    @rustc --version
    @nu --version
    @print ""
    @print "📦 Workspace Members (6 active, 2 WIP):"
    @print "  ✅ syntaxis-core        - Core library (173 tests)"
    @print "  ✅ syntaxis-cli         - CLI tool"
    @print "  ✅ syntaxis-tui         - Terminal UI (10 tests)"
    @print "  ✅ syntaxis-dashboard   - Web Dashboard (52 tests)"
    @print "  ✅ dashboard-client      - Dashboard client (4 tests)"
    @print "  ✅ dashboard-shared      - Shared types (5 tests)"
    @print "  🟡 syntaxis-api        - REST API (37 errors - WIP)"
    @print "  🟡 syntaxis-vapora     - VAPORA adapter (planned)"
    @print ""
    @print "📊 Test Status: 632/632 tests passing ✅"
    @print ""

# Check development tools are available
check-tools:
    @print "🔧 Checking development tools..."
    @print ""
    @try { cargo --version; print "  ✅ cargo found" } catch { print "  ❌ cargo NOT found" }
    @try { rustc --version; print "  ✅ rustc found" } catch { print "  ❌ rustc NOT found" }
    @try { nu --version; print "  ✅ nushell found" } catch { print "  ❌ nushell NOT found" }
    @try { which cargo-fmt; print "  ✅ cargo-fmt found" } catch { print "  ⚠️  cargo-fmt not found" }
    @try { which cargo-clippy; print "  ✅ cargo-clippy found" } catch { print "  ⚠️  cargo-clippy not found" }
    @print ""

# Initialize development environment
init: check-tools
    @print "🚀 Initializing syntaxis development environment..."
    @print ""
    mkdir -p .syntaxis
    mkdir -p .project-tools
    @print "✅ Created workspace directories"
    @print ""
    @print "Next steps:"
    @print "  1. Run: just install-scripts (to install binary management scripts)"
    @print "  2. Run: just build (to build the project)"
    @print "  3. Run: just test (to run tests)"
    @print ""

# Show build and test status
status:
    @print "📈 syntaxis Status"
    @print "────────────────────────────"
    @print ""
    cargo --version
    rustc --version
    @print ""
    @print "Recent changes:"
    @(cd syntaxis && git log --oneline -5 2>/dev/null || echo "  (not a git repository)")
    @print ""

# ═══════════════════════════════════════════════════════════════════════════
# 🏗️  BUILD RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Check entire workspace compiles
check:
    @echo "🔍 Checking workspace..."
    cargo check --workspace

# Build debug binaries
build:
    @echo "🏗️  Building debug binaries..."
    cargo build --workspace

# Build optimized release binaries
build-release:
    @echo "🚀 Building release binaries..."
    cargo build --workspace --release

# Watch mode: rebuild on file changes
build-watch:
    @echo "👀 Watching for changes (build mode)..."
    cargo watch -x 'build --workspace'

# ═══════════════════════════════════════════════════════════════════════════
# 🧪 TESTING RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Run all library tests
test:
    @echo "🧪 Running library tests..."
    cargo test --workspace --lib -- --test-threads=1

# Run tests with output
test-verbose:
    @echo "🧪 Running tests (verbose)..."
    RUST_LOG=debug cargo test --workspace --lib -- --nocapture --test-threads=1

# Run integration tests
test-integration:
    @echo "🧪 Running integration tests..."
    cargo test --workspace --test '*' -- --test-threads=1

# Run all tests (library + integration)
test-all:
    @echo "🧪 Running all tests..."
    cargo test --workspace -- --test-threads=1

# Watch mode: continuous testing
test-watch:
    @echo "👀 Watching for changes (test mode)..."
    cargo watch -x 'test --workspace --lib'

# Generate test coverage reports
test-coverage:
    @echo "📊 Generating test coverage..."
    @echo "Note: requires cargo-tarpaulin or cargo-llvm-cov"
    @(cargo tarpaulin --workspace --out Html || echo "⚠️  cargo-tarpaulin not found")

# ═══════════════════════════════════════════════════════════════════════════
# 📝 CODE QUALITY RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Format code with cargo fmt
fmt:
    @echo "📝 Formatting code..."
    cargo fmt --all

# Check formatting without making changes
fmt-check:
    @echo "📝 Checking code format..."
    cargo fmt --all -- --check

# Lint code with clippy
lint:
    @echo "🔎 Linting code..."
    cargo clippy --all-targets --all-features

# Lint with pedantic warnings
lint-pedantic:
    @echo "🔎 Linting with pedantic warnings..."
    cargo clippy --all-targets --all-features -- -W clippy::all -W clippy::pedantic

# Security audit of dependencies
audit:
    @echo "🔐 Running security audit..."
    cargo audit

# Validate dependency versions against canonical registry
check-deps:
    @echo "🔍 Validating dependency versions..."
    dependency-manager validate

# Sync dependencies interactively (fix mismatches)
sync-deps:
    @echo "🔄 Synchronizing dependencies..."
    dependency-manager sync --interactive

# Show canonical dependency registry
show-deps:
    @echo "📋 Canonical Dependency Registry:"
    @echo ""
    dependency-manager registry

# Full verification: fmt → lint → check → test → audit → check-deps
check-all:
    @echo "✅ Running full verification suite..."
    @echo ""
    @echo "Step 1/6: Formatting..."
    just fmt
    @echo ""
    @echo "Step 2/6: Linting..."
    just lint
    @echo ""
    @echo "Step 3/6: Checking compilation..."
    just check
    @echo ""
    @echo "Step 4/6: Running tests..."
    just test-all
    @echo ""
    @echo "Step 5/6: Security audit..."
    just audit
    @echo ""
    @echo "Step 6/6: Validating dependencies..."
    just check-deps
    @echo ""
    @echo "✅ All checks passed successfully!"

# ═══════════════════════════════════════════════════════════════════════════
# 📚 DOCUMENTATION RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Generate and open Rust documentation
doc:
    @echo "📚 Generating documentation..."
    cargo doc --workspace --no-deps --open

# ═══════════════════════════════════════════════════════════════════════════
# 🚀 RUNNING RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Run syntaxis CLI
run-cli *args:
    @echo "🚀 Running syntaxis CLI..."
    cargo run -p syntaxis-cli -- {{ args }}

# Run syntaxis TUI
run-tui:
    @echo "🚀 Starting syntaxis TUI..."
    cargo run -p syntaxis-tui

# Run syntaxis API with dashboard (serves WASM at root)
run-api:
    @echo "🚀 Starting syntaxis API with Leptos dashboard..."
    cargo run -p syntaxis-api

# Build Leptos WASM dashboard (release mode)
build-dashboard:
    @echo "🎨 Building Leptos WASM dashboard..."
    nu scripts/core/build-dashboard.nu build

# Build Leptos WASM dashboard (development mode)
build-dashboard-dev:
    @echo "🎨 Building Leptos WASM dashboard (dev mode)..."
    nu scripts/core/build-dashboard.nu build --dev

# Run Leptos dashboard dev server (port 8080, proxies API)
dev-dashboard:
    @echo "👀 Starting Leptos dashboard dev server (port 8080)..."
    @echo "    • Dashboard: http://localhost:8080"
    @echo "    • API proxy: /api → http://localhost:3000/api"
    @echo "    • Make sure API server is running: just run-api"
    @echo ""
    cd core && trunk serve --port 8080 --proxy-backend=http://localhost:3000/api

# Check dashboard build prerequisites
check-dashboard-tools:
    @echo "🔧 Checking dashboard prerequisites..."
    nu scripts/core/build-dashboard.nu check-install

# Install dashboard build prerequisites
install-dashboard-tools:
    @echo "📦 Installing dashboard prerequisites..."
    nu scripts/core/build-dashboard.nu install

# ═══════════════════════════════════════════════════════════════════════════
# 🚀 INSTALLATION PRESETS (with provctl integration)
# ═══════════════════════════════════════════════════════════════════════════

# List all available installation presets
install-list:
    @echo "📋 Available installation presets:"
    nu scripts/provisioning/install-with-presets.nu --list-presets

# Install with minimal preset (CLI only)
install-minimal:
    @echo "🔨 Installing with minimal preset (CLI only)..."
    nu scripts/provisioning/install-with-presets.nu --preset minimal

# Install with local preset (default - simple development)
install-local:
    @echo "🔨 Installing with local preset (simple development)..."
    nu scripts/provisioning/install-with-presets.nu --preset local

# Install with dev preset (full-stack with services)
install-dev:
    @echo "🔨 Installing with dev preset (full-stack with services)..."
    nu scripts/provisioning/install-with-presets.nu --preset dev --verbose

# Install with staging preset (Docker/CI-CD environment)
install-staging:
    @echo "🔨 Installing with staging preset (Docker/CI-CD)..."
    nu scripts/provisioning/install-with-presets.nu --preset staging --verbose

# Install with production preset (Kubernetes/Enterprise)
install-production:
    @echo "🔨 Installing with production preset (Kubernetes)..."
    nu scripts/provisioning/install-with-presets.nu --preset production --verbose

# Interactive installation (guided setup)
install-interactive:
    @echo "🔨 Starting interactive installation..."
    nu scripts/provisioning/install-with-presets.nu --interactive

# Generate installation configuration (no installation)
install-generate [PRESET="dev"]:
    @echo "📝 Generating configuration for preset: {{PRESET}}"
    nu scripts/provisioning/install-with-presets.nu --preset {{PRESET}} --generate-config

# Install from configuration file
install-config [CONFIG_FILE]:
    @echo "📦 Installing from configuration: {{CONFIG_FILE}}"
    nu scripts/provisioning/install-with-presets.nu --config {{CONFIG_FILE}}

# Detect provctl availability
detect-provctl:
    @echo "🔍 Detecting provctl..."
    nu scripts/provisioning/detect-provctl.nu --verbose

# Manage services (list, deploy, start, stop, status)
services [COMMAND="list", PRESET="dev"]:
    @echo "🎮 Service management: {{COMMAND}} {{PRESET}}"
    nu scripts/provisioning/provctl-services.nu {{COMMAND}} {{PRESET}}

# Test provisioning setup
test-provisioning:
    @echo "🧪 Testing provisioning configuration..."
    nu tests/provisioning/test-all.nu

# ═══════════════════════════════════════════════════════════════════════════
# 📦 SCRIPTS RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# List available binaries
scripts-list:
    @echo "📦 Available binaries:"
    nu scripts/install-cli.nu list

# Install all binaries (workspace, syntaxis-tui, syntaxis-dashboard)
scripts-install:
    @echo "📦 Installing binaries..."
    nu scripts/install-cli.nu all

# Show binary installation status
scripts-status:
    @echo "📊 Installation status:"
    nu scripts/install-cli.nu status

# Show full installation manifest
scripts-manifest:
    @echo "📋 Installation manifest:"
    nu scripts/manifest.nu show-manifest

# ═══════════════════════════════════════════════════════════════════════════
# 🧹 CLEANUP RECIPES
# ═══════════════════════════════════════════════════════════════════════════

# Clean all build artifacts
clean:
    @echo "🧹 Cleaning build artifacts..."
    cargo clean
    @echo "✅ Clean complete"
