.PHONY: test

test: test-backend test-frontend

.PHONY: test-frontend

test-frontend:
	cd frontend/app && flutter gen-l10n
	cd frontend/packages/sxchnge_wallet && flutter test
	cd frontend/packages/sxchnge_kyc && flutter test
	cd frontend/packages/sxchnge_analytics && flutter test
	cd frontend/packages/sxchnge_auth && flutter test
	cd frontend/packages/sxchnge_theme && flutter test
	cd frontend/packages/sxchnge_theme/example && flutter test
	cd frontend/app && flutter test

.PHONY: test-backend lint-backend run-backend

test-backend: generate-notifications
	docker pull mongo:latest
	docker pull nats:latest
	cd backend/shared && go test -race -count=1 ./...
	cd backend/notifications/interface && go test -race -count=1 ./...
	cd backend/notifications && go test -race -count=1 ./...
	cd backend/product && go test -race -count=1 ./...

lint-backend: generate-notifications
	cd backend/product && go test ./architecture
	cd backend && test -z "$$(gofmt -l .)"
	cd backend/shared && go vet ./...
	cd backend/notifications/interface && go vet ./...
	cd backend/notifications && go vet ./...
	cd backend/product && go vet ./...

run-backend:
	cd backend/product && go run .

.PHONY: run-notifications generate-notifications
run-notifications: generate-notifications
	cd backend/notifications && go run .

generate-notifications:
	@command -v protoc >/dev/null
	@tmp=$$(mktemp -d); trap 'rm -rf "$$tmp"' EXIT; \
	GOBIN="$$tmp" go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.36.11 && \
	GOBIN="$$tmp" go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.5.1 && \
	PATH="$$tmp:$$PATH" protoc -I backend/notifications/interface/proto --go_out=backend/notifications/interface --go_opt=module=git.sendico.io/tech/sxchnge/notifications/interface --go-grpc_out=backend/notifications/interface --go-grpc_opt=module=git.sendico.io/tech/sxchnge/notifications/interface backend/notifications/interface/proto/v1/interface.proto

.PHONY: package-notifications-interface
package-notifications-interface: generate-notifications
	mkdir -p dist
	tar -czf dist/notifications-proto-v1.tar.gz -C backend/notifications/interface proto README.md
	tar -czf dist/notifications-go-v1.tar.gz -C backend/notifications/interface go.mod go.sum notifications README.md

# Local product stack. Releases are resolved afresh, then pinned by digest.
COMPOSE = docker compose --project-directory . -f infra/local/compose.yml --env-file .local/dev/images.env
.PHONY: build up down logs ps test-dev
build:
	sh infra/local/tools.sh build
	sh infra/local/tools.sh images
	$(COMPOSE) pull vault mongo
	$(COMPOSE) build --pull

up:
	sh infra/local/tools.sh up

down:
	$(COMPOSE) down

logs:
	$(COMPOSE) logs --tail=100 -f

ps:
	$(COMPOSE) ps

test-dev:
	sh infra/local/tools.sh test
	sh infra/local/tools.sh verify

.PHONY: test-dev-infra
test-dev-infra:
	sh infra/local/tools.sh test
	sh infra/local/tools.sh verify-infra

.PHONY: test-dev-rotation
test-dev-rotation:
	sh infra/local/tools.sh rotation

.PHONY: email test-dev-tooling
email:
	sh infra/local/tools.sh email

test-dev-tooling:
	sh infra/local/tools.sh test
