# One command per pipeline step. Run `make help` to list them.
PY ?= python
CONFIG ?= configs/config.yaml

.PHONY: help install data synthetic validate prepare eda train evaluate all serve test lint typecheck check clean docker

help:          ## List the available commands
	@grep -E '^[a-z-]+:.*## ' Makefile | sed 's/:.*## /\t/'

install:       ## Install pinned dependencies and the package in editable mode
	$(PY) -m pip install -r requirements-dev.txt && $(PY) -m pip install -e . --no-deps && pre-commit install

data:          ## Download AEMO demand (monthly files) and Open-Meteo temperature, with provenance
	$(PY) -m demand.cli download --config $(CONFIG)

synthetic:     ## Create synthetic AEMO-format files instead (no download needed)
	$(PY) scripts/make_synthetic.py --start 2019-01 --end 2025-12 --raw-dir data/raw

validate:      ## Check the raw files against the schema and physical limits
	$(PY) -m demand.cli validate --config $(CONFIG)

prepare:       ## Hourly series, repairs, temperature, holidays, forecast-time-safe features
	$(PY) -m demand.cli prepare --config $(CONFIG)

eda:           ## Exploratory analysis of the period before the backtest
	$(PY) -m demand.cli eda --config $(CONFIG)

train:         ## Tune, choose, walk-forward backtest, fit the final model
	$(PY) -m demand.cli train --config $(CONFIG)

evaluate:      ## Backtest metrics with intervals, error patterns, the forecast card
	$(PY) -m demand.cli evaluate --config $(CONFIG)

all: validate prepare eda train evaluate   ## Run the whole pipeline

serve:         ## Run the forecast API locally
	$(PY) -m flask --app "demand.serving.app:create_app()" run --debug --port 8000

test:          ## Run the test suite
	$(PY) -m pytest

lint:          ## Lint and check formatting
	ruff check . && ruff format --check .

typecheck:     ## Static type checks
	mypy

check: lint typecheck test   ## Everything CI runs

docker:        ## Build the serving image
	docker build -t demand-api .

clean:         ## Remove generated data, models and reports
	rm -rf data/interim/* data/processed/* models/* reports/* runs/*
