# Ping River Monitor. The service is Rust (rust/, ping-collector); Python is
# the model training/evaluation stack and the archive backfills (src/).
# Database tests (both sides) need TEST_DATABASE_URL, e.g.
#   docker run -d --name ping-pg -e POSTGRES_PASSWORD=ping -e POSTGRES_DB=ping -p 55432:5432 postgres:16
#   export TEST_DATABASE_URL=postgres://postgres:ping@localhost:55432/ping

PY_SRC := src/ scripts/ tests/ rust/forecast/testdata rust/collector/testdata
CARGO := cargo
MANIFEST := --manifest-path rust/Cargo.toml

.PHONY: help install format lint test test-py test-rust build check serve train evaluate export clean

help:
	@echo "make install    uv sync (Python 3.11) + pre-commit hooks"
	@echo "make format     black + isort (Python), cargo fmt (Rust)"
	@echo "make lint       the CI checks without tests (format, flake8, clippy)"
	@echo "make test       pytest + cargo test"
	@echo "make build      release build of ping-collector (rust/target/release)"
	@echo "make check      lint + test + the Rust 1.88 minimum-version check"
	@echo "make serve      website/API from the local database on 127.0.0.1:8001"
	@echo "make train      retrain every station's model into models/"
	@echo "make evaluate   rolling-origin harness on P.1 (see scripts/evaluate_variants.py)"
	@echo "make export     export trained models + flood_config.json for the Rust forecaster"

install:
	uv sync --python 3.11
	uv run pre-commit install

format:
	uv run black $(PY_SRC)
	uv run isort src/ scripts/ tests/
	$(CARGO) fmt --all $(MANIFEST)

lint:
	uv run black --check $(PY_SRC)
	uv run isort --check-only src/ scripts/ tests/
	uv run flake8 src/ scripts/ tests/ --count --select=E9,F63,F7,F82 --show-source --statistics
	$(CARGO) fmt --all --check $(MANIFEST)
	$(CARGO) clippy --all-targets --locked $(MANIFEST) -- -D warnings

test: test-py test-rust

test-py:
	uv run pytest -q

test-rust:
	$(CARGO) test --locked $(MANIFEST)

build:
	$(CARGO) build --release --locked $(MANIFEST)

check: lint test
	CARGO_TARGET_DIR=rust/target/msrv $(CARGO) +1.88 check --all-targets --locked $(MANIFEST)

serve:
	rust/target/release/ping-collector serve --listen 127.0.0.1:8001 --no-analytics

train:
	uv run python scripts/train_flood_model.py --stations all

evaluate:
	uv run python scripts/evaluate_variants.py --stations P.1

export:
	uv run python -m src.ml.export
	uv run python -m src.ml.export --config

clean:
	find . -type d -name "__pycache__" -prune -exec rm -rf {} +
	rm -rf .pytest_cache/ htmlcov/ dist/ build/ *.egg-info/
