# Website service — operational commands
# --- Configuration ------------------------------------------------------------

# Load configuration from .env (the same file Compose reads).
ENV_FILE := $(if $(wildcard .env),.env,.env.local)
include $(ENV_FILE)
export

# Variables
IMAGE_URL := $(HOST_CONTAINER)/$(HOST_OWNER)/$(IMAGE_NAME)

# Arguments
override version ?= $(IMAGE_TAG)

# Show help if no target is specified.
.DEFAULT_GOAL := help

# --- Swift package ------------------------------------------------------------

.PHONY: pkg-build
pkg-build: ## Build the Swift package
	@swift build

.PHONY: pkg-release
pkg-release: ## Release the Swift package
	@swift build -c release

.PHONY: pkg-test
pkg-test: ## Run the Swift package tests
	@swift test \
		--disable-xctest \
		--enable-code-coverage \
		--enable-swift-testing \
		--parallel

.PHONY: pkg-clean
pkg-clean: ## Remove the Swift build artifacts
	@swift package clean

.PHONY: pkg-reset
pkg-reset: ## Reset the SPM cache and build folder
	@swift package reset

.PHONY: pkg-outdated
pkg-outdated: ## List the SPM dependencies that can be updated
	@swift package update --dry-run

.PHONY: pkg-update
pkg-update: ## Update the SPM dependencies
	@swift package update

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

.PHONY: site-run
site-run: ## Run the website locally, rebuilding on source changes
	@hb watch

.PHONY: site-mount
site-mount: ## Mount the website locally
	@docker compose up \
		--build \
		--detach

.PHONY: site-unmount
site-unmount: ## Unmount and remove the local website
	@docker compose down
	@$(MAKE) img-remove

# --- Local database -----------------------------------------------------------

.PHONY: db-mount
db-mount: ## Start the local database instance
	@docker compose \
		--profile database up \
		--detach \
		--wait postgres

.PHONY: db-migrate
db-migrate: ## Run the migrations against the local database instance
	@DATABASE_DRIVER=postgres \
	DATABASE_HOST=127.0.0.1 \
	DATABASE_TLS=off \
	swift run \
		Website \
		--database-migrate

.PHONY: db-shell
db-shell: ## Open a SQL shell on the local database instance
	@docker compose \
		--profile database \
		exec \
		--env PGPASSWORD=$(or $(DATABASE_PASSWORD),ccn) \
		postgres \
		psql \
			--username=$(or $(DATABASE_USERNAME),ccn) \
			--dbname=$(or $(DATABASE_NAME),ccn)

.PHONY: db-unmount
db-unmount: ## Stop and remove the local database instance while keeping its data
	@docker compose \
		--profile database down postgres

.PHONY: db-reset
db-reset: ## Stop and remove the local database instance, then delete its data
	@docker compose \
		--profile database down postgres \
		--volumes
	@if [ -d Tests/DB ]; then \
		rm -rf Tests/DB; \
		mkdir -p Tests/DB; \
	fi

# --- Assets minification ------------------------------------------------------

.PHONY: ast-minify
ast-minify: ## Preview the minified JS/CSS assets in .build/minified
	@docker build \
		--target assets-export \
		--output .build/minified \
		--file Dockerfile \
		../..
	@echo "Minified assets written to .build/minified"

# --- Registry deployment ------------------------------------------------------

.PHONY: img-check
img-check: ## Check the production image builds
	@docker build \
		--platform $(IMAGE_PLATFORM) \
		--file Dockerfile \
		../..

.PHONY: img-release
img-release: ## Build and push the production image into the container registry
	@if [ -z "$(version)" ] || [ "$(version)" = "latest" ]; then \
		echo "Error: 'version' must be an explicit tag — e.g. make img-release version=1.2.3"; \
		exit 1; \
	fi
	@docker build \
		--platform $(IMAGE_PLATFORM) \
		--tag $(IMAGE_URL):$(version) \
		--tag $(IMAGE_URL):latest \
		--file Dockerfile \
		../..
	@echo "${HOST_PASSWORD}" \
		| docker login $(HOST_CONTAINER) \
			--username $(HOST_USER) \
			--password-stdin
	@docker push $(IMAGE_URL):$(version)
	@docker push $(IMAGE_URL):latest
	@docker logout $(HOST_CONTAINER)
	@$(MAKE) img-remove

.PHONY: img-remove
img-remove: # Removes the generated Docker images
	@images="$$(docker image ls --format '{{.Repository}}:{{.Tag}}' | grep '$(IMAGE_NAME)' | awk '{print $$1}')"; \
	if [ -n "$$images" ]; then \
		docker image rm --force $$images; \
	else \
		echo "No '$(IMAGE_NAME)' images to remove."; \
	fi

# --- Meta ---------------------------------------------------------------------

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