# Um venv local. O Python do Homebrew recusa `pip install --user` (PEP 668),
# então o venv não é preferência: é o que funciona em macOS sem mexer no sistema.
VENV := .venv
PY   := $(VENV)/bin/python
PIP  := $(VENV)/bin/pip
ROOT := $(shell pwd)

.PHONY: help install seed test typecheck build dev api web up down matrix oracle clean distclean

help:
	@echo "make install    instala as dependências de Python e de Node"
	@echo "make seed       regera o seed (só se houver um novo crawl)"
	@echo "make test       roda a suíte (domínio + API + rastreabilidade)"
	@echo "make typecheck  tsc --noEmit no frontend"
	@echo "make build      build de produção do frontend"
	@echo "make api        sobe o gateway em :8080"
	@echo "make web        sobe o frontend em :5173"
	@echo "make dev        sobe os dois"
	@echo "make up         Compose: 10 serviços + Postgres + RabbitMQ + MinIO"
	@echo "make matrix     gera a matriz de rastreabilidade (Fase 5)"
	@echo "make oracle     roda o oráculo de equivalência contra as células COMPUTED"

$(VENV):
	python3 -m venv $(VENV)
	$(PIP) install --upgrade pip

install: $(VENV)
	$(PIP) install fastapi "uvicorn[standard]" httpx pytest
	cd frontend && npm install
	@echo
	@echo "pronto. agora:  make test"

seed:
	SPREADSHEET_DATA=$${SPREADSHEET_DATA:-../spreadsheet_data} $(PY) tools/build_seed.py

test: | $(VENV)
	$(PY) -m pytest

typecheck:
	cd frontend && npx tsc --noEmit

build:
	cd frontend && npx vite build

api: | $(VENV)
	cd services/gateway && $(ROOT)/$(PY) -m uvicorn main:app --port 8080 --reload

web:
	cd frontend && npx vite

dev: | $(VENV)
	@( cd services/gateway && $(ROOT)/$(PY) -m uvicorn main:app --port 8080 & ) ; \
	 cd frontend && npx vite

up:
	docker compose up --build

down:
	docker compose down -v

matrix:
	$(PY) tools/generate_traceability_matrix.py
	$(PY) tools/render_traceability_md.py

oracle:
	$(PY) tools/equivalence_oracle.py

clean:
	find . -name __pycache__ -type d -exec rm -rf {} + 2>/dev/null || true
	rm -rf frontend/dist .pytest_cache

distclean: clean
	rm -rf $(VENV) frontend/node_modules
