# SPDX-License-Identifier: LGPL-3.0-or-later
# Copyright (C) 2026 Shibaji Chakraborty
#
# SQUITTER developer entry points.  `make` (or `make help`) lists them.
# Override the interpreter with  make PY=/path/to/python  (default: the conda env named "squitter").

ENV      ?= squitter
CONDA    ?= conda
PY       ?= $(shell $(CONDA) run -n $(ENV) which python 2>/dev/null || echo python)
OUT      ?= outputs

# ---- colours (disabled with NO_COLOR=1) ----
ifeq ($(NO_COLOR),)
  C0 := \033[0m
  CB := \033[1m
  CR := \033[38;5;203m
  CO := \033[38;5;214m
  CG := \033[38;5;78m
  CC := \033[38;5;45m
  CP := \033[38;5;177m
  CD := \033[2m
endif

.DEFAULT_GOAL := help
.PHONY: publish-test publish api-docs docs docs-serve cov-xml cov-floors check release-check doc-figures help banner env install dev test cov lint fmt format fmt-check typecheck dupes spdx docstyle deadcode deps audit changelog-check pre-commit pre-commit-install qa golden update-golden slow gates smoke figures examples logo tree build clean distclean

help: banner ## Show this list
	@printf "$(CB)Usage:$(C0) make $(CC)<target>$(C0)\n\n"
	@awk 'BEGIN {FS = ":.*##"} /^[a-zA-Z0-9_%-]+:.*##/ {printf "  \033[38;5;45m%-16s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@printf "\n$(CD)env: $(ENV)   python: $(PY)$(C0)\n"
	@printf "$(CD)gate order: constants -> G -> A -> W -> L -> smoke -> I -> C/N$(C0)\n"

banner:
	@printf "$(CO)$(CB)"
	@printf "   ____   ___  _   _ ___ _____ _____ _____ ____ \n"
	@printf "  / ___| / _ \\\\| | | |_ _|_   _|_   _| ____|  _ \\\\\n"
	@printf "  \\\\___ \\\\| | | | | | || |  | |   | | |  _| | |_) |\n"
	@printf "   ___) | |_| | |_| || |  | |   | | | |___|  _ < \n"
	@printf "  |____/ \\\\__\\\\_\\\\\\\\___/|___| |_|   |_| |_____|_| \\\\_\\\\\n"
	@printf "$(C0)$(CP)  ADS-B/C Link Outage, Fading, and Degradation from Ionospheric Irregularities Simulator$(C0)\n"
	@printf "$(CD)  ionospheric irregularities  ->  scintillation (S4)  ->  ADS-B / ADS-C / GNSS message success$(C0)\n\n"

env: ## Create the conda environment (numpy, scipy only)
	@printf "$(CG)creating conda env '$(ENV)'$(C0)\n"
	$(CONDA) env create -f environment.yml -y

install: ## Install the package (runtime dependencies only)
	$(PY) -m pip install .

dev: ## Editable install with the dev and plots extras (pytest, ruff, mypy, matplotlib, ...)
	$(PY) -m pip install -e ".[dev,plots]"

test: ## Run the tests
	@printf "$(CG)pytest$(C0)\n"
	$(PY) -m pytest

cov: ## Tests with a coverage report
	$(PY) -m pytest --cov=squitter --cov-report=term-missing

cov-xml: ## Tests with coverage.xml, then the per-package coverage floors
	$(PY) -m pytest --cov=squitter --cov-report=xml -q
	$(PY) scripts/check_coverage_floors.py

cov-floors: ## Check the per-package coverage floors against an existing coverage.xml
	$(PY) scripts/check_coverage_floors.py

slow: ## Run slow tests only
	$(PY) -m pytest -m slow

golden: ## Run the golden (fixed-seed) tests only
	$(PY) -m pytest -m golden

update-golden: ## Regenerate the W6 and L8 golden files (only when the numbers are meant to change; the characterization file is never regenerated here)
	$(PY) tests/golden/generate_w6.py
	$(PY) tests/golden/generate_l8.py

gates: ## Run every gate marker, one by one
	@for g in G A W L C N I; do \
		printf "$(CB)$(CG)== gate_$$g ==$(C0)\n"; \
		$(PY) -m pytest -m gate_$$g -q || [ $$? -eq 5 ]; \
	done

gate-%: ## Run one gate, e.g. make gate-G (G A W L C N I)
	$(PY) -m pytest -m gate_$*

smoke: ## End-to-end smoke test (aircraft -> LEO 1090 MHz hop)
	$(PY) -m pytest tests/analytic/test_smoke.py -v

lint: ## Lint with ruff
	@printf "$(CC)ruff check$(C0)\n"
	$(PY) -m ruff check src tests scripts examples functions

fmt: ## Auto-fix what ruff can fix
	$(PY) -m ruff check --fix src tests scripts examples functions

format: ## Format the code with ruff format (line width 110)
	$(PY) -m ruff format src tests scripts examples functions

fmt-check: ## Check the formatting without changing files
	@printf "$(CC)ruff format --check$(C0)\n"
	$(PY) -m ruff format --check src tests scripts examples functions

typecheck: ## Type-check src/squitter with mypy (a ratchet, see pyproject.toml)
	@printf "$(CC)mypy$(C0)\n"
	$(PY) -m mypy

dupes: ## Duplicate-code check (pylint R0801, see .pylintrc)
	@printf "$(CP)pylint duplicate-code$(C0)\n"
	$(PY) -m pylint --rcfile=.pylintrc src/squitter tests

docstyle: ## Docstring coverage of the public API must be 100% (interrogate)
	@printf "$(CP)interrogate$(C0)\n"
	$(PY) -m interrogate src/squitter

deadcode: ## Dead code: anything defined and never used (vulture)
	@printf "$(CP)vulture$(C0)\n"
	$(PY) -m vulture

deps: ## Dependency hygiene: declared, used, no transitive imports (deptry)
	@printf "$(CP)deptry$(C0)\n"
	$(PY) -m deptry src

audit: ## Known vulnerabilities in the installed dependencies (pip-audit; needs the network)
	$(PY) -m pip_audit --progress-spinner off

release-check: ## The version in the code, CITATION.cff and CHANGELOG.md agree (add TAG=vX.Y.Z to compare a tag); fails on a dev version
	$(PY) scripts/check_release.py $(if $(TAG),--tag $(TAG),)

spdx: ## Check the SPDX license header on every Python file
	$(PY) scripts/check_spdx.py

changelog-check: ## CHANGELOG.md must change with src/squitter (staged files)
	$(PY) scripts/check_changelog.py

pre-commit-install: ## Install the git hooks (pre-commit and pre-push)
	$(PY) -m pre_commit install --hook-type pre-commit --hook-type pre-push

pre-commit: ## Run every pre-commit hook on all tracked and staged files
	$(PY) -m pre_commit run --all-files

qa: lint fmt-check typecheck docstyle deadcode deps dupes spdx test ## Everything that should pass before a commit
	@printf "$(CG)$(CB)qa passed$(C0)\n"

figures: ## Run functions/demo_*.py -> outputs/func_out/*.png (git-ignored)
	@for s in functions/demo_*.py; do printf "$(CB)$(CG)== $$s ==$(C0)\n"; $(PY) $$s || exit 1; done

examples: ## Run examples/*.py (print and plot numbers to check against what you know)
	@for f in examples/[0-9]*.py; do printf "$(CC)%s$(C0)\n" $$f; $(PY) $$f || exit 1; done

doc-figures: ## Redraw the model-description illustrations in docs/figures/ from the package
	$(PY) scripts/make_doc_figures.py

logo: ## Regenerate the logo SVGs in docs/_static/ (pen-and-ink style; needs fonttools, extra "docs")
	$(PY) scripts/make_logo.py

link-glossary: ## Link the first use of each glossary term in every guide section (idempotent)
	$(PY) scripts/link_glossary.py

api-docs: ## Regenerate the API reference pages from the package layout
	$(PY) scripts/gen_api_docs.py

docs: api-docs ## Build the documentation (Sphinx, warnings are errors)
	$(PY) -m sphinx -W --keep-going -b html docs docs/_build/html

docs-serve: docs ## Build the documentation and serve it on http://localhost:8000
	$(PY) -m http.server 8000 --directory docs/_build/html

build: ## Build sdist and wheel
	$(PY) -m build

check: build ## Check the built distributions (twine, and that they hold what they should)
	$(PY) -m twine check dist/*
	$(PY) scripts/check_dist.py

publish-test: check ## Upload dist/ to TestPyPI with twine (needs an API token in the environment, see docs/dev/release.md)
	$(PY) -m twine upload --repository testpypi --skip-existing dist/*

publish: check ## Upload dist/ to PyPI with twine (asks first; the normal route is the tag-triggered release workflow)
	@printf "Upload the files in dist/ to PyPI? [y/N] " && read a && [ "$$a" = y ]
	$(PY) -m twine upload dist/*

tree: ## Show the package layout
	@find src/squitter -name '*.py' -not -name '__init__.py' | sed 's|src/||' | sort | awk -F/ '{print "  " $$0}'

clean: ## Remove caches and build output
	rm -rf docs/_build build dist .pytest_cache .ruff_cache .mypy_cache .coverage coverage.xml htmlcov $(OUT) src/*.egg-info
	find . -name __pycache__ -type d -prune -exec rm -rf {} +

distclean: clean ## clean, and the conda env too
	-$(CONDA) env remove -n $(ENV) -y
