.PHONY: help requirements req clean env pip_dev dev lint fmt openapi test test_unit test_integration test_integration_clean up down shell ps logs
.DEFAULT_GOAL := help

# define root project dir
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))

export APP_RUN_FROM_CLI = 1

help:
	@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

clean: ## Clean environment
	rm -rf .venv .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env pip_dev: ## Install dev requirements
	uv sync --locked

dev: ## Run dev
	uv run uvicorn preference_center.api.asgi:app --reload

lint: ## Run code linters
	uv run ty check
	uv run ruff check
	uv run ruff format --check

fmt: ## Run code formatters
	uv run ruff check preference_center tests --fix
	uv run ruff format preference_center tests

openapi: ## Generate OpenAPI specification
	uv run python manage.py api gen-spec

openapi-check: ## Check OpenAPI specification
	uv run python manage.py api check-spec

test: ## Run unit tests
	uv run pytest tests/unit/

test_unit: ## Run unit tests with coverage
	uv run pytest \
		--cov preference_center tests/unit/ \
		--cov-report xml \
		--cov-report term \
		--junitxml=pyunit.xml

test_integration: ## Run integration tests inside docker
	docker compose run --rm --build integration-tests

test_integration_clean: ## Shutdown docker integration tests
	docker compose down

up: ## Compose docker app
	docker compose up app -d --build --remove-orphans

down: ## Shutdown docker app
	docker compose down

shell sh: ## Spawn shell inside docker
	docker compose exec ows-preference-center /bin/sh

ps: ## List all processes
	docker compose ps

logs: ## View logs
	docker compose logs -f

ci_lint_and_test:  ## Run linters and unit tests inside docker
	docker compose run --rm --build lint-and-test

ci_lint_and_test_clean: down  ## Shutdown docker linters and unit tests
