# Raccourcis de développement.
# La production passe par docker compose — voir docs/02-deploiement.md.

.DEFAULT_GOAL := aide
SHELL := /bin/bash

.PHONY: aide
aide:  ## Affiche cette aide
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) \
	 | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-18s\033[0m %s\n", $$1, $$2}'

# ------------------------------------------------------------------ backend

.PHONY: install
install:  ## Installe le backend et l'interface en mode développement
	cd backend && pip install -e ".[dev]"
	cd frontend && npm ci --no-audit --no-fund

.PHONY: test
test:  ## Lance les tests du backend
	cd backend && LIMIER_ENVIRONMENT=dev \
	  LIMIER_SESSION_SECRET=secret-de-dev-suffisamment-long-pour-passer \
	  LIMIER_IDENTIFIER_HASH_PEPPER=poivre-de-dev \
	  pytest -q

.PHONY: lint
lint:  ## Lint et vérification des types
	cd backend && ruff check src tests && ruff format --check src tests
	cd frontend && npm run lint

.PHONY: format
format:  ## Reformate le code
	cd backend && ruff format src tests && ruff check --fix src tests

.PHONY: api
api:  ## Démarre l'API en local (port 8000)
	cd backend && LIMIER_ENVIRONMENT=dev \
	  LIMIER_SESSION_SECRET=secret-de-dev-suffisamment-long-pour-passer \
	  LIMIER_IDENTIFIER_HASH_PEPPER=poivre-de-dev \
	  PYTHONPATH=src python -m limier.entrypoints.api

.PHONY: worker
worker:  ## Démarre le worker en local
	cd backend && LIMIER_ENVIRONMENT=dev \
	  LIMIER_SESSION_SECRET=secret-de-dev-suffisamment-long-pour-passer \
	  LIMIER_IDENTIFIER_HASH_PEPPER=poivre-de-dev \
	  PYTHONPATH=src python -m limier.entrypoints.worker

.PHONY: web
web:  ## Démarre l'interface en local (port 5173, relaie /api vers 8000)
	cd frontend && npm run dev

# --------------------------------------------------------------- migrations

.PHONY: migration
migration:  ## Génère une migration — make migration M="ajout de X"
	cd backend && alembic revision --autogenerate -m "$(M)"

.PHONY: migrer
migrer:  ## Applique les migrations
	cd backend && alembic upgrade head

# ------------------------------------------------------------------ images

.PHONY: images
images:  ## Construit les images localement
	docker build -t limier-backend:dev ./backend
	docker build -t limier-frontend:dev ./frontend
	docker build -t limier-holehe:dev ./services/holehe

.PHONY: verifier
verifier: lint test  ## Tout ce que la CI vérifie
	cd frontend && npm run build
