PHONY :=
.DEFAULT_GOAL := help
SHELL := /bin/bash

# Colors
RED := \033[0;31m
GREEN := \033[0;32m
YELLOW := \033[1;33m
CYAN := \033[1;36m
NC := \033[0m # No Color

# Container configuration
LOCAL_DIR := $(shell pwd)

# Auto-detect container name based on current working directory
# Extract site name from path: sites/<site>/wordpress/wp-content/plugins/wpdatatables
SITE_NAME := $(shell echo $(LOCAL_DIR) | grep -oE 'sites/[^/]+' | cut -d'/' -f2)
ifeq ($(SITE_NAME),)
SITE_NAME := wp
endif
CONTAINER_NAME := wordpress-$(SITE_NAME)

PLUGIN_DIR ?= /var/www/html/wp-content/plugins/wpdatatables
LITE_PLUGIN_DIR ?= /var/www/html/wp-content/plugins/wpdatatables-lite
DB_ROOT_PASSWORD ?= rootpass

define ensure_container
	@if ! docker ps -a --format '{{.Names}}' | grep -qx '$(CONTAINER_NAME)'; then \
		printf "$(YELLOW)Container $(CONTAINER_NAME) not found. Start stack with 'make up'.$(NC)\n"; \
		exit 1; \
	fi
endef

help: ## Show this help message
	@printf "$(CYAN)=== wpDataTables Plugin Commands ====$(NC)\n"
	@printf "\n"
	@printf "$(CYAN)Setup$(NC)\n"
	@printf "  $(YELLOW)setup$(NC)          Setup E2E environment (wp-docker site + Composer + e2e deps)\n"
	@printf "  $(YELLOW)install$(NC)        Install Composer dependencies only\n"
	@printf "\n"
	@printf "$(CYAN)Container$(NC)\n"
	@printf "  $(YELLOW)ssh$(NC)            Open shell in WordPress container (at plugin directory)\n"
	@printf "  $(YELLOW)logs$(NC)           Follow container logs\n"
	@printf "  $(YELLOW)status$(NC)         Container + runtime info\n"
	@printf "\n"
	@printf "$(CYAN)E2E Tests$(NC)\n"
	@printf "  $(YELLOW)test$(NC)           Headless (sets up env first)\n"
	@printf "  $(YELLOW)test-remote$(NC)    Headless, skips env setup (already running / remote env)\n"
	@printf "  $(YELLOW)test-open$(NC)      With visible browser\n"
	@printf "  $(YELLOW)test-ui$(NC)        Interactive Playwright UI mode\n"
	@printf "\n"
	@printf "$(CYAN)Translations$(NC)\n"
	@printf "  $(YELLOW)lang-upload$(NC)    Upload source language to Localazy\n"
	@printf "  $(YELLOW)lang-download$(NC)  Download translations from Localazy\n"

##
##Container Commands
##

PHONY += ssh
ssh: ## Open shell in WordPress container (at plugin directory)
	$(ensure_container)
	@printf "$(CYAN)Opening shell in $(CONTAINER_NAME)...$(NC)\n"
	@docker exec -it -w $(PLUGIN_DIR) $(CONTAINER_NAME) bash

PHONY += logs
logs: ## Show container logs
	$(ensure_container)
	@docker logs -f $(CONTAINER_NAME)

PHONY += status
status: ## Show plugin and container status
	@printf "$(CYAN)Site Name:$(NC) $(SITE_NAME)\n"
	@printf "$(CYAN)Container Name:$(NC) $(CONTAINER_NAME)\n"
	@printf "\n"
	@printf "$(CYAN)Container Status:$(NC)\n"
	@docker ps --filter "name=$(CONTAINER_NAME)" --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"
	@printf "\n"
	@printf "$(CYAN)Plugin Directory:$(NC)\n"
	@printf "  Local:  $(LOCAL_DIR)\n"
	@printf "  Docker: $(PLUGIN_DIR)\n"

##
##Project Setup Commands
##

PHONY += setup
setup: ## Setup wp-docker site, Composer deps and E2E deps
	@bash e2e/scripts/setup-env.sh
	@printf "$(YELLOW)Installing E2E dependencies...$(NC)\n"
	@cd e2e && npm install
	@printf "$(GREEN)✓ Setup completed$(NC)\n"

PHONY += install
install: ## Install Composer dependencies only
	$(ensure_container)
	@printf "$(YELLOW)Installing Composer dependencies...$(NC)\n"
	@docker exec -w $(PLUGIN_DIR) $(CONTAINER_NAME) composer install
	@printf "$(GREEN)✓ Dependencies installed$(NC)\n"

##
##E2E Test Commands
##

PHONY += test
test: ## Run E2E Playwright tests (sets up wp-docker site automatically)
	@printf "$(YELLOW)\nSetting up E2E environment...\n$(NC)"
	@bash e2e/scripts/setup-env.sh
	@printf "$(YELLOW)\nRunning E2E tests...\n$(NC)"
	@cd e2e && npm run test:e2e

PHONY += test-remote
test-remote: ## Run E2E tests, skipping env setup (already running / remote env)
	@cd e2e && npm run test:e2e

PHONY += test-open
test-open: ## Run E2E Playwright tests with visible browser
	@printf "$(YELLOW)\nSetting up E2E environment...\n$(NC)"
	@bash e2e/scripts/setup-env.sh
	@printf "$(YELLOW)\nRunning E2E tests with visible browser...\n$(NC)"
	@cd e2e && npm run test:e2e:headed

PHONY += test-ui
test-ui: ## Run E2E Playwright tests in UI mode
	@printf "$(YELLOW)\nSetting up E2E environment...\n$(NC)"
	@bash e2e/scripts/setup-env.sh
	@printf "$(YELLOW)\nRunning E2E tests in UI mode...\n$(NC)"
	@cd e2e && npm run test:e2e:ui

##
##Translation Commands
##

PHONY += lang-download
lang-download: ## Download translations via Localazy
	$(ensure_container)
	@printf "$(YELLOW)Downloading translations from Localazy...$(NC)\n"
	@docker exec -w $(PLUGIN_DIR) $(CONTAINER_NAME) bash jenkins/localazyDownload.sh || (printf "$(RED)Localazy download failed$(NC)\n" && exit 1)
	@printf "$(GREEN)Translations downloaded$(NC)\n"

PHONY += lang-upload
lang-upload: ## Upload source language to Localazy
	$(ensure_container)
	@printf "$(YELLOW)Uploading translations to Localazy...$(NC)\n"
	@docker exec -w $(PLUGIN_DIR) -e LITE_PATH=$(LITE_PLUGIN_DIR) $(CONTAINER_NAME) bash jenkins/localazyUpload.sh || (printf "$(RED)Localazy upload failed$(NC)\n" && exit 1)
	@printf "$(GREEN)Translations uploaded$(NC)\n"

.PHONY: $(PHONY)
