.PHONY: help run reindex build tidy sqlc api-contract mail-preview gen-contracts gen-cities gen-metros gen-skill-descriptions cv-previews up down logs migrate psql cover cover-html cover-integration

# Prefer Docker; fall back to Podman when `docker` is missing. Override with
# `make DOCKER=podman up` (or COMPOSE=…) when both are installed.
DOCKER ?= $(shell command -v docker >/dev/null 2>&1 && echo docker || echo podman)
COMPOSE ?= $(DOCKER) compose

# Coverage profiles land under coverage/ (gitignored). Informational only — no % gate.
COVERAGE_DIR ?= coverage

COVER_UNIT ?= $(COVERAGE_DIR)/unit.out
COVER_INTEGRATION ?= $(COVERAGE_DIR)/integration.out

# Where `make mail-preview` writes. Committed, because Storybook frames these files
# and a test asserts they still match what the templates render.
MAIL_PREVIEW_DIR ?= design-system/static/email-previews

SQLC_VERSION ?= 1.31.1

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

run: ## Run the server locally (requires a running Postgres)
	go run ./cmd/server

reindex: ## Reindex jobs into Meilisearch (requires running Postgres + Meilisearch)
	go run ./cmd/reindex

build: ## Build the binary
	sh scripts/heavy.sh go build -o bin/hire ./cmd/server

tidy: ## Tidy up dependencies
	go mod tidy

# A local sqlc is used only when it is exactly SQLC_VERSION, so this stays the one pin. The
# coding agents' runner has sqlc and no Docker.
sqlc: ## Generate code from SQL (a local sqlc of exactly SQLC_VERSION, else via Docker/Podman)
	@if [ "$$(sqlc version 2>/dev/null)" = "v$(SQLC_VERSION)" ]; then sqlc generate; \
	else $(DOCKER) run --rm -v "$(PWD):/src" -w /src docker.io/sqlc/sqlc:$(SQLC_VERSION) generate; fi

mail-preview: ## Render every outgoing email and open the contact sheet in a browser
	@go run ./cmd/mail-preview
	@(open $(MAIL_PREVIEW_DIR)/index.html 2>/dev/null || xdg-open $(MAIL_PREVIEW_DIR)/index.html 2>/dev/null) || \
		echo "open $(MAIL_PREVIEW_DIR)/index.html"

api-contract: ## Compile typespec/ to openapi/api.openapi.json, then generate internal/api/apigen and the web's types from it
	@# Compiled beside the document and renamed over it once finished: the api-docs-drift
	@# hook reads it in parallel with this one and must never see a half-processed file.
	@out=$$(mktemp -d openapi/.compile.XXXXXX) && \
		pnpm exec tsp compile typespec --option "@typespec/openapi3.emitter-output-dir=$(CURDIR)/$$out" && \
		node scripts/openapi-time-format.mjs $$out/api.openapi.json && \
		mv $$out/api.openapi.json openapi/api.openapi.json; status=$$?; rm -rf $$out; exit $$status
	pnpm exec openapi-typescript openapi/api.openapi.json -o web/src/lib/generated/api.ts --silent
	@# Generated into a scratch dir and synced by content, so an unchanged contract touches no
	@# file: the pre-commit hooks run in parallel, and go vet/govulncheck compiling apigen
	@# while ogen --clean rewrote it in place saw a half-written package.
	@# ogen reads the contract without its security requirements (scripts/openapi-ogen-input.mjs).
	@tmp=$$(mktemp -d) && in=$$(mktemp) && \
		node scripts/openapi-ogen-input.mjs openapi/api.openapi.json $$in && \
		go tool ogen --config ogen.yml --target $$tmp --package apigen --clean $$in && \
		rsync -rc --delete $$tmp/ internal/api/apigen/; status=$$?; rm -rf $$tmp; rm -f $$in; exit $$status

gen-contracts: ## Regenerate web/src/lib/generated/contracts.ts from Go contracts
	cd tools && go run ./gen-contracts

gen-cities: ## Regenerate internal/dict/location/cities1000.tsv from the GeoNames dump
	go run ./cmd/gen-cities

gen-metros: ## Regenerate internal/dict/location/metros.tsv from GeoNames + GHS-FUA
	cd tools && go run ./gen-metros

# Prints to stdout on purpose — the shipped text is reviewed, so a human pipes this into
# internal/dict/skilltag/descriptions.tsv and edits every line before committing. LIMIT
# sizes the wave; 0 drafts every skill that has none.
gen-skill-descriptions: ## Draft the next wave of skill glossary entries (prints; edit before committing)
	go run ./cmd/gen-skill-descriptions -limit $(or $(LIMIT),100)

cv-previews: ## Regenerate web/static/cv-previews/*.svg from the CV templates (needs typst)
	go run ./cmd/cv-previews

up: ## Start everything (app + postgres) via Docker or Podman Compose
	$(COMPOSE) up --build -d

down: ## Stop and remove containers
	$(COMPOSE) down

logs: ## Tail application logs
	$(COMPOSE) logs -f app

migrate: ## Apply pending migrations to DATABASE_URL (use mise for this checkout's DB)
	@sh scripts/check-migration-history.sh
	go run ./cmd/migrate

psql: ## Open psql at DATABASE_URL (use mise for this checkout's DB)
	psql "$${DATABASE_URL:?DATABASE_URL is required; run make through mise exec}"

cover: ## Unit tests with coverage profile → coverage/unit.out
	@mkdir -p $(COVERAGE_DIR)
	sh scripts/heavy.sh go test ./... -coverprofile=$(COVER_UNIT) -covermode=atomic
	@go tool cover -func=$(COVER_UNIT) | tail -1

cover-html: ## HTML report from coverage/unit.out → coverage/unit.html
	@test -f $(COVER_UNIT) || $(MAKE) cover
	go tool cover -html=$(COVER_UNIT) -o $(COVERAGE_DIR)/unit.html
	@echo "wrote $(COVERAGE_DIR)/unit.html"

cover-integration: ## Integration-tagged tests with coverage (needs Docker for testcontainers)
	@mkdir -p $(COVERAGE_DIR)
	sh scripts/heavy.sh go test -tags=integration ./... -coverprofile=$(COVER_INTEGRATION) -covermode=atomic
	@go tool cover -func=$(COVER_INTEGRATION) | tail -1
	@echo "profile: $(COVER_INTEGRATION)"

# CI uses this same entry point; go run builds with the module toolchain.
LINT_VERSION ?= 2.15.0
.PHONY: lint
lint: ## Run the pinned linter against origin/main
	GOTOOLCHAIN=$$(awk '/^toolchain / {print $$2}' go.mod) sh scripts/heavy.sh go run github.com/golangci/golangci-lint/v2/cmd/golangci-lint@v$(LINT_VERSION) run --new-from-merge-base=origin/main --allow-parallel-runners ./...
