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

export UV_FROZEN := "true"

# List available commands
[private]
@default:
    just --list
    echo ""
    echo "For help with a specific recipe, run: just --usage <recipe>"

# ============================================================================
# Development
# ============================================================================

# Run all quality checks
[group("dev")]
check: format lint test
    @echo; echo "✓ All checks passed!"

# Format the codebase
[group("dev")]
format:
    # Fix generic linting issues
    uv run ruff check --fix-only
    # Fix import-related issues (including ordering)
    uv run ruff check --select=I --fix-only
    # Format the code
    uv run ruff format

# Lint the codebase
[group("dev")]
lint:
    # Lint the code
    uv run ruff check
    # Run static checks
    uv run ty check src
    # Check for misspellings
    uv run codespell src
    # Check for dead code
    uv run vulture src
    # Run actionlint on GitHub Actions workflows
    test -d .github/workflows && uv run actionlint || true
    @echo "✓ Linting passed!"


# Run tests
[group("dev")]
test:
    uv run pytest

# Run tests with coverage information
[group("dev")]
coverage:
    uv run pytest --cov=src --cov-report=term-missing

# Sync and activate the virtual environment (.venv)
[group("dev")]
venv:
    @echo "Activating virtual environment..."
    @uv sync --all-groups --all-extras
    @. .venv/bin/activate; exec "$SHELL" -i

# ============================================================================
# Build & Package
# ============================================================================

# Build the project
[group("build")]
build:
    uv build

# Remove build artifacts and temporary files
[group("build")]
clean:
    # Remove __pycache__ directories
    find . -type d -name "__pycache__" -exec rm -r {} + || true
    # Remove .ruff_cache directory
    rm -rf .ruff_cache
    # Remove .pytest_cache directory
    rm -rf .pytest_cache
    # Remove build/dist/egg-info directories
    rm -rf build dist *.egg-info
    # Remove coverage reports
    rm -f .coverage coverage.xml

# ============================================================================
# Maintenance
# ============================================================================

# Update uv.lock dependencies
[group("maintenance")]
lock:
    unset UV_FROZEN; uv lock --upgrade --no-cache

# Scan for security vulnerabilities
[group("maintenance")]
scan:
    @echo "Scanning for security vulnerabilities..."
    uv audit

# ============================================================================
# Version & Release
# ============================================================================
# Releases are driven by the version in pyproject.toml: bump it (see `bump`
# below), merge to `main`, and the release workflow will detect the new
# version, tag it, create a GitHub Release, and publish to PyPI automatically.

# Bump the version in pyproject.toml
[group("release")]
[arg("level", long="level", pattern="^(major|minor|patch)$", help="Semantic version bump level")]
bump level:
    #!/usr/bin/env bash
    uv version --bump={{level}}

# Publish to PyPI (used by CI; requires authentication)
[group("release")]
publish: build
    #!/usr/bin/env bash
    uv publish
