SHELL := /bin/sh
COMPOSE := docker compose
.DEFAULT_GOAL := help

.PHONY: help up down reset setup seed logs ps shell db-shell restart status open wp

WP_GOALS := $(filter-out wp,$(MAKECMDGOALS))

help:
	@printf '%s\n' \
		'Available commands:' \
		'  make up        - start wordpress, db and phpmyadmin' \
		'  make down      - stop services' \
		'  make setup     - bootstrap wordpress, woocommerce and the plugin' \
		'  make seed      - reseed the demo store, customer and gateway settings' \
		'  make reset     - remove local volumes and containers' \
		'  make logs      - follow compose logs' \
		'  make ps        - show service status' \
		'  make status    - alias for ps' \
		'  make restart   - restart running services' \
		'  make open      - open the storefront URL if the OS supports it' \
		'  make shell     - open a shell in the wordpress container' \
		'  make db-shell  - open a MariaDB shell' \
		'  make wp plugin list - run wp-cli commands'

up:
	$(COMPOSE) up -d

down:
	$(COMPOSE) down

setup:
	./scripts/setup-local.sh

seed:
	./scripts/setup-local.sh --seed-only

reset:
	./scripts/reset-local.sh

logs:
	$(COMPOSE) logs -f

ps status:
	$(COMPOSE) ps

restart:
	$(COMPOSE) restart

open:
	@URL=$$(grep -E '^WP_HOME=' .env 2>/dev/null | tail -n 1 | cut -d'=' -f2- || true); \
	if [ -z "$$URL" ]; then URL='http://bpayd.localhost:8080'; fi; \
	if command -v open >/dev/null 2>&1; then open "$$URL"; \
	elif command -v xdg-open >/dev/null 2>&1; then xdg-open "$$URL"; \
	else printf '%s\n' "$$URL"; fi

shell:
	$(COMPOSE) exec wordpress sh

db-shell:
	$(COMPOSE) exec db sh -lc 'exec mariadb -u"$$MYSQL_USER" -p"$$MYSQL_PASSWORD" "$$MYSQL_DATABASE"'

wp:
	$(COMPOSE) run --rm wpcli wp $(if $(WP_GOALS),$(WP_GOALS),--info)

%:
	@:
