.PHONY: all frontend backend clean dev docker docker-run tests check-static

all: frontend backend

frontend:
	rm -rf frontend/public/dist
	cd frontend && bun run build
	rm -rf backend/web/static
	mkdir -p backend/web/static
	cp -R frontend/public/. backend/web/static/
	find backend/web/static -type d -empty -delete
	touch backend/web/static/.gitkeep

check-static:
	@test -f backend/web/static/index.html || (echo "missing backend/web/static/index.html; run 'make frontend' first" && exit 1)
	@test -f backend/web/static/dist/main.js || (echo "missing backend/web/static/dist/main.js; run 'make frontend' first" && exit 1)
	@test -f backend/web/static/dist/styles.css || (echo "missing backend/web/static/dist/styles.css; run 'make frontend' first" && exit 1)

backend: check-static
	cd backend && go build -o ./dist/aethera ./cmd

clean:
	rm -rf frontend/public/dist
	rm -rf backend/dist
	rm -rf backend/web/static

dev:
	rm -rf frontend/public/dist
	cd frontend && bun run build
	cd backend && AETHERA_STATIC_DIR=../frontend/public air & \
	backend_pid=$$!; \
	trap 'kill $$backend_pid' INT TERM EXIT; \
	cd frontend && bun run dev

docker:
	docker build -t aethera .

docker-run:
	docker run -p 8080:8080 -v aethera-data:/app/data aethera

tests:
	cd backend && go test ./...
