# Foxhunt HFT Trading System - Development Commands
# Use `just` for convenient development workflows
# Install just: cargo install just

# Default recipe to display available commands
default:
    @just --list

# ============================================================================
# QUICK CHECKS - Run before committing
# ============================================================================

# Quick check before commit (fast feedback)
pre-commit:
    @echo "🔍 Running pre-commit checks..."
    cargo check --workspace
    @echo ""
    @echo "📊 Warning count:"
    @cargo check --workspace 2>&1 | grep 'warning:' | wc -l
    @echo ""
    @echo "✅ Pre-commit checks complete"

# Full quality gate checks (comprehensive)
check-all:
    @echo "🚀 Running full quality gate checks..."
    @echo ""
    @echo "1️⃣ Compilation check..."
    cargo check --workspace --all-targets
    @echo ""
    @echo "2️⃣ Clippy linting..."
    cargo clippy --workspace --all-targets -- -D warnings
    @echo ""
    @echo "3️⃣ Test suite..."
    cargo test --workspace --lib -- --skip redis --skip kill_switch
    @echo ""
    @echo "✅ All quality gates passed!"

# ============================================================================
# COMPILATION AND BUILD
# ============================================================================

# Check compilation without building
check:
    cargo check --workspace --all-targets

# Build all services in debug mode
build:
    cargo build --workspace

# Build all services in release mode (optimized)
build-release:
    cargo build --release --workspace

# Build specific service
build-service SERVICE:
    cargo build --release -p {{SERVICE}}

# ============================================================================
# CODE QUALITY
# ============================================================================

# Format all code
fmt:
    cargo fmt --all

# Check formatting without applying
fmt-check:
    cargo fmt --all -- --check

# Run clippy lints
clippy:
    cargo clippy --workspace --all-targets -- -D warnings

# Run clippy with all lints enabled
clippy-all:
    cargo clippy --workspace --all-targets --all-features -- -W clippy::all -W clippy::pedantic

# Count warnings in codebase
warnings:
    @echo "📊 Warning Statistics:"
    @echo ""
    @echo "Total warnings:"
    @cargo check --workspace 2>&1 | grep 'warning:' | wc -l
    @echo ""
    @echo "Warnings by type:"
    @cargo check --workspace 2>&1 | grep 'warning:' | sed 's/.*warning: //' | sort | uniq -c | sort -rn | head -20

# ============================================================================
# TESTING
# ============================================================================

# Run all tests
test:
    cargo test --workspace

# Run unit tests only
test-unit:
    cargo test --workspace --lib

# Run integration tests only
test-integration:
    cargo test --workspace --tests

# Run tests with output
test-verbose:
    cargo test --workspace -- --nocapture

# Run tests excluding external dependencies
test-fast:
    cargo test --workspace --lib -- --skip redis --skip kill_switch

# Run specific test
test-one TEST:
    cargo test {{TEST}} -- --nocapture

# ============================================================================
# CODE COVERAGE
# ============================================================================

# Generate code coverage report with tarpaulin
coverage:
    @echo "📊 Generating code coverage report..."
    cargo tarpaulin --workspace --out Html --skip-clean
    @echo ""
    @echo "✅ Coverage report generated: target/tarpaulin/index.html"
    @echo "📂 Open with: xdg-open target/tarpaulin/index.html"

# Generate code coverage with lcov format
coverage-lcov:
    cargo tarpaulin --workspace --out Lcov --skip-clean

# Generate code coverage in XML format (for CI)
coverage-xml:
    cargo tarpaulin --workspace --out Xml --skip-clean

# ============================================================================
# SECURITY AND DEPENDENCIES
# ============================================================================

# Run security audit
audit:
    @echo "🔒 Running security audit..."
    cargo audit

# Check for outdated dependencies
outdated:
    @echo "📦 Checking for outdated dependencies..."
    cargo outdated

# Update dependencies
update:
    cargo update

# Check dependency tree
tree:
    cargo tree --workspace

# ============================================================================
# BENCHMARKS AND PERFORMANCE
# ============================================================================

# Run benchmarks
bench:
    cargo bench --workspace

# Run latency benchmarks
bench-latency:
    cargo bench --workspace --features bench -- latency

# Run performance benchmarks
bench-perf:
    cargo bench --workspace --features bench -- perf

# ============================================================================
# DOCUMENTATION
# ============================================================================

# Generate and open documentation
doc:
    cargo doc --workspace --no-deps --open

# Generate documentation with private items
doc-private:
    cargo doc --workspace --no-deps --document-private-items --open

# Check documentation for warnings
doc-check:
    RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps

# ============================================================================
# CLEANING
# ============================================================================

# Clean build artifacts
clean:
    cargo clean
    rm -rf target/

# Clean and rebuild everything
clean-rebuild: clean build

# Remove all generated files
clean-all: clean
    find . -type f -name "*.profraw" -delete
    find . -type f -name "*.profdata" -delete
    rm -rf target/
    rm -rf tmp/
    rm -f *.log

# ============================================================================
# DEVELOPMENT WORKFLOW
# ============================================================================

# Watch for changes and run checks
watch:
    cargo watch -x check -x test

# Watch and run specific command
watch-cmd CMD:
    cargo watch -x {{CMD}}

# Fix common issues automatically
fix:
    cargo fix --workspace --allow-dirty
    cargo fmt --all
    cargo clippy --workspace --fix --allow-dirty

# ============================================================================
# CI/CD SIMULATION
# ============================================================================

# Simulate CI pipeline locally
ci-local: check-all coverage
    @echo ""
    @echo "✅ Local CI simulation complete"

# Run pre-merge checks (same as CI)
pre-merge:
    @echo "🚀 Running pre-merge validation..."
    @echo ""
    cargo check --workspace --all-targets
    cargo clippy --workspace --all-targets -- -D warnings
    cargo test --workspace --lib -- --skip redis --skip kill_switch
    cargo fmt --all -- --check
    @echo ""
    @echo "✅ Pre-merge validation passed - ready to merge!"

# ============================================================================
# SERVICE MANAGEMENT
# ============================================================================

# List all services
list-services:
    @echo "📦 Available services:"
    @ls -1 services/ 2>/dev/null || echo "No services directory found"
    @echo ""
    @echo "📦 Available crates:"
    @ls -1 crates/ 2>/dev/null || echo "No crates directory found"

# Build and run trading service
run-trading:
    cargo run --release --bin trading-service

# Build and run backtesting service
run-backtesting:
    cargo run --release --bin backtesting-service

# Build and run ML training service
run-ml-training:
    cargo run --release --bin ml-training-service

# Run TLI (Terminal Line Interface)
run-tli:
    cargo run --release --bin tli

# ============================================================================
# DATABASE MANAGEMENT
# ============================================================================

# Run database migrations
db-migrate:
    @echo "🗄️ Running database migrations..."
    sqlx migrate run

# Create new migration
db-migration NAME:
    @echo "📝 Creating new migration: {{NAME}}"
    sqlx migrate add {{NAME}}

# Reset database (DANGEROUS - drops all data)
db-reset:
    @echo "⚠️  WARNING: This will drop all database data!"
    @read -p "Are you sure? (y/N) " -n 1 -r && echo "" && if [[ $$REPLY =~ ^[Yy]$$ ]]; then sqlx database reset; fi

# ============================================================================
# UTILITIES
# ============================================================================

# Show environment information
env-info:
    @echo "🔧 Environment Information:"
    @echo ""
    @echo "Rust version:"
    @rustc --version
    @echo ""
    @echo "Cargo version:"
    @cargo --version
    @echo ""
    @echo "Workspace root:"
    @pwd
    @echo ""
    @echo "Git branch:"
    @git branch --show-current 2>/dev/null || echo "Not a git repository"

# Install development tools
install-tools:
    @echo "🔧 Installing development tools..."
    cargo install cargo-watch cargo-tarpaulin cargo-audit cargo-outdated cargo-deny
    @echo "✅ Development tools installed"

# Update Rust toolchain
update-rust:
    rustup update
    rustup component add clippy rustfmt

# ============================================================================
# HELPER RECIPES
# ============================================================================

# Show project statistics
stats:
    @echo "📊 Project Statistics:"
    @echo ""
    @echo "Total Rust files:"
    @find . -name "*.rs" -type f | wc -l
    @echo ""
    @echo "Total lines of code:"
    @find . -name "*.rs" -type f -exec cat {} \; | wc -l
    @echo ""
    @echo "Total crates:"
    @find . -name "Cargo.toml" -type f | wc -l
    @echo ""
    @echo "Workspace members:"
    @cargo metadata --no-deps --format-version 1 | jq -r '.workspace_members | length'

# Open project in favorite editor
edit:
    $EDITOR .
