# Yeneb backend — common project commands.
# Run `make help` (or just `make`) to see this list.
#
# Assumes your virtualenv is already active (e.g. `.venv\Scripts\activate`
# on Windows, `source .venv/bin/activate` on Linux/macOS).

.DEFAULT_GOAL := help

HOST           ?= 0.0.0.0
PORT           ?= 8000
WORKERS        ?= 4
DOCKER_COMPOSE ?= docker compose

.PHONY: help install dev run prod test test-cov migrate migrate-down migration \
        lint format format-check clean docker-build docker-up docker-down docker-logs db-shell \
        seed-superadmin redis-up redis-cli export-openapi

help: ## Show this list of commands
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2}'

install: ## Install project dependencies
	pip install -r requirements.txt

dev: ## Run the API locally with auto-reload (development)
	uvicorn app.main:app --reload --host $(HOST) --port $(PORT)

run: dev ## Alias for `dev`

prod: ## Run the API with Gunicorn + Uvicorn workers (Linux/Docker only — gunicorn needs fork(), which Windows doesn't have)
	gunicorn app.main:app \
		--workers $(WORKERS) \
		--worker-class uvicorn.workers.UvicornWorker \
		--bind $(HOST):$(PORT)

test: ## Run the test suite
	pytest -q

test-cov: ## Run tests with a coverage report
	pytest --cov=app --cov-report=term-missing

migrate: ## Apply all pending Alembic migrations
	alembic upgrade head

migrate-down: ## Revert the most recent Alembic migration
	alembic downgrade -1

migration: ## Create a new Alembic migration — usage: make migration name="add orders table"
	alembic revision --autogenerate -m "$(name)"

lint: ## Check code style with Ruff
	ruff check .

format: ## Auto-format code with Ruff
	ruff format .

format-check: ## Check formatting without changing files (what CI runs)
	ruff format --check .

clean: ## Remove caches and bytecode
	find . -type d -name "__pycache__" -not -path "./.venv/*" -exec rm -rf {} +
	rm -rf .pytest_cache .ruff_cache htmlcov .coverage

docker-build: ## Build the app's Docker image
	$(DOCKER_COMPOSE) build

docker-up: ## Start the app + Postgres via Docker Compose
	$(DOCKER_COMPOSE) up -d

docker-down: ## Stop the Docker Compose stack
	$(DOCKER_COMPOSE) down

docker-logs: ## Tail the app container's logs
	$(DOCKER_COMPOSE) logs -f api

db-shell: ## Open a psql shell against the local dev database
	psql -U postgres -h localhost -p 5432 -d yeneb

seed-superadmin: ## Create the first superadmin account (run once, prompts for a PIN)
	python -m app.commands.seed_superadmin

export-openapi: ## Regenerate openapi.json from the live app schema
	python -m app.commands.export_openapi

redis-up: ## Keep WSL (and its systemd-managed Redis) running for this dev session — run once per login
	nohup wsl.exe -d Ubuntu -e sh -c "sleep infinity" > /dev/null 2>&1 &
	disown
	@echo "WSL keep-alive started — Redis (systemd-managed) will stay up as long as this session is open."

redis-cli: ## Open a redis-cli shell against the WSL Redis instance
	wsl -d Ubuntu -e redis-cli
