.PHONY: dev install clean test tunnel deploy logs secrets help

help:
	@echo "Targets:"
	@echo "  make dev      — run locally on :8000 with reload"
	@echo "  make test     — run pytest"
	@echo "  make tunnel   — expose localhost:8000 via cloudflared quick tunnel"
	@echo "  make deploy   — fly deploy"
	@echo "  make secrets  — push your .env to fly secrets"
	@echo "  make logs     — tail fly logs"
	@echo "  make clean    — remove local DB + secrets file"

install:
	uv sync

dev: install
	uv run uvicorn main:app --reload --port 8000

test: install
	uv run pytest

clean:
	rm -f *.db .parbaked.json

# --- Local tunneling --------------------------------------------------------
# Uses cloudflared quick tunnels — no Cloudflare account needed. The
# trycloudflare.com URL it prints is ephemeral (dies when you ctrl-C).
#
# Important: once you have the URL, set PARBAKED_APP_URL=<that URL> so magic
# links resolve correctly. Easiest way: run `make dev` in one terminal and
# `PARBAKED_APP_URL=<url> make dev` once you know the URL.

tunnel:
	@command -v cloudflared >/dev/null || { \
		echo "Install cloudflared first:"; \
		echo "  macOS:  brew install cloudflared"; \
		echo "  Linux:  see https://github.com/cloudflare/cloudflared"; \
		exit 1; }
	@echo "Tunnel starting — copy the trycloudflare.com URL and set PARBAKED_APP_URL to it"
	cloudflared tunnel --url http://localhost:8000

# --- Deploy (fly.io) --------------------------------------------------------

deploy:
	@command -v fly >/dev/null || { \
		echo "Install flyctl first: https://fly.io/docs/flyctl/install/"; \
		exit 1; }
	fly deploy

logs:
	fly logs

# One-time: push your local .env to fly as secrets. Re-run any time you
# change secrets.
secrets:
	@test -f .env || { \
		echo ".env not found — copy .env.example to .env and fill in your secrets"; \
		exit 1; }
	fly secrets import < .env
