.PHONY: help test debug format clean_reports check_scenario_ids

help:
	@echo "Usage: make <target> [<file[:line]>] [TAGS=<tag expression>]"
	@echo ""
	@echo "Targets:"
	@echo "  test                 Generate BDD specs and run Playwright tests (requires a file path or TAGS=<tag expression>)"
	@echo "  debug                Generate BDD specs and run Playwright tests with PWDEBUG=1 (requires a file path or TAGS=<tag expression>)"
	@echo "  format               Run linting and formatting fixes"
	@echo "  clean_reports        Remove and recreate test report directories for CI"
	@echo "  check_scenario_ids   Check that all scenarios have unique ID tags"

_TEST_TARGET := $(filter-out test debug, $(MAKECMDGOALS))

test:
	@[ -n "$(_TEST_TARGET)" ] || [ -n "$(TAGS)" ] || { echo "Error: provide a test file or TAGS=<tag>"; exit 1; }
	pnpm bddgen && TAGS="$(TAGS)" pnpm playwright test $(_TEST_TARGET)

debug:
	@[ -n "$(_TEST_TARGET)" ] || [ -n "$(TAGS)" ] || { echo "Error: provide a test file or TAGS=<tag>"; exit 1; }
	pnpm bddgen && TAGS="$(TAGS)" PWDEBUG=1 pnpm playwright test $(_TEST_TARGET)

ifneq ($(filter test debug,$(MAKECMDGOALS)),)
%:
	@:
endif
format:
	pnpm lint:fix && pnpm format:fix

clean_reports:
# This line is to keep the pipeline job workspace clean, can be removed once the pipeline is stable
	rm -rf test-results
	rm -rf playwright-reports
	mkdir playwright-reports
	chmod -R 777 ./playwright-reports
	rm -rf playwright-report
	mkdir playwright-report
	chmod -R 777 ./playwright-report
	rm -rf report
	mkdir report
	chmod -R 777 ./report

check_scenario_ids:
	pnpm ts-node -r tsconfig-paths/register framework/src/tagScenariosWithIds.ts --dir ./features
