.PHONY: test rebuild validate validate-schema check-routes check-route-syntax all

# Default: run full CI pipeline
all: test validate-schema validate check-routes check-route-syntax

# Run the comprehensive test suite
test:
	php test.php

# Rebuild all JSON files and routes
rebuild:
	./rebuild.sh

# Validate HTML tag balance in all content files
validate:
	php validate_tags.php

# Validate equations.json against the JSON schema
# (Always runs because target is .PHONY; script is fast < 1s)
validate-schema:
	php validate-equations.php

# Validate route file count (must be exactly 26)
check-routes:
	@echo "Checking route file count..."
	@SECTIONS=$$(ls -d */index.php 2>/dev/null | wc -l); \
	SUBPAGES=$$(ls -d */*/index.php 2>/dev/null | wc -l); \
	TOTAL=$$((SECTIONS + SUBPAGES)); \
	if [ "$$TOTAL" -ne 27 ]; then \
		echo "  \033[31m✗ Route count mismatch: $$TOTAL (expected 27)\033[0m"; \
		echo "    Section-level routes: $$SECTIONS"; \
		echo "    Sub-page routes: $$SUBPAGES"; \
		exit 1; \
	else \
		echo "  \033[32m✓ Route count: $$TOTAL (6 section + 21 sub-page)\033[0m"; \
	fi

# Validate that all route files parse as valid PHP
check-route-syntax:
	@echo "Checking route file syntax..."
	@FAILED=0; \
	for f in $$(find . -maxdepth 3 -name 'index.php' -not -path './index.php' -not -path './test.php'); do \
		php -l "$$f" > /dev/null 2>&1; \
		if [ $$? -ne 0 ]; then \
			echo "  \033[31m✗ Syntax error in $$f\033[0m"; \
			FAILED=1; \
		fi; \
	done; \
	if [ "$$FAILED" -eq 0 ]; then \
		echo "  \033[32m✓ All route files parse cleanly\033[0m"; \
	fi
