# justfile - Task runner for the project
# Use 'just --list' to see all available commands

# --- Variables ---
# Use uv to run commands in the virtual env automatically
run := "uv run"
python := "uv run python"

# --- Recipes ---
# List all available commands
default:
    @just --list

# ==========================================
# 🐣 Setup
# ==========================================

# Setup the development environment
setup:
    @echo "🐣 Setting up development environment..."
    just install

# ==========================================
# 📦 Dependency Management
# ==========================================

# Install dependencies from uv.lock
install:
    @echo "🚀 Syncing dependencies..."
    uv sync --all-extras --dev

# Update dependencies to latest allowed versions and update lockfile
update:
    @echo "🔄 Updating dependencies..."
    uv lock --upgrade

# Add a new dependency
add *packages:
    @echo "➕ Adding packages: {{packages}}"
    uv add {{packages}}

# Add a new dev dependency
add-dev *packages:
    @echo "➕ Adding dev packages: {{packages}}"
    uv add --group dev {{packages}}

# ==========================================
# 💅 Code Quality & Formatting
# ==========================================

# Format code (Ruff) and fix auto-fixable lint errors
fmt:
    @echo "✨ Formatting code..."
    {{run}} ruff format .
    {{run}} ruff check --fix .

# Run static analysis (Ruff + Mypy). CI should run this.
lint:
    @echo "🧐 Linting (Ruff)..."
    {{run}} ruff check .
    @echo "🧠 Type Checking (Mypy)..."
    {{run}} mypy .

# Check formatting without making changes
check:
    @echo "🔍 Checking code format..."
    {{run}} ruff format --check .
    {{run}} ruff check .

# ==========================================
# 🧪 Testing
# ==========================================

# Run unit tests with pytest
test:
    @echo "🧪 Running tests..."
    {{run}} pytest

# Run tests with coverage report
test-cov:
    @echo "📊 Running tests with coverage..."
    {{run}} pytest --cov=src --cov-report=term-missing

# Run only unit tests (fast)
test-unit:
    @echo "🧪 Running unit tests..."
    {{run}} pytest -m unit

# Run only integration tests
test-integration:
    @echo "🔗 Running integration tests..."
    {{run}} pytest -m integration

# ==========================================
# 🏃 Execution
# ==========================================

# Run the FastAPI development server with hot reload
dev:
    @echo "🔥 Starting Dev Server..."
    {{run}} fastapi dev src/main.py

# Run the production server (uvicorn)
serve:
    @echo "🚀 Starting Production Server..."
    {{run}} uvicorn src.main:app --host 0.0.0.0 --port 8000

# Run a Python script with env vars loaded
run *args:
    @set -a && source .env 2>/dev/null || true && set +a && {{python}} {{args}}

# ==========================================
# 🧹 Maintenance
# ==========================================

# Clean up cache files (.ruff_cache, .pytest_cache, __pycache__)
clean:
    @echo "🗑️ Cleaning artifacts..."
    rm -rf .ruff_cache .pytest_cache .mypy_cache .coverage htmlcov
    find . -type d -name "__pycache__" -exec rm -rf {} + 2>/dev/null || true
    find . -type f -name "*.pyc" -delete 2>/dev/null || true

# ==========================================
# 🚀 Publishing
# ==========================================

# Publish to PyPI (e.g., just publish v0.0.2)
publish version:
    @echo "📦 Publishing {{version}} to PyPI..."
    uv version {{version}}
    rm -rf dist/*
    uv build
    @set -a && source .env && set +a && uv publish --token=$UV_PUBLISH_TOKEN

# Publish to PyPI and create GitHub release (e.g., just publish_and_release v0.0.2 "Bug fixes and improvements")
publish_and_release version message:
    @echo "📦 Publishing {{version}} to PyPI..."
    uv version {{version}}
    rm -rf dist/*
    uv build
    @set -a && source .env && set +a && uv publish --token=$UV_PUBLISH_TOKEN
    @echo "🏷️ Creating GitHub tag and release..."
    git tag -a {{version}} -m "Release version {{version}}"
    git push origin {{version}}
    gh release create {{version}} --title "{{version}}" --notes "{{message}}"

# ==========================================
# 📚 Documentation
# ==========================================

# Serve documentation locally
docs:
    @echo "📚 Serving documentation..."
    {{run}} mkdocs serve

# Build documentation
docs-build:
    @echo "📦 Building documentation..."
    {{run}} mkdocs build

