# php-qml framework skeleton — Make targets.

.DEFAULT_GOAL := help

SYMFONY_DIR  := symfony
QML_SRC_DIR  := qml
BUILD_DIR    := build/qml
QT_BIN       := $(BUILD_DIR)/skeleton
# Path to framework/php (path-repo source) and packaging/linux (build-appimage.sh).
# Both are framework-tree relative; bin/php-qml-init rewrites them at scaffold time
# to either an absolute framework path (default) or a vendored copy under .bridge/.
BUNDLE_SRC   := ../../php
PACKAGING    := ../../packaging/linux

.PHONY: help
help: ## Show available targets
	@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf "  %-12s %s\n", $$1, $$2}' $(MAKEFILE_LIST)

.PHONY: install
install: ## Install Composer dependencies for the Symfony app
	cd $(SYMFONY_DIR) && composer install

.PHONY: build
build: ## Build the Qt host
	cmake -S $(QML_SRC_DIR) -B $(BUILD_DIR) -DCMAKE_BUILD_TYPE=Debug
	cmake --build $(BUILD_DIR) --parallel

.PHONY: dev
dev: build ## Run skeleton in dev mode (FrankenPHP --watch + Qt host)
	./scripts/dev.sh

.PHONY: doctor
doctor: ## Run bridge:doctor inside the Symfony app
	cd $(SYMFONY_DIR) && bin/console bridge:doctor

.PHONY: doctor-connect
doctor-connect: ## Run bridge:doctor with backend connectivity probe
	cd $(SYMFONY_DIR) && bin/console bridge:doctor --connect

.PHONY: clean
clean: ## Remove build artefacts
	rm -rf $(BUILD_DIR) build/staging-symfony

.PHONY: staging-symfony
staging-symfony: ## Stage a --no-dev composer copy of symfony for AppImage assembly
	# See examples/todo/Makefile for the rationale; same logic, mirrored here
	# so scaffolded apps inherit a working AppImage flow without copy-paste.
	# BUNDLE_SRC may be absolute (after `php-qml-init` rewrites it for a
	# scaffolded app) or relative-to-symfony (framework default `../../php`);
	# the case-statement handles both.
	rm -rf build/staging-symfony
	rsync -a --delete \
		--exclude='vendor/' \
		--exclude='var/cache/' --exclude='var/log/' \
		$(SYMFONY_DIR)/ build/staging-symfony/
	set -e; case "$(BUNDLE_SRC)" in \
		/*) BUNDLE_ABS="$(BUNDLE_SRC)" ;; \
		*)  BUNDLE_ABS="$$(cd $(SYMFONY_DIR)/$(BUNDLE_SRC) && pwd)" ;; \
	esac; \
	    sed -i "s|\"$(BUNDLE_SRC)\"|\"$$BUNDLE_ABS\"|" build/staging-symfony/composer.json
	sed -i 's|"symlink": true|"symlink": false|' build/staging-symfony/composer.json
	rm -f build/staging-symfony/composer.lock
	cd build/staging-symfony && composer install --no-dev --no-interaction --classmap-authoritative

.PHONY: appimage
appimage: build staging-symfony ## Package as a single-file Linux AppImage at build/Skeleton-x86_64.AppImage
	$(PACKAGING)/build-appimage.sh \
		--app-name skeleton \
		--host-binary $(QT_BIN) \
		--symfony-dir build/staging-symfony \
		--frankenphp $${FRANKENPHP:-frankenphp} \
		--caddyfile Caddyfile \
		--desktop packaging/skeleton.desktop \
		--icon packaging/skeleton.png \
		--output build/Skeleton-x86_64.AppImage \
		$${APPIMAGE_UPDATE_INFO:+--update-info "$$APPIMAGE_UPDATE_INFO"}
	@echo
	@echo "AppImage built. Test with: ./build/Skeleton-x86_64.AppImage"

.PHONY: quality
quality: build qmltest ## Run PHPStan, php-cs-fixer (check), PHPUnit, qmllint, qmltest, maker snapshots
	cd ../php && composer quality
	cmake --build $(BUILD_DIR) --target all_qmllint
	../php/tests/snapshot/run.sh

.PHONY: qmltest
qmltest: ## Run QML unit tests (Qt::QuickTest via qmltestrunner)
	cmake -S ../qml -B ../qml/build-tests -DBUILD_TESTING=ON
	cmake --build ../qml/build-tests --target qml_unit_tests --parallel
	QT_QPA_PLATFORM=offscreen ctest --test-dir ../qml/build-tests --output-on-failure -R qml_unit_tests
