# WordPress Plugin (PHP + Frontend)
REPO_ROOT               := $(shell git rev-parse --show-toplevel)
WP_DIR                  ?= /var/www/html
PLUGIN_NAME             = wpengine-smart-search
PLUGIN_DIR              = $(WP_DIR)/wp-content/plugins/$(PLUGIN_NAME)
WP_DOCKER_RUN           = docker run --rm -v $(REPO_ROOT)/wordpress/$(PLUGIN_NAME):/workspace
COMPOSER_IMAGE          = -v ~/.composer/cache:/tmp/cache -w /workspace composer
PLUGIN_VERSION          ?= $(shell cat $(REPO_ROOT)/VERSION)
DOCKER_COMPOSE          = docker compose --project-directory $(REPO_ROOT)

# Composer
composer-install: ## Installs packages listed in composer.json
	$(WP_DOCKER_RUN) $(COMPOSER_IMAGE) install

composer-update: ## Run composer update
	$(WP_DOCKER_RUN) $(COMPOSER_IMAGE) update

dump-autoload: ## Regenerate Composer autoloader
	$(WP_DOCKER_RUN) $(COMPOSER_IMAGE) dump-autoload

composer-install-prod: ## Install Composer dependencies for production (no dev)
	$(WP_DOCKER_RUN) $(COMPOSER_IMAGE) install --no-dev --optimize-autoloader

# Build
.PHONY: latest-plugin-zip
latest-plugin-zip: composer-install
	@$(REPO_ROOT)/wordpress/bin/create-plugin-zip;

build-blocks:
	cd $(REPO_ROOT)/wordpress/wpengine-smart-search && yarn && yarn build:blocks

start-blocks:
	cd $(REPO_ROOT)/wordpress/wpengine-smart-search && yarn && yarn start:blocks

# Lint
lint-plugin:
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpcs --extensions=php
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpcs --standard=.strict.phpcs.xml.dist --extensions=php app

lint-fix-plugin:
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpcbf --extensions=php
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpcbf --standard=.strict.phpcs.xml.dist --extensions=php app

lint-js:
	@$(DOCKER_COMPOSE) run --rm -w /app smart-search-plugin-blocks-dev yarn lint

lint-ts:
	@$(DOCKER_COMPOSE) run --rm -w /app smart-search-plugin-blocks-dev bash -c 'yarn tsc --noEmit'

lint-style:
	@$(DOCKER_COMPOSE) run --rm -w /app smart-search-plugin-blocks-dev yarn style

MSG ?= $(REPO_ROOT)/.git/COMMIT_EDITMSG
commitlint:
	@cat $(MSG) | $(DOCKER_COMPOSE) run --rm -T -w /app smart-search-plugin-blocks-dev yarn commitlint

lint-all: lint-plugin lint-ts lint-js lint-style

# Test
test-blocks:
	cd $(REPO_ROOT)/wordpress/wpengine-smart-search && yarn && yarn test:blocks

test-plugin:
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpunit

test-plugin-e2e:
	cd $(REPO_ROOT)/wordpress/wpengine-smart-search && yarn && yarn playwright test --workers=1

test-plugin-with-coverage:
	@$(DOCKER_COMPOSE) exec -e XDEBUG_MODE=coverage -w $(PLUGIN_DIR) wordpress \
	 ./vendor/bin/phpunit --coverage-html coverage/coverage.html --coverage-clover coverage/coverage.xml

test-plugin-watch:
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpunit-watcher watch

# To run a specific test:  make test-plugin-watch-file filter=test_query_with_different_search_after_params
test-plugin-watch-file:
	@$(DOCKER_COMPOSE) exec -w $(PLUGIN_DIR) wordpress ./vendor/bin/phpunit-watcher watch --filter $(filter)

# Debug
tail-wordpress-debug-log:
	@$(DOCKER_COMPOSE) run -T --rm wordpress tail -f /var/www/html/wp-content/debug.log

# Deploy
gbuild-deploy-plugin-staging:
	gcloud builds submit \
		--config="$(REPO_ROOT)/wordpress/cloud-build/build.yaml" \
		--project="wp-engine-headless-build"

gbuild-pull-requests-wordpress:
	gcloud builds submit \
		--config="$(REPO_ROOT)/wordpress/cloud-build/pull-requests.yaml" \
		--project="wp-engine-headless-build"

pull-plugin-artifacts:
	mkdir -p artifacts
	gcloud storage cp -r \
		"gs://wpe-content-engine-plugin/info.json" \
		"gs://wpe-content-engine-plugin/atlas-search.$(PLUGIN_VERSION).zip" \
		"gs://wpe-content-engine-plugin/atlas-search.zip" \
		./artifacts
