.PHONY: help clean env pip_dev test_unit test_cov fmt lint
.PHONY: clean_wip_pdp generate_models
.DEFAULT_GOAL := help

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 env .cache .pytest_cache .mypy_cache .ruff_cache pyunit.xml .coverage coverage.xml
	find . | grep -E '__pycache__|\.pyc|\.pyo$\' | xargs rm -rf

env pip_dev:  ## Prepare environment
	poetry install --all-extras

test_unit: pip_dev  ## Run unit tests
	poetry run pytest tests/unit/ \
		--cov python_pdp_sdk \
		--cov-report xml \
		--cov-report term \
		--junitxml=pyunit.xml

test_cov: pip_dev  ## Run unit and integration tests with coverage
	poetry run pytest tests/ \
		--cov python_pdp_sdk \
		--cov-report term-missing

lint: pip_dev  ## Run code linters
	poetry run mypy python_pdp_sdk tests
	poetry run ruff check python_pdp_sdk tests
	poetry run ruff format python_pdp_sdk tests --check --diff

fmt:  pip_dev ## Run code formatters
	poetry run ruff check python_pdp_sdk tests --fix
	poetry run ruff format python_pdp_sdk tests

clean_wip_pdp:
	rm -rf wip_pdp

generate_models: clean_wip_pdp
	# Use Docker to generate models to WIP directory
	docker run --rm \
	-v ${PWD}:/local \
	-v ${PWD}/.openapi-generator-ignore:/local/wip_pdp/.openapi-generator-ignore \
	openapitools/openapi-generator-cli:v7.12.0 generate \
	--additional-properties=packageName=python_pdp_sdk.connectors.ows_pdp \
	-i https://qa-ows-pdp.theorchard.io/openapi.json \
	-g python \
	-o /local/wip_pdp

	# Move models from WIP directory to python_pdp_sdk package
	cp -r wip_pdp/.openapi-generator python_pdp_sdk/connectors/
	cp -r wip_pdp/python_pdp_sdk/connectors/ows_pdp/models python_pdp_sdk/connectors/ows_pdp/

	# Pre-emptively run formatter
	make fmt
