# lmrelay developer Makefile.
#
#   make             — show this help
#   make install     — create .venv and install with dev extras
#   make check       — lint + typecheck + tests
#   make serve       — run the dev gateway (reload + bind to 127.0.0.1)
#
# Everything respects an externally activated venv; if .venv/bin/python
# is missing, `make install` creates one.

SHELL := /usr/bin/env bash

VENV       := .venv
PYTHON     := $(VENV)/bin/python
PIP        := $(VENV)/bin/pip
PYTEST     := $(VENV)/bin/pytest
RUFF       := $(VENV)/bin/ruff
MYPY       := $(VENV)/bin/mypy
LMRELAY  := $(VENV)/bin/lmrelay

.DEFAULT_GOAL := help
.PHONY: help install dev test lint fmt format typecheck check clean \
        serve reload ping build docker compose-up compose-down

help:  ## Show this help
	@awk 'BEGIN {FS = ":.*##"; printf "lmrelay — make targets:\n\n"} \
	      /^[a-zA-Z][a-zA-Z0-9_-]*:.*##/ \
	      { printf "  \033[1m%-14s\033[0m %s\n", $$1, $$2 }' \
	      $(MAKEFILE_LIST)

$(VENV):  ## (internal) create the dev virtualenv
	python3 -m venv $(VENV)
	$(PIP) install --upgrade pip

install: $(VENV)  ## Create .venv and install lmrelay with dev extras
	$(PIP) install -e ".[dev]"

dev: install  ## Alias for install

test:  ## Run the test suite (quiet)
	$(PYTEST) -q

lint:  ## ruff check
	$(RUFF) check lmrelay tests

fmt:  ## ruff --fix + ruff format
	$(RUFF) check --fix lmrelay tests
	$(RUFF) format lmrelay tests

format: fmt  ## Alias for fmt

typecheck:  ## mypy
	$(MYPY) lmrelay

check: lint typecheck test  ## lint + typecheck + tests

clean:  ## Remove build artifacts and tool caches
	rm -rf build dist *.egg-info \
	       .pytest_cache .mypy_cache .ruff_cache \
	       htmlcov .coverage coverage.xml
	find . -type d -name __pycache__ -prune -exec rm -rf {} +

serve:  ## Run lmrelay serve in dev mode (--reload, bound to 127.0.0.1)
	$(LMRELAY) serve --reload --host 127.0.0.1

reload:  ## Tell a running gateway to re-read its config
	$(LMRELAY) reload

ping:  ## List providers (enabled/disabled) and ping each enabled one
	$(LMRELAY) ping

build:  ## Build sdist and wheel into ./dist/
	$(PYTHON) -m pip install --upgrade build
	$(PYTHON) -m build

docker:  ## Build the local docker image (tag: lmrelay:dev)
	docker build -t lmrelay:dev .

compose-up:  ## docker compose up -d (frl_app + frl_watcher)
	docker compose up -d

compose-down:  ## docker compose down
	docker compose down
