SHELL=/bin/bash
.DEFAULT_GOAL=_help

# [ENUM] Styling / Colors
STYLE_CYAN := $(shell tput setaf 6 2>/dev/null || echo -e "\033[36m")
STYLE_RESET := $(shell tput sgr0 2>/dev/null || echo -e "\033[0m")

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Initialization and Dependencies
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.PHONY: init
init: ##H Initialize the virtual environment
	python3 -m venv .venv
	@echo "✓ Virtual environment created in .venv/"

.PHONY: deps
deps: ##H Install dependencies and tools
	python3 -m pip install --upgrade pip
	python3 -m pip install -e ".[dev]"
	@echo "✓ Dependencies installed."

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Unit tests and local running
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.PHONY: test
test:   ##H Run unit tests with coverage
	PYTHONPATH=src python3 -m pytest --cov=src/scheduler_ta --cov-report=term-missing --cov-report=html tests/

.PHONY: test-v
test-v: ##H Run tests with stdout enabled (verbose)
	PYTHONPATH=src python3 -m pytest -s -v tests/

.PHONY: test-ci
test-ci: ##H Run tests for CI and generate reports
	PYTHONPATH=src python3 -m pytest -s -v --cov=src/scheduler_ta --cov-report=term --cov-report=xml:coverage.xml --junitxml=report.xml tests/

.PHONY: run
run:    ##H Run the application locally
	python3 src/scheduler_ta/main.py --help

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Linting, formatting
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

LINT_LOCS_PY = $$(git ls-files '*.py')

.PHONY: format
format: ##H Format the code using Black and isort
	python3 -m black $(LINT_LOCS_PY)
	python3 -m isort $(LINT_LOCS_PY)
	-prettier -w .
	-pre-commit run --all-files

.PHONY: lint
lint: ##H Lint the code using Flake8, Pylint, and Ruff
	python3 -m flake8 $(LINT_LOCS_PY)
	python3 -m pylint $(LINT_LOCS_PY)
	python3 -m ruff check $(LINT_LOCS_PY)

.PHONY: clean
clean: ##H Clean caches and temporary files
	find . -type f -name '*.pyc' -delete
	find . -type d -name '__pycache__' -exec rm -rf {} +
	rm -rf .pytest_cache
	rm -rf .coverage
	rm -rf .mypy_cache
	rm -rf dist/
	rm -rf build/

.PHONY: _help
_help: ##H Show this help, list available targets
	@grep -hE '^[a-zA-Z0-9_\/-]+:[[:space:]]*##H .*$$' $(MAKEFILE_LIST) \
		| awk 'BEGIN {FS = ":[[:space:]]*##H "}; {printf "$(STYLE_CYAN)%-15s$(STYLE_RESET) %s\n", $$1, $$2}'

# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Packaging and Releases
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.PHONY: build
build: ##H Build the source and wheel distributions
	python3 -m build
	twine check dist/*

.PHONY: publish-test
publish-test: build ##H Publish the package to TestPyPI
	twine upload --repository testpypi dist/*

.PHONY: publish
publish: build ##H Publish the package to PyPI
	twine upload dist/*
