# ECHIDNA Justfile
# =============
#
# Build, test, and benchmark targets for the ECHIDNA theorem proving system
#
# Usage:
#   just build      - Build the system
#   just test       - Run all tests
#   just bench      - Run all benchmarks
#   just check      - Build + test + bench
#   just clean      - Clean build artifacts

# Configuration
idris2 := "idris2"
test_dir := "test"
bench_dir := "bench"
build_dir := "build"

# Build targets
build:
  @echo "Building ECHIDNA..."
  mkdir -p @{build_dir}
  @{idris2} --build Echidna.ipkg -o @{build_dir}/echidna
  @echo "✓ Build complete"

# Test targets
test:
  @echo "Running ECHIDNA tests..."
  @{idris2} --test @{test_dir}/VocabularyTest.idr
  @echo "✓ Tests complete"

# Benchmark targets
bench:
  @echo "Running ECHIDNA benchmarks..."
  @{idris2} --exec @{bench_dir}/VocabularyBench.idr
  @echo "✓ Benchmarks complete"

# Combined check target
check: build test bench
  @echo "✓ All checks passed"

# Clean target
clean:
  @echo "Cleaning build artifacts..."
  rm -rf @{build_dir}
  rm -rf .idris2
  @echo "✓ Clean complete"

# Development targets
dev:
  @echo "Development mode: watching for changes..."
  @{idris2} --watch Echidna.ipkg

# Documentation targets
doc:
  @echo "Generating documentation..."
  # Would integrate with documentation generator
  @echo "Documentation generated"

# CI targets
ci-test:
  @echo "Running CI test suite..."
  just test
  # Exit with error code if tests fail
  @echo "CI tests passed"

ci-bench:
  @echo "Running CI benchmarks..."
  just bench
  @echo "CI benchmarks completed"

# Help target (default)
default:
  @echo "ECHIDNA Justfile Help"
  @echo "====================="
  @echo ""
  @echo "Build:"
  @echo "  just build      - Build the ECHIDNA system"
  @echo "  just clean      - Clean build artifacts"
  @echo ""
  @echo "Testing:"
  @echo "  just test       - Run unit tests"
  @echo "  just bench      - Run performance benchmarks"
  @echo "  just check      - Build + test + bench"
  @echo ""
  @echo "Development:"
  @echo "  just dev        - Development mode with file watching"
  @echo "  just doc        - Generate documentation"
  @echo ""
  @echo "CI/CD:"
  @echo "  just ci-test    - Run CI test suite"
  @echo "  just ci-bench   - Run CI benchmarks"
  @echo ""
  @echo "Misc:"
  @echo "  just             - Show this help message"

# Test coverage target
coverage:
  @echo "Measuring test coverage..."
  # Would integrate with coverage tools
  @echo "Coverage measurement would be implemented"

# Lint target
lint:
  @echo "Running linter..."
  # Would integrate with Idris linter
  @echo "Linting would be implemented"

# Format target
format:
  @echo "Formatting code..."
  # Would integrate with formatter
  @echo "Formatting would be implemented"

# Release target
release version:
  @echo "Creating release @{version}..."
  # Would create release artifacts
  @echo "Release @{version} would be created"

# Install target
install:
  @echo "Installing ECHIDNA..."
  just build
  cp @{build_dir}/echidna /usr/local/bin/echidna
  @echo "✓ ECHIDNA installed to /usr/local/bin/echidna"

# Uninstall target
uninstall:
  @echo "Uninstalling ECHIDNA..."
  rm -f /usr/local/bin/echidna
  @echo "✓ ECHIDNA uninstalled"

# Benchmark comparison target
bench-compare:
  @echo "Comparing benchmarks..."
  # Would compare current vs previous benchmarks
  @echo "Benchmark comparison would be implemented"

# Test specific components
test-vocabulary:
  @echo "Running vocabulary tests..."
  @{idris2} --test @{test_dir}/VocabularyTest.idr

bench-vocabulary:
  @echo "Running vocabulary benchmarks..."
  @{idris2} --exec @{bench_dir}/VocabularyBench.idr

# Generate test report
test-report:
  @echo "Generating test report..."
  mkdir -p reports
  @{idris2} --test @{test_dir}/VocabularyTest.idr > reports/test-report-$(date +%Y%m%d-%H%M%S).txt
  @echo "✓ Test report generated in reports/"

# Generate benchmark report
bench-report:
  @echo "Generating benchmark report..."
  mkdir -p reports
  @{idris2} --exec @{bench_dir}/VocabularyBench.idr > reports/bench-report-$(date +%Y%m%d-%H%M%S).txt
  @echo "✓ Benchmark report generated in reports/"

# Run security tests
security-test:
  @echo "Running security tests..."
  # Would run specialized security tests
  @echo "Security tests would be implemented"

# Run integration tests
integration-test:
  @echo "Running integration tests..."
  # Would run integration tests
  @echo "Integration tests would be implemented"

# Run all tests
all-tests: test-vocabulary security-test integration-test
  @echo "✓ All tests completed"

# Run all benchmarks
all-benchmarks: bench-vocabulary
  @echo "✓ All benchmarks completed"

# Profile target
profile:
  @echo "Profiling ECHIDNA..."
  # Would run profiling tools
  @echo "Profiling would be implemented"

# Memory analysis target
memory:
  @echo "Analyzing memory usage..."
  # Would analyze memory usage
  @echo "Memory analysis would be implemented"

# Generate API documentation
api-doc:
  @echo "Generating API documentation..."
  # Would generate API docs
  @echo "API documentation would be generated"

# Run examples
examples:
  @echo "Running examples..."
  # Would run example programs
  @echo "Examples would run"

# Update dependencies
deps-update:
  @echo "Updating dependencies..."
  # Would update dependencies
  @echo "Dependencies would be updated"

# Audit dependencies
deps-audit:
  @echo "Auditing dependencies..."
  # Would audit dependencies for security
  @echo "Dependency audit would be performed"

# Check for updates
update-check:
  @echo "Checking for updates..."
  # Would check for updates
  @echo "Update check would be performed"

# System health check
health:
  @echo "Checking system health..."
  just test
  just bench
  @echo "✓ System healthy"

# Backup target
backup:
  @echo "Creating backup..."
  mkdir -p backups
  tar -czf backups/echidna-$(date +%Y%m%d-%H%M%S).tar.gz src/ test/ bench/ Justfile
  @echo "✓ Backup created in backups/"

# Restore target
restore file:
  @echo "Restoring from backup @{file}..."
  tar -xzf @{file}
  @echo "✓ Restored from @{file}"

# Initialize project
init:
  @echo "Initializing ECHIDNA project..."
  mkdir -p src test bench reports backups
  @echo "✓ Project initialized"

# Verify installation
verify:
  @echo "Verifying ECHIDNA installation..."
  which echidna || echo "ECHIDNA not found in PATH"
  echidna --version 2>/dev/null || echo "ECHIDNA not executable"
  @echo "✓ Verification complete"

# Show version
version:
  @echo "ECHIDNA version 0.2.0"
  @echo "Vocabulary: 6000+ terms"
  @echo "Provers: 30+ core, scalable registry"
  @echo "Built: $(date)"

# Show environment info
env:
  @echo "ECHIDNA Environment Info"
  @echo "========================"
  @{idris2} --version
  uname -a
  @echo "ECHIDNA_DIR=$(pwd)"

# Clean everything
nuke:
  @echo "🔥 NUKING everything..."
  rm -rf @{build_dir} .idris2 reports/ backups/
  @echo "✓ Everything nuked"

# Doctor target (fix common issues)
doctor:
  @echo "Running ECHIDNA doctor..."
  just clean
  just build
  @echo "✓ Doctor completed"

# CI full workflow
ci-full:
  @echo "Running full CI workflow..."
  just clean
  just build
  just test
  just bench
  just test-report
  just bench-report
  @echo "✓ CI workflow completed"

# Development setup
dev-setup:
  @echo "Setting up development environment..."
  just init
  just build
  @echo "✓ Development environment ready"

# Production setup
prod-setup:
  @echo "Setting up production environment..."
  just clean
  just build
  just install
  @echo "✓ Production environment ready"

# Smoke test
smoke:
  @echo "Running smoke tests..."
  just test-vocabulary
  @echo "✓ Smoke tests passed"

# Sanity check
sanity:
  @echo "Running sanity checks..."
  just build
  just smoke
  @echo "✓ Sanity checks passed"

# Quick check
quick:
  @echo "Running quick checks..."
  just test
  @echo "✓ Quick checks passed"

# Full validation
validate:
  @echo "Running full validation..."
  just build
  just test
  just bench
  just health
  @echo "✓ Full validation passed"

# Pre-commit hook
precommit:
  @echo "Running pre-commit checks..."
  just test
  just bench
  # Would include lint, format, etc.
  @echo "✓ Pre-commit checks passed"

# Pre-release checklist
prerelease:
  @echo "Running pre-release checklist..."
  just validate
  just doc
  just version
  @echo "✓ Pre-release checklist completed"

# Post-release cleanup
postrelease:
  @echo "Running post-release cleanup..."
  just clean
  just doctor
  @echo "✓ Post-release cleanup completed"

# Emergency fix
emergency:
  @echo "🚨 EMERGENCY MODE 🚨"
  just nuke
  just init
  just build
  just smoke
  @echo "✓ Emergency recovery completed"

# Interactive setup
interactive:
  @echo "Interactive ECHIDNA setup"
  @echo "1. Development setup"
  @echo "2. Production setup"
  @echo "3. Test setup"
  @echo "4. Benchmark setup"
  @echo "5. Exit"
  read -p "Choose option: " choice
  case $$choice in
    1) just dev-setup ;;
    2) just prod-setup ;;
    3) just test ;;
    4) just bench ;;
    *) echo "Exiting" ;;
  esac

# Batch mode
batch file:
  @echo "Running batch commands from @{file}..."
  while read -r line; do
    just $$line
  done < @{file}
  @echo "✓ Batch completed"

# Self-update
self-update:
  @echo "Updating Justfile..."
  # Would implement self-update logic
  @echo "Self-update would be implemented"

# Help alias
h: default
? : default
-? : default
--help : default
-help : default

# Version alias
-v : version
--version : version
-vv : version

# Clean alias
c : clean
cl : clean
cln : clean

# Build alias
b : build
bl : build
bld : build

# Test alias
t : test
ts : test
tst : test

# Benchmark alias
bn : bench
bch : bench
bmark : bench

# Check alias
chk : check
ch : check
ck : check

# Development alias
d : dev
dv : dev
dvl : dev

# Help alias
help : default
h : default
? : default
secret-scan-trufflehog:
    @command -v trufflehog >/dev/null && trufflehog filesystem . --only-verified || true
