.PHONY: help install types lint format typecheck test test-cov clean

help:  ## Show this help
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-15s\033[0m %s\n", $$1, $$2}'

install:  ## Install package + dev dependencies
	pip install -e ".[dev]"

types:  ## Regenerate TypeScript types from Pydantic JSON Schema
	@echo "Exporting JSON Schema from Pydantic models..."
	@python -m api_medic.core.export_schema > /tmp/api-medic-schema.json
	@echo "Generating TypeScript types via quicktype..."
	@mkdir -p frontend/src/lib
	@npx --yes quicktype@23 \
		--src-lang schema \
		--lang typescript \
		--just-types \
		--prefer-unions \
		--no-date-times \
		--top-level Report \
		-o frontend/src/lib/types.ts \
		/tmp/api-medic-schema.json
	@echo "/* eslint-disable */" > /tmp/types.header
	@echo "/**" >> /tmp/types.header
	@echo " * AUTO-GENERATED by 'make types' from Pydantic models in src/api_medic/core/models.py." >> /tmp/types.header
	@echo " * Do not edit by hand. Run 'make types' to regenerate." >> /tmp/types.header
	@echo " */" >> /tmp/types.header
	@cat /tmp/types.header frontend/src/lib/types.ts > /tmp/types.combined
	@mv /tmp/types.combined frontend/src/lib/types.ts
	@rm /tmp/types.header /tmp/api-medic-schema.json
	@echo "Wrote frontend/src/lib/types.ts"

lint:  ## Run ruff lint
	ruff check .

format:  ## Format code with ruff
	ruff format .

typecheck:  ## Run mypy
	mypy src

test:  ## Run tests
	pytest

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

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