#* Variables
SHELL := /usr/bin/env bash
PYTHON := python
PYTHONPATH := `pwd`

#* Docker variables
IMAGE := biomassage
VERSION := latest

#* Hatch environment (default)
HATCH := hatch
ENV_NAME := default

#* Installation
.PHONY: install
install:
	$(HATCH) env create $(ENV_NAME)
	$(HATCH) run pip install -e .

.PHONY: pre-commit-install
pre-commit-install:
	$(HATCH) run pre-commit install

#* Formatters
.PHONY: codestyle
codestyle:
	$(HATCH) run pyupgrade --exit-zero-even-if-changed --py39-plus **/*.py
	$(HATCH) run isort --settings-path pyproject.toml ./
	$(HATCH) run black --config pyproject.toml ./

.PHONY: formatting
formatting: codestyle

#* Linting
.PHONY: test
test:
	$(HATCH) run pytest -c pyproject.toml --cov-report=html --cov=biomassage tests/
	$(HATCH) run coverage-badge -o assets/images/coverage.svg -f

.PHONY: check-codestyle
check-codestyle:
	$(HATCH) run isort --diff --check-only --settings-path pyproject.toml ./
	$(HATCH) run black --diff --check --config pyproject.toml ./
	$(HATCH) run darglint --verbosity 2 biomassage tests

.PHONY: mypy
mypy:
	$(HATCH) run mypy --config-file pyproject.toml ./

.PHONY: check-safety
check-safety:
	$(HATCH) run safety check --full-report
	$(HATCH) run bandit -ll --recursive biomassage tests

.PHONY: lint
lint: test check-codestyle mypy check-safety

.PHONY: update-dev-deps
update-dev-deps:
	$(HATCH) run pip install --upgrade bandit darglint isort mypy pre-commit pydocstyle pylint pytest pyupgrade safety coverage coverage-badge pytest-html pytest-cov black

#* Docker
.PHONY: docker-build
docker-build:
	@echo Building docker $(IMAGE):$(VERSION) ...
	docker build \
		-t $(IMAGE):$(VERSION) . \
		-f ./docker/Dockerfile --no-cache

.PHONY: docker-remove
docker-remove:
	@echo Removing docker $(IMAGE):$(VERSION) ...
	docker rmi -f $(IMAGE):$(VERSION)

#* Documentation
.PHONY: html
html:
	$(HATCH) run sphinx-build -b html docs/source docs/build

#* Cleaning
.PHONY: pycache-remove
pycache-remove:
	find . | grep -E "(__pycache__|\.pyc|\.pyo$$)" | xargs rm -rf

.PHONY: dsstore-remove
dsstore-remove:
	find . | grep -E ".DS_Store" | xargs rm -rf

.PHONY: mypycache-remove
mypycache-remove:
	find . | grep -E ".mypy_cache" | xargs rm -rf

.PHONY: ipynbcheckpoints-remove
ipynbcheckpoints-remove:
	find . | grep -E ".ipynb_checkpoints" | xargs rm -rf

.PHONY: pytestcache-remove
pytestcache-remove:
	find . | grep -E ".pytest_cache" | xargs rm -rf

.PHONY: build-remove
build-remove:
	rm -rf build/

.PHONY: cleanup
cleanup: pycache-remove dsstore-remove mypycache-remove ipynbcheckpoints-remove pytestcache-remove
