# Legyen Nyugta landing — local development and container tasks.
#
# Written for the make that ships with macOS (GNU Make 3.81), so no .ONESHELL,
# no $(file ...) and no other 3.82+ constructs.
#
# Override any variable on the command line, e.g.
#   make docker-run PORT=9090
#   make docker-build TAG=$(git rev-parse --short HEAD)

SHELL := /bin/bash
.SHELLFLAGS := -eu -o pipefail -c

IMAGE     ?= legyennyugta
TAG       ?= local
ENV_FILE  ?= .env
PORT      ?= 8080
CONTAINER ?= legyennyugta

IMAGE_REF := $(IMAGE):$(TAG)

.DEFAULT_GOAL := help

.PHONY: help install dev build preview check clean env-check \
        docker-build docker-run docker-stop docker-logs docker-shell

help: ## Show this help
	@echo "Legyen Nyugta — available targets:"
	@echo
	@grep -hE '^[a-zA-Z_-]+:.*?## ' $(MAKEFILE_LIST) \
		| sort \
		| awk 'BEGIN {FS = ":.*?## "}; {printf "  \033[36m%-16s\033[0m %s\n", $$1, $$2}'
	@echo
	@echo "  Current image ref: $(IMAGE_REF)"

# --- Guards ------------------------------------------------------------------

env-check: ## Verify the env file exists
	@test -f "$(ENV_FILE)" || { \
		echo "error: $(ENV_FILE) not found."; \
		echo "       cp .env.example $(ENV_FILE) and set IS_DEV and NEW_MESSAGE_API_URL."; \
		exit 1; \
	}

# --- Local development -------------------------------------------------------

install: ## Install dependencies from the lockfile
	npm ci

dev: ## Run the Astro dev server
	npm run dev

build: ## Build the site into dist/
	npm run build

preview: ## Serve the production build locally
	npm run preview

check: ## Type-check the project
	npx astro check

clean: ## Remove build output and generated types
	rm -rf dist .astro

# --- Container ---------------------------------------------------------------

# The image is environment independent; no env file is needed to build it.
docker-build: ## Build the image
	DOCKER_BUILDKIT=1 docker build \
		--build-arg BUILD_REVISION=$$(git rev-parse HEAD 2>/dev/null || echo unknown) \
		--build-arg BUILD_VERSION=$(TAG) \
		--build-arg BUILD_CREATED=$$(date -u +%Y-%m-%dT%H:%M:%SZ) \
		--tag $(IMAGE_REF) \
		.

# Only the runtime variables are passed, with quotes and inline comments stripped: `docker --env-file`
# would keep both (unlike Astro's dotenv), and it would also leak FIGMA_TOKEN into the container.
RUNTIME_ENV_KEYS := IS_DEV|NEW_MESSAGE_API_URL

docker-run: env-check ## Run the image in the background (override with PORT=...)
	docker rm -f $(CONTAINER) >/dev/null 2>&1 || true
	docker run -d \
		--name $(CONTAINER) \
		--publish $(PORT):8080 \
		$$(sed -nE 's/^($(RUNTIME_ENV_KEYS))=[[:space:]]*["'\'']?([^"'\''#[:space:]]*).*$$/-e \1=\2/p' $(ENV_FILE)) \
		$(IMAGE_REF)
	@echo "Legyen Nyugta running at http://localhost:$(PORT)/"

docker-stop: ## Stop and remove the running container
	docker rm -f $(CONTAINER) >/dev/null 2>&1 || true

docker-logs: ## Follow container logs
	docker logs -f $(CONTAINER)

docker-shell: ## Open a shell inside the running container
	docker exec -it $(CONTAINER) sh
