.PHONY: help install format lint typecheck test openapi dev all-checks clean

# Use activated environment if available, otherwise use .venv explicitly
PYTHON := $(shell command -v python 2>/dev/null || echo .venv/bin/python)
PIP := $(shell command -v pip 2>/dev/null || echo .venv/bin/pip)

help:
	@echo "Available targets:"
	@echo "  install      - Create venv and install dependencies"
	@echo "  format       - Format code with ruff"
	@echo "  lint         - Lint code with ruff"
	@echo "  typecheck    - Type check with mypy"
	@echo "  test         - Run tests with pytest"
	@echo "  openapi      - Export OpenAPI schema"
	@echo "  dev          - Run development server"
	@echo "  all-checks   - Run all quality checks"
	@echo "  clean        - Remove venv and caches"
	@echo ""
	@echo "Note: Make sure to activate your venv first: source .venv/bin/activate"

install:
	@echo "Creating virtual environment..."
	python3 -m venv .venv
	@echo "Installing dependencies..."
	.venv/bin/pip install --upgrade pip
	.venv/bin/pip install -r requirements.txt
	.venv/bin/pip install -r dev-requirements.txt
	@echo "Installing pre-commit hooks..."
	.venv/bin/pre-commit install
	@echo "Done! Activate with: source .venv/bin/activate"

format:
	$(PYTHON) -m ruff format .

lint:
	$(PYTHON) -m ruff check .

typecheck:
	$(PYTHON) -m mypy .

test:
	$(PYTHON) -m pytest -q

openapi:
	$(PYTHON) scripts/export_openapi.py

dev:
	$(PYTHON) -m uvicorn main:app --reload

all-checks: lint typecheck test
	@echo "Running format check..."
	$(PYTHON) -m ruff format --check .
	@echo "Exporting OpenAPI schema..."
	$(PYTHON) scripts/export_openapi.py
	@echo ""
	@echo "✓ All checks passed!"

clean:
	rm -rf .venv
	find . -type d -name __pycache__ -exec rm -rf {} +
	find . -type d -name .pytest_cache -exec rm -rf {} +
	find . -type d -name .mypy_cache -exec rm -rf {} +
	find . -type d -name .ruff_cache -exec rm -rf {} +
