# 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 segment 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 the real dataset from the UCI repository and convert it to CSV
	$(PY) -m segments.cli download --config $(CONFIG)

synthetic:     ## Create a synthetic transactions file with the same schema (no download needed)
	$(PY) scripts/make_synthetic.py --customers 6000 --out data/raw/online_retail.csv

validate:      ## Check the raw transactions against the schema
	$(PY) -m segments.cli validate --config $(CONFIG)

prepare:       ## Clean transactions, build customer features (before the cut-off) and outcomes (after it)
	$(PY) -m segments.cli prepare --config $(CONFIG)

eda:           ## Exploratory analysis of the feature window
	$(PY) -m segments.cli eda --config $(CONFIG)

segment:       ## Choose k, fit the segmentation, check stability, name the segments
	$(PY) -m segments.cli segment --config $(CONFIG)

evaluate:      ## Do segments predict the outcome window? Compare with the rule-based RFM baseline; segment card
	$(PY) -m segments.cli evaluate --config $(CONFIG)

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

serve:         ## Run the segment-assignment API locally
	$(PY) -m flask --app "segments.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 segments-api .

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