# newzy — development shortcuts (docker compose under the hood).
# Run `make help` (or just `make`) for the list of targets.

COMPOSE ?= docker compose

.DEFAULT_GOAL := help

.PHONY: help dev stop restart build logs status backend-start frontend-start \
        test test-frontend test-backend e2e shell-backend shell-frontend clean

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

dev: ## Build (if needed) and start both services in the background
	$(COMPOSE) up -d --build

stop: ## Stop and remove containers (local data in ./data is kept)
	$(COMPOSE) down

restart: ## Recreate and start both services
	$(COMPOSE) down
	$(COMPOSE) up -d --build

build: ## Rebuild all images from scratch (no cache)
	$(COMPOSE) build --no-cache

logs: ## Follow the logs of both services
	$(COMPOSE) logs -f backend frontend

status: ## Show the status of both services
	$(COMPOSE) ps

backend-start: ## Build (if needed) and start the backend only
	$(COMPOSE) up -d backend

frontend-start: ## Build (if needed) and start the frontend only
	$(COMPOSE) up -d frontend

test: test-frontend test-backend ## Run all unit tests (frontend + backend)

test-frontend: ## Run frontend unit tests (requires Node.js 18+)
	cd frontend && npm run test

test-backend: ## Run backend tests (uses backend/.venv if present, else system pytest)
	cd backend && if [ -x .venv/bin/pytest ]; then .venv/bin/pytest; else pytest; fi

e2e: ## Run Playwright E2E tests (frontend/playwright; API calls are mocked)
	cd frontend && npm run e2e

shell-backend: ## Open a shell inside the running backend container
	$(COMPOSE) exec backend bash

shell-frontend: ## Open a shell inside the running frontend container
	$(COMPOSE) exec frontend sh

clean: ## Stop everything; remove containers, volumes and local data
	$(COMPOSE) down -v --remove-orphans
	rm -rf data/
	git checkout -- data/ 2>/dev/null || true
	docker image prune -f
