# Foxhunt HFT Trading System - GNU Make Automation
# Alternative to justfile for environments where Make is preferred
#
# Usage: make <target>
# Example: make check-all

.PHONY: default help check-all pre-commit check build build-release clean \
        test test-unit test-integration test-fast coverage fmt clippy \
        audit outdated doc run-trading run-tli ci-local

# Default target
default: help

# ============================================================================
# HELP AND INFORMATION
# ============================================================================

help:
	@echo "Foxhunt HFT Trading System - Available Make Targets"
	@echo ""
	@echo "Quick Checks:"
	@echo "  pre-commit       - Fast checks before committing"
	@echo "  check-all        - Full quality gate checks"
	@echo ""
	@echo "Build Targets:"
	@echo "  check            - Check compilation"
	@echo "  build            - Build all services (debug)"
	@echo "  build-release    - Build all services (release)"
	@echo "  clean            - Clean build artifacts"
	@echo ""
	@echo "Testing:"
	@echo "  test             - Run all tests"
	@echo "  test-unit        - Run unit tests only"
	@echo "  test-integration - Run integration tests"
	@echo "  test-fast        - Run tests excluding external deps"
	@echo "  coverage         - Generate code coverage report"
	@echo ""
	@echo "Code Quality:"
	@echo "  fmt              - Format code"
	@echo "  clippy           - Run clippy lints"
	@echo "  warnings         - Count warnings in codebase"
	@echo ""
	@echo "Security:"
	@echo "  audit            - Run security audit"
	@echo "  outdated         - Check for outdated dependencies"
	@echo ""
	@echo "Services:"
	@echo "  run-trading      - Run trading service"
	@echo "  run-tli          - Run terminal interface"
	@echo ""
	@echo "CI/CD:"
	@echo "  ci-local         - Simulate CI pipeline locally"
	@echo "  pre-merge        - Run pre-merge validation"

# ============================================================================
# QUICK CHECKS
# ============================================================================

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"

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!"

# ============================================================================
# BUILD TARGETS
# ============================================================================

check:
	cargo check --workspace --all-targets

build:
	cargo build --workspace

build-release:
	cargo build --release --workspace

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

test:
	cargo test --workspace

test-unit:
	cargo test --workspace --lib

test-integration:
	cargo test --workspace --tests

test-fast:
	cargo test --workspace --lib -- --skip redis --skip kill_switch

test-verbose:
	cargo test --workspace -- --nocapture

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

coverage:
	@echo "📊 Generating code coverage report..."
	cargo tarpaulin --workspace --out Html --skip-clean
	@echo ""
	@echo "✅ Coverage report generated: target/tarpaulin/index.html"

coverage-lcov:
	cargo tarpaulin --workspace --out Lcov --skip-clean

coverage-xml:
	cargo tarpaulin --workspace --out Xml --skip-clean

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

fmt:
	cargo fmt --all

fmt-check:
	cargo fmt --all -- --check

clippy:
	cargo clippy --workspace --all-targets -- -D warnings

clippy-all:
	cargo clippy --workspace --all-targets --all-features -- -W clippy::all -W clippy::pedantic

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

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

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

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

update:
	cargo update

tree:
	cargo tree --workspace

# ============================================================================
# BENCHMARKS
# ============================================================================

bench:
	cargo bench --workspace

bench-latency:
	cargo bench --workspace --features bench -- latency

bench-perf:
	cargo bench --workspace --features bench -- perf

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

doc:
	cargo doc --workspace --no-deps --open

doc-private:
	cargo doc --workspace --no-deps --document-private-items --open

doc-check:
	RUSTDOCFLAGS="-D warnings" cargo doc --workspace --no-deps

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

clean:
	cargo clean
	rm -rf target/

clean-all: clean
	find . -type f -name "*.profraw" -delete
	find . -type f -name "*.profdata" -delete
	rm -rf tmp/
	rm -f *.log

clean-rebuild: clean build

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

watch:
	cargo watch -x check -x test

fix:
	cargo fix --workspace --allow-dirty
	cargo fmt --all
	cargo clippy --workspace --fix --allow-dirty

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

ci-local: check-all coverage
	@echo ""
	@echo "✅ Local CI simulation complete"

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-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"

run-trading:
	cargo run --release --bin trading-service

run-backtesting:
	cargo run --release --bin backtesting-service

run-ml-training:
	cargo run --release --bin ml-training-service

run-tli:
	cargo run --release --bin tli

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

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

db-reset:
	@echo "⚠️  WARNING: This will drop all database data!"
	@read -p "Are you sure? (y/N) " -r; \
	if [ "$$REPLY" = "y" ] || [ "$$REPLY" = "Y" ]; then \
		sqlx database reset; \
	fi

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

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-tools:
	@echo "🔧 Installing development tools..."
	cargo install cargo-watch cargo-tarpaulin cargo-audit cargo-outdated cargo-deny
	@echo "✅ Development tools installed"

update-rust:
	rustup update
	rustup component add clippy rustfmt

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
