PY := python3
TOOLS := tools

# Parameters for the query and write targets (see `make help` for examples).
ID ?=
SEV ?=
SYSTEM ?=
OUTCOME ?=
NOTE ?=

.DEFAULT_GOAL := help
.PHONY: help list all show waits escalations policies check due fixes coverage \
        register checked test

##@ Inspect the register (read-only)

list: ## Live findings, most severe first. Filters: make list SEV=high  |  make list SYSTEM=qonto-assistant
	@$(PY) $(TOOLS)/risk.py list $(if $(SEV),--severity $(SEV)) $(if $(SYSTEM),--system $(SYSTEM))

all: ## Every finding, including fixed and withdrawn
	@$(PY) $(TOOLS)/risk.py list --all $(if $(SEV),--severity $(SEV)) $(if $(SYSTEM),--system $(SYSTEM))

show: ## One finding or regulatory record: make show ID=RISK-F-0012
	@test -n "$(ID)" || { echo 'usage: make show ID=RISK-F-0012'; exit 2; }
	@$(PY) $(TOOLS)/risk.py show $(ID)

waits: ## What the register waits on, or owes, ordered by default date
	@$(PY) $(TOOLS)/risk.py waits

escalations: ## Escalations and their delivery state
	@$(PY) $(TOOLS)/risk.py escalations

policies: ## Regulatory records and full legal policies with next review
	@$(PY) $(TOOLS)/risk.py policies

##@ Run the register

check: ## Full sweep: verify the index, report due and quiet work, read the inbox
	@$(PY) $(TOOLS)/check_all.py

due: ## Just the work list: what needs a check right now
	@$(PY) $(TOOLS)/register_check.py
	@$(PY) $(TOOLS)/inbox_check.py

fixes: ## State of every fix the register tracks, read from owner workplans
	@$(PY) $(TOOLS)/fix_tracker.py

coverage: ## Which estate repos the register has never heard from
	@$(PY) $(TOOLS)/coverage.py

##@ Record and maintain

checked: ## Record a check: make checked ID=RISK-F-0012 OUTCOME=clean|moved NOTE="what was seen"
	@if [ -n "$(ARGS)" ]; then $(PY) $(TOOLS)/record_check.py $(ARGS); \
	 else test -n "$(ID)" -a -n "$(OUTCOME)" || { echo 'usage: make checked ID=RISK-F-0012 OUTCOME=clean NOTE="..."'; exit 2; }; \
	 $(PY) $(TOOLS)/record_check.py $(ID) $(OUTCOME) $(if $(NOTE),"$(NOTE)"); fi
	@$(PY) $(TOOLS)/register_index.py

register: ## Rebuild REGISTER.md from findings/
	@$(PY) $(TOOLS)/register_index.py

test: ## Run the tool test suite
	@PYTHONDONTWRITEBYTECODE=1 $(PY) -m unittest discover -s tests -v

##@ Help

help: ## List targets with examples (the default)
	@awk 'BEGIN {FS = ":.*## "} \
	  /^##@/ {printf "\n\033[1m%s\033[0m\n", substr($$0, 5); next} \
	  /^[a-z][a-zA-Z_-]*:.*## / {printf "  \033[36m%-12s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST)
	@echo
	@echo "Defer a check (operator decision): make checked ARGS='RISK-F-0012 defer 2026-10-15 \"reason\"'"
