SHELL := /bin/bash

help:
	@echo "Cibles utiles :"
	@echo "  make build     - build des images"
	@echo "  make up        - lance db + api (en arrière-plan)"
	@echo "  make down      - stop & supprime les conteneurs"
	@echo "  make logs      - logs agrégés"
	@echo "  make sh        - shell dans le conteneur API"
	@echo "  make psql      - psql dans le conteneur DB"
	@echo "  make resetdb   - reset le volume Postgres (⚠️ supprime les données)"
	@echo "  make curl      - teste l'endpoint /healthz"

build:
	docker compose build

up:
	docker compose up -d

down:
	docker compose down

logs:
	docker compose logs -f --tail=200

sh:
	docker exec -it instat_ng_api bash

psql:
	docker exec -it instat_ng_db psql -U $${POSTGRES_USER:-instat} -d $${POSTGRES_DB:-instat}

resetdb: down
	docker volume rm $$(basename $$(pwd))_db_data || true
	docker compose up -d

curl:
	@curl -s http://localhost:$${APP_PORT:-8000}/healthz | jq .

clean:
	rm -rf reports .venv .pytest_cache
	find . -type d -name "__pycache__" -exec rm -rf {} +

test:
	@set -a; \
	source .env; \
	source .venv/bin/activate; \
	BASE_URL="$${BASE_URL}" \
	DB_HOST="$${DB_HOST:-localhost}" \
	POSTGRES_PORT="$${POSTGRES_PORT}" \
	POSTGRES_USER="$${POSTGRES_USER}" \
	POSTGRES_PASSWORD="$${POSTGRES_PASSWORD}" \
	POSTGRES_DB="$${POSTGRES_DB}" \
	SCHEMA_NAME="$${SCHEMA_NAME:-instat_test}" \
	API_PREFIX="$${API_PREFIX:-}" \
	REPORT_DIR="$${REPORT_DIR:-reports}" \
	pytest tests/ -v --junitxml=reports/junit.xml --html=reports/report.html --self-contained-html
