.PHONY: build deps up down setup deploy redeploy logs clean package release

# Install dependencies (composer via Docker since PHP may not be local)
deps:
	npm install --no-audit --no-fund
	docker run --rm -v "$$(pwd):/app" -w /app composer:2 install --no-dev --optimize-autoloader --no-interaction

# Build frontend
build: deps
	npx webpack --node-env production --progress

# Start containers (detached)
up:
	docker compose up -d

# Wait for NC to be ready, install it, copy app in, enable it
setup:
	@echo "Waiting for Nextcloud container to be ready..."
	@until docker compose exec -T nextcloud test -f config/CAN_INSTALL 2>/dev/null || \
	       docker compose exec -T nextcloud test -f config/config.php 2>/dev/null; do \
		sleep 3; \
		echo "  still waiting..."; \
	done
	@sleep 5
	@echo "Fixing custom_apps ownership..."
	docker compose exec nextcloud chown www-data:www-data /var/www/html/custom_apps
	@echo "Installing Nextcloud..."
	docker compose exec -u www-data nextcloud php occ maintenance:install \
		--database=mysql \
		--database-host=db \
		--database-name=$${MYSQL_DATABASE:-nextcloud} \
		--database-user=$${MYSQL_USER:-nextcloud} \
		--database-pass=$$(grep MYSQL_PASSWORD .env | head -1 | cut -d= -f2) \
		--admin-user=$$(grep NEXTCLOUD_ADMIN_USER .env | cut -d= -f2) \
		--admin-pass=$$(grep NEXTCLOUD_ADMIN_PASSWORD .env | cut -d= -f2) \
		2>&1 || true
	@echo "Copying app into Nextcloud..."
	docker compose exec nextcloud mkdir -p /var/www/html/custom_apps/mitgliederverwaltung
	docker compose exec nextcloud cp -a /app-src/appinfo /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/lib /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/templates /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/js /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/vendor /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud chown -R www-data:www-data /var/www/html/custom_apps/mitgliederverwaltung
	@echo "Enabling app..."
	docker compose exec -u www-data nextcloud php occ app:enable mitgliederverwaltung
	@echo ""
	@echo "Done! Nextcloud is running at http://localhost:8080"
	@echo "Login with credentials from .env (NEXTCLOUD_ADMIN_USER / NEXTCLOUD_ADMIN_PASSWORD)"

# Full deploy: build, start, install, enable
deploy: build up setup

# Rebuild and redeploy app into running NC
redeploy: build
	docker compose exec nextcloud rm -rf /var/www/html/custom_apps/mitgliederverwaltung
	docker compose exec nextcloud mkdir -p /var/www/html/custom_apps/mitgliederverwaltung
	docker compose exec nextcloud cp -a /app-src/appinfo /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/lib /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/templates /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/js /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud cp -a /app-src/vendor /var/www/html/custom_apps/mitgliederverwaltung/
	docker compose exec nextcloud chown -R www-data:www-data /var/www/html/custom_apps/mitgliederverwaltung
	docker compose exec -u www-data nextcloud php occ upgrade 2>/dev/null || true
	@echo "App redeployed."

down:
	docker compose down

logs:
	docker compose logs -f nextcloud

# Create a distributable tar.gz for production installation
package: build
	$(eval VERSION := $(shell grep '<version>' appinfo/info.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/'))
	@echo "Packaging mitgliederverwaltung v$(VERSION)..."
	mkdir -p artifacts
	rm -rf /tmp/mitgliederverwaltung
	mkdir -p /tmp/mitgliederverwaltung
	cp -a appinfo lib templates js vendor img /tmp/mitgliederverwaltung/
	@for f in LICENSE README.md CHANGELOG.md; do \
		[ -f "$$f" ] && cp "$$f" /tmp/mitgliederverwaltung/ || true; \
	done
	cd /tmp && tar -czf mitgliederverwaltung-$(VERSION).tar.gz mitgliederverwaltung
	mv /tmp/mitgliederverwaltung-$(VERSION).tar.gz artifacts/
	rm -rf /tmp/mitgliederverwaltung
	@# Sign the tarball with Ed25519
	@if [ -f "$$HOME/.mv-release-key" ]; then \
		docker run --rm -v "$$(pwd):/app" -v "$$HOME/.mv-release-key:$$HOME/.mv-release-key:ro" -e HOME="$$HOME" -w /app php:8.1-cli php scripts/sign-release.php artifacts/mitgliederverwaltung-$(VERSION).tar.gz; \
	else \
		echo "WARNING: ~/.mv-release-key not found — tarball NOT signed."; \
		echo "  Run: php scripts/generate-keypair.php"; \
	fi
	@echo ""
	@echo "Package created: artifacts/mitgliederverwaltung-$(VERSION).tar.gz"
	@echo "Size: $$(du -h artifacts/mitgliederverwaltung-$(VERSION).tar.gz | cut -f1)"

# Build, package, tag, and create a Gitea release with the tarball attached
release: package
	$(eval VERSION := $(shell grep '<version>' appinfo/info.xml | sed 's/.*<version>\(.*\)<\/version>.*/\1/'))
	@echo "Creating release v$(VERSION) on Gitea..."
	git tag -a "v$(VERSION)" -m "Release v$(VERSION)" 2>/dev/null || echo "Tag v$(VERSION) already exists, skipping"
	git push origin "v$(VERSION)" 2>/dev/null || echo "Tag already pushed"
	@# Create Gitea release via API
	@RELEASE_ID=$$(curl -s -X POST \
		"https://git.shahondin1624.de/api/v1/repos/shahondin1624/Mitgliederverwaltung/releases" \
		-H "Content-Type: application/json" \
		-H "Authorization: token $$(cat ~/.gitea-token 2>/dev/null || echo '')" \
		-d '{"tag_name": "v$(VERSION)", "name": "v$(VERSION)", "body": "Release v$(VERSION)", "draft": false, "prerelease": false}' \
		| python3 -c "import sys,json; print(json.load(sys.stdin).get('id',''))" 2>/dev/null) && \
	if [ -n "$$RELEASE_ID" ] && [ "$$RELEASE_ID" != "" ]; then \
		echo "Uploading tarball to release $$RELEASE_ID..."; \
		curl -s -X POST \
			"https://git.shahondin1624.de/api/v1/repos/shahondin1624/Mitgliederverwaltung/releases/$$RELEASE_ID/assets?name=mitgliederverwaltung-$(VERSION).tar.gz" \
			-H "Authorization: token $$(cat ~/.gitea-token 2>/dev/null || echo '')" \
			-F "attachment=@artifacts/mitgliederverwaltung-$(VERSION).tar.gz" \
			>/dev/null && echo "  Tarball uploaded."; \
		if [ -f "artifacts/mitgliederverwaltung-$(VERSION).tar.gz.sig" ]; then \
			curl -s -X POST \
				"https://git.shahondin1624.de/api/v1/repos/shahondin1624/Mitgliederverwaltung/releases/$$RELEASE_ID/assets?name=mitgliederverwaltung-$(VERSION).tar.gz.sig" \
				-H "Authorization: token $$(cat ~/.gitea-token 2>/dev/null || echo '')" \
				-F "attachment=@artifacts/mitgliederverwaltung-$(VERSION).tar.gz.sig" \
				>/dev/null && echo "  Signature uploaded."; \
		else \
			echo "  WARNING: No .sig file found — release is UNSIGNED."; \
		fi; \
		echo "Release v$(VERSION) created."; \
	else \
		echo "WARNING: Could not create Gitea release (missing ~/.gitea-token?). Tarball is in artifacts/."; \
	fi

# Remove volumes (full reset)
clean:
	docker compose down -v
