.DEFAULT_GOAL := help
SHELL := /bin/bash

UV      ?= uv
PY      := $(UV) run python

.PHONY: help
help:
	@grep -hE '^[a-z0-9-]+:.*?## ' $(MAKEFILE_LIST) | sort | awk 'BEGIN{FS=":.*?## "}{printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2}'

# ---------------------------------------------------------------- python
.PHONY: install
install: ## Create the venv and sync all dependencies
	$(UV) sync

.PHONY: lint
lint: ## ruff check + format check
	$(UV) run ruff check .
	$(UV) run ruff format --check .

.PHONY: fmt
fmt: ## Apply ruff formatting and autofixes
	$(UV) run ruff check --fix .
	$(UV) run ruff format .

.PHONY: type
type: ## mypy --strict
	$(UV) run mypy

.PHONY: test
test: ## pytest (Tier-1 only; excludes `slow`)
	$(UV) run pytest

.PHONY: ci
ci: lint type test ## The enforceable gate (mirrors .github/workflows/ci.yml)

# ---------------------------------------------------------------- regression oracle
# The characterization net: freeze the current Python output, then require the library to keep
# reproducing it. No MATLAB.
.PHONY: oracle
oracle: ## Freeze the committed Tier-1 oracle (one record per (db, fs), both modes, self-contained)
	$(PY) tools/freeze_oracle.py --tier committed --force

.PHONY: oracle-full
oracle-full: ## Freeze the full gitignored oracle (218 corpus + 305 clinical, both modes)
	$(PY) tools/freeze_oracle.py --tier full --records all --jobs $$(nproc)

.PHONY: oracle-check
oracle-check: ## Run the regression net including the @slow full-oracle tier
	$(UV) run pytest tests/test_regression.py -m 'slow or not slow'

# ---------------------------------------------------------------- corpus
.PHONY: corpus
corpus: ## Download every pinned record and verify its SHA-256
	$(PY) tools/fetch_corpus.py

.PHONY: corpus-freeze
corpus-freeze: ## Download, compute SHA-256s, and rewrite corpus.toml (frozen = true)
	$(PY) tools/fetch_corpus.py --freeze

.PHONY: annotations
annotations: ## Fetch + pin the QTDB/LUDB expert annotations (clinical gate, part 3)
	$(PY) tools/fetch_annotations.py --freeze

.PHONY: clinical
clinical: ## Score delineation vs QTDB/LUDB experts -> docs/CLINICAL.md
	$(PY) tools/clinical_validation.py

.PHONY: clean
clean: ## Remove build trees and caches (leaves data/ and goldens/ alone)
	rm -rf build .mypy_cache .ruff_cache .pytest_cache
