# pyDocExtractor Development Commands
# Hexagonal Architecture Workflow

# Default recipe to display help information
default:
    @just --list

# Bootstrap: Install all dependencies including dev extras
bootstrap:
    uv sync --all-extras --group dev

# Format code with ruff
fmt:
    uv run ruff format src tests

# Lint code with ruff
lint:
    uv run ruff check src tests

# Fix linting issues automatically
fix:
    uv run ruff check --fix src tests

# Type check with mypy (strict mode)
types:
    uv run mypy src tests

# Guard architectural boundaries with import-linter
guard:
    uv run lint-imports

# Run all quality checks (format, lint, types, guard)
check:
    @echo "🔍 Running format check..."
    uv run ruff format --check src tests
    @echo "✓ Format check passed"
    @echo ""
    @echo "🔍 Running linter..."
    uv run ruff check src tests
    @echo "✓ Lint check passed"
    @echo ""
    @echo "🔍 Running type checker..."
    uv run mypy src tests
    @echo "✓ Type check passed"
    @echo ""
    @echo "🔍 Guarding architectural boundaries..."
    uv run lint-imports
    @echo "✓ Boundary check passed"
    @echo ""
    @echo "✅ All checks passed!"

# Run all tests
test:
    uv run pytest tests/

# Run unit tests only (domain + app layer)
test-unit:
    uv run pytest tests/unit/ -v

# Run unit tests with coverage
test-unit-coverage:
    uv run pytest tests/unit/ --cov=src/pydocextractor --cov-report=term-missing --cov-report=html

# Check coverage meets minimum threshold (70%)
coverage-check:
    uv run pytest tests/ --cov=src/pydocextractor --cov-report=term --cov-fail-under=70

# Type check with mypy (alias for types command)
typecheck:
    uv run mypy src tests

# Run adapter tests (infrastructure layer)
test-adapters:
    uv run pytest tests/adapters/ -v

# Run contract tests (port compliance)
test-contract:
    uv run pytest tests/ -k "contract" -v

# Run tests with coverage report
test-cov:
    uv run pytest tests/ --cov --cov-report=term-missing --cov-report=html

# Run integration tests only
test-integration:
    uv run pytest tests/integration/ -v

# Run benchmark tests only
test-benchmark:
    uv run pytest tests/benchmarks/ -v --benchmark-only

# Run benchmark tests with comparison
benchmark:
    uv run pytest tests/benchmarks/ --benchmark-only --benchmark-autosave

# Run BDD tests only (Behavior-Driven Development with pytest-bdd)
test-bdd:
    uv run pytest tests/bdd/ -v

# Run BDD tests with specific marker (e.g., just test-bdd-feature feature1)
test-bdd-feature FEATURE:
    uv run pytest tests/bdd/ -v -m "{{FEATURE}}"

# Run BDD tests for specific scenario (e.g., just test-bdd-scenario "Convert a PDF")
test-bdd-scenario SCENARIO:
    uv run pytest tests/bdd/ -v -k "{{SCENARIO}}"

# Compare benchmark results
benchmark-compare:
    uv run pytest tests/benchmarks/ --benchmark-only --benchmark-compare

# Clean build artifacts and cache
clean:
    rm -rf build/
    rm -rf dist/
    rm -rf *.egg-info
    rm -rf .pytest_cache
    rm -rf .mypy_cache
    rm -rf .ruff_cache
    rm -rf htmlcov/
    rm -rf .coverage
    find . -type d -name __pycache__ -exec rm -rf {} +
    find . -type f -name "*.pyc" -delete

# Build the package
build:
    uv build

# Install package in editable mode with all extras (for development)
install:
    uv pip install -e ".[all]"

# Install package in editable mode (minimal, no optional deps)
install-dev:
    uv pip install -e .

# Install package for production (non-editable)
install-prod:
    uv pip install .

# Install from PyPI (if published)
install-from-pypi:
    uv pip install pydocextractor

# Show project statistics
stats:
    @echo "📊 Project Statistics"
    @echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    @echo "Domain Layer:"
    @find src/pydocextractor/domain -name "*.py" | xargs wc -l | tail -1 | awk '{print "  Lines: " $$1}'
    @echo ""
    @echo "Application Layer:"
    @find src/pydocextractor/app -name "*.py" | xargs wc -l | tail -1 | awk '{print "  Lines: " $$1}'
    @echo ""
    @echo "Infrastructure Layer:"
    @find src/pydocextractor/infra -name "*.py" | xargs wc -l | tail -1 | awk '{print "  Lines: " $$1}'
    @echo ""
    @echo "Total Source:"
    @find src/pydocextractor -name "*.py" | xargs wc -l | tail -1 | awk '{print "  Lines: " $$1}'
    @echo ""
    @echo "Tests:"
    @find tests -name "*.py" | xargs wc -l | tail -1 | awk '{print "  Lines: " $$1}'

# Show dependency tree by layer
layers:
    @echo "📦 Hexagonal Architecture Layers"
    @echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    @echo ""
    @echo "🎯 Domain Layer (Pure - Zero Dependencies):"
    @ls -1 src/pydocextractor/domain/*.py | sed 's|src/pydocextractor/domain/||' | sed 's/^/  - /'
    @echo ""
    @echo "🔧 Application Layer (Uses Ports Only):"
    @ls -1 src/pydocextractor/app/*.py | sed 's|src/pydocextractor/app/||' | sed 's/^/  - /'
    @echo ""
    @echo "🔌 Infrastructure Layer (Implements Ports):"
    @echo "  Extractors:"
    @ls -1 src/pydocextractor/infra/extractors/*.py | sed 's|src/pydocextractor/infra/extractors/||' | sed 's/^/    - /'
    @echo "  Policy:"
    @ls -1 src/pydocextractor/infra/policy/*.py | sed 's|src/pydocextractor/infra/policy/||' | sed 's/^/    - /'
    @echo "  Templates:"
    @ls -1 src/pydocextractor/infra/templates/*.py | sed 's|src/pydocextractor/infra/templates/||' | sed 's/^/    - /'
    @echo "  Scoring:"
    @ls -1 src/pydocextractor/infra/scoring/*.py | sed 's|src/pydocextractor/infra/scoring/||' | sed 's/^/    - /'

# Demo: Convert a sample PDF with different precision levels
demo:
    @echo "🎬 pyDocExtractor Demo"
    @echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
    @echo ""
    @echo "This demo requires a sample PDF file."
    @echo "Usage: just demo <pdf-file>"
    @echo ""
    @echo "Example precision levels:"
    @echo "  1 - FASTEST (Chunked Parallel)"
    @echo "  2 - BALANCED (PyMuPDF4LLM) - Default"
    @echo "  3 - TABLE_OPTIMIZED (PDFPlumber)"
    @echo "  4 - HIGHEST_QUALITY (Docling)"
    @echo ""
    @echo "Run: uv run pydocextractor convert <file> --precision <level>"

# Run pre-commit checks before committing
pre-commit: fmt check test-unit test-contract
    @echo "✅ Pre-commit checks passed! Safe to commit."

# Run full CI pipeline locally
ci: check test-cov guard
    @echo "✅ Full CI pipeline passed!"

# Serve coverage report
serve-cov:
    python -m http.server 8000 --directory htmlcov

# Update dependencies
update:
    uv sync --upgrade
