SHELL := /bin/sh

CARGO ?= cargo
DOCKER ?= docker
IMAGE ?= pokedex-api
TAG ?= local
BASE_URL ?= http://localhost:5000
POKEMON ?= mewtwo
PLATFORMS ?= linux/amd64,linux/arm64
GIT_SHA ?= $(shell git rev-parse HEAD 2>/dev/null || printf unknown)

.PHONY: help run docker-build docker-run cli-health cli-pokemon cli-translated health pokemon translated metrics fmt clippy test check contract-test docker-buildx

help:
	@printf 'Available targets:\n'
	@printf '  make run              Run the API locally with cargo\n'
	@printf '  make docker-build     Build the Docker image\n'
	@printf '  make docker-run       Run the Docker image on port 5000\n'
	@printf '  make cli-health       Call /health through the CLI helper\n'
	@printf '  make cli-pokemon      Call /pokemon/$${POKEMON} through the CLI helper\n'
	@printf '  make cli-translated   Call /pokemon/translated/$${POKEMON} through the CLI helper\n'
	@printf '  make health           curl /health\n'
	@printf '  make pokemon          curl /pokemon/$${POKEMON}\n'
	@printf '  make translated       curl /pokemon/translated/$${POKEMON}\n'
	@printf '  make metrics          curl /metrics\n'
	@printf '  make check            Run fmt, clippy, and tests\n'
	@printf '  make contract-test    Run ignored external contract tests\n'
	@printf '  make docker-buildx    Build multi-arch Docker image\n'

run:
	$(CARGO) run --bin pokedex-api

docker-build:
	$(DOCKER) build -t $(IMAGE) .

docker-run:
	$(DOCKER) run --rm -p 5000:5000 $(IMAGE)

cli-health:
	$(CARGO) run --bin pokedex-cli -- health

cli-pokemon:
	$(CARGO) run --bin pokedex-cli -- --base-url $(BASE_URL) pokemon $(POKEMON)

cli-translated:
	$(CARGO) run --bin pokedex-cli -- --base-url $(BASE_URL) translated $(POKEMON)

health:
	curl -fsS $(BASE_URL)/health

pokemon:
	curl -fsS $(BASE_URL)/pokemon/$(POKEMON)

translated:
	curl -fsS $(BASE_URL)/pokemon/translated/$(POKEMON)

metrics:
	curl -fsS $(BASE_URL)/metrics

fmt:
	$(CARGO) fmt --all --check

clippy:
	$(CARGO) clippy --locked --all-targets -- -D warnings

test:
	$(CARGO) test --locked

check: fmt clippy test

contract-test:
	$(CARGO) test --locked --test external_contract -- --ignored --test-threads=1

docker-buildx:
	$(DOCKER) buildx build --platform $(PLATFORMS) --build-arg GIT_SHA=$(GIT_SHA) -t $(IMAGE):$(TAG) .
