.PHONY: test

test: test-backend test-frontend

.PHONY: test-frontend

test-frontend:
	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
	cd backend/shared && go test -race ./...
	cd backend/notifications/interface && go test -race ./...
	cd backend/notifications && go test -race ./...
	cd backend/product && go test -race ./...

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
