include .env.local
export

# Docker registry info
IMAGE_TAG     ?= $(shell date +%Y%m%d%H%M%S) # default to timestamp if not set
PLATFORMS      = linux/amd64,linux/arm64

# Docker targets mapping
IMAGES = \
    base:php_base \
    local:php_local \
    dev:php_dev \
    staging:php_prod \
    prod:php_prod

# Executables (local)
DOCKER_COMPOSE = docker compose

# Docker containers
PHP_CONTAINER = php-container
PHP_EXEC = docker exec -it $(PHP_CONTAINER)

# Misc
.DEFAULT_GOAL = help
.PHONY        : help build up start down logs sh

## —— 🎵 🐳 The Symfony Docker Makefile 🐳 🎵 ——————————————————————————————————
HELP_SRC := $(filter-out .env%,$(MAKEFILE_LIST))

# Variable DUMP_DB is set on the command line, e.g., make up DUMP_DB=1

help: ## Outputs this help screen
	@grep -h -E '(^[a-zA-Z0-9%._\/-]+:.*?##.*$$)|(^(##).*)' $(HELP_SRC) | \
	awk 'BEGIN {FS = ":.*?## " } \
	/^(##)/ {printf "\033[33m%s\033[0m\n", $$0; next} \
	{printf "\033[32m%-30s\033[0m %s\n", $$1, $$2}'

## —— Docker 🐳 ————————————————————————————————————————————————————————————————
up: ## Start the docker hub in detached mode (no logs). Use 'make up DUMP_DB=1' to dump the DB.
	@echo "Login to GHCR..."
	@echo ${GHCR_TOKEN} | docker login ghcr.io -u ${GHCR_USERNAME} --password-stdin
	@if [ -n "$(DUMP_DB)" ]; then \
		$(MAKE) dump-db; \
	else \
		echo "Skipping DB dump (use make up DUMP_DB=1 to enable)"; \
	fi
	@echo "Pull and Run the project..."
	@$(DOCKER_COMPOSE) up -d
	@$(MAKE) ssl

ssl: ## Trusting the Authority
	@echo "Add SSL to Trusting the Authority..."; \
	sudo security add-trusted-cert \
		-d \
		-r trustRoot \
		-k /Library/Keychains/System.keychain \
		docker/apache/ssl/foxorders.crt

start: ## Start containers
	@echo "Starting previously stopped containers..."; \
	${DOCKER_COMPOSE} start

stop: ## Stop containers
	@echo "Stopping all running containers..."; \
	${DOCKER_COMPOSE} stop

restart: ## Restart containers
	@echo "Restarting containers..."; \
	${DOCKER_COMPOSE} stop && ${DOCKER_COMPOSE} start

down: ## Stop and remove containers
	@echo "Stopping and removing containers..."; \
	${DOCKER_COMPOSE} down

clean: ## Stop and remove containers, images and volumes, Also clean unsed Docker resources
	@echo "Stopping containers and removing volumes..."
	@${DOCKER_COMPOSE} down -v;
	@echo "Cleaning up unused Docker resources..."
	@docker system prune -a -f

sh: ## Run the PHP container shell
	@echo "Running PHP shell..."; \
	${PHP_EXEC} sh

bash: ## Run the PHP container bash
	@echo "Running PHP shell..."; \
	${PHP_EXEC} bash

dump-db: ## Dump copy of Staging Database
	@echo "Dump DB from staging..."; \
	mysqldump \
		--ssl-mode=REQUIRED \
		-h ${AZURE_STAGING_DB_HOST} \
		-u ${AZURE_STAGING_DB_USERNAME} \
		-p${AZURE_STAGING_DB_PASSWORD} \
		${AZURE_STAGING_DB_NAME} > ./docker/db/init/00-azure-db-dump.sql 2>/dev/null

sync-db: ## Sync Azure DB locally
	@echo "Sync local..."; \
	bash ./docker/db/sync-db.sh

build-%: ## Build Image
	@echo "Login to GHCR..." 
	@echo ${GHCR_TOKEN} | docker login ghcr.io -u ${GHCR_USERNAME} --password-stdin;
	@name=$*; \
	target=$$(echo "$(IMAGES)" | tr ' ' '\n' | grep "^$$name:" | cut -d: -f2); \
	if [ -z "$$target" ]; then \
		echo "Unknown image '$$name'"; exit 1; \
	fi; \
	echo "Building $$name ($$target) for $(PLATFORMS) ..."; \
	docker buildx build \
		--platform $(PLATFORMS) \
		-t ghcr.io/popina/foxorders-$$name:latest \
		-t ghcr.io/popina/foxorders-$$name:$(IMAGE_TAG) \
		-f Dockerfile \
		--target $$target \
		.

push-%: ## Push image
	@echo "Login to GHCR..."
	@echo ${GHCR_TOKEN} | docker login ghcr.io -u ${GHCR_USERNAME} --password-stdin;
	@name=$*; \
	docker push -a ghcr.io/popina/foxorders-$$name

# Avoid error: "Makefile: *** No rule to make target ..."
%:
	@: