.PHONY: all setup test runner-get runner-analyze runner-test runner-test-legacy-iop client-get client-analyze client-test core-test core-run client-run-web proto proto-go proto-dart

PROTOC_GEN_DART := $(shell command -v protoc-gen-dart 2>/dev/null)
OTO_CORE_ADDR ?= 0.0.0.0:18020
OTO_WEB_PORT ?= 13020
OTO_SERVER_HTTP_URL ?= http://localhost:18020
RUNNER_REQUIRED_TESTS := \
	test/oto_agent_bootstrap_script_test.dart \
	test/oto_agent_cli_test.dart \
	test/oto_agent_config_test.dart \
	test/oto_agent_migration_plan_test.dart \
	test/oto_agent_registration_test.dart \
	test/oto_application_test.dart \
	test/oto_catalog_cli_test.dart \
	test/oto_cli_runtime_test.dart \
	test/oto_command_catalog_test.dart \
	test/oto_command_runtime_test.dart \
	test/oto_context_test.dart \
	test/oto_core_test.dart \
	test/oto_scheduler_runtime_test.dart \
	test/oto_server_connection_smoke_test.dart \
	test/oto_system_runtime_test.dart \
	test/oto_validate_cli_test.dart

all: setup test

setup: runner-get client-get

runner-get:
	cd apps/runner && dart pub get

runner-analyze:
	cd apps/runner && dart analyze

runner-test:
	cd apps/runner && dart test $(RUNNER_REQUIRED_TESTS)

runner-test-legacy-iop:
	cd apps/runner && OTO_ENABLE_LEGACY_IOP_SMOKE=1 dart test test/oto_iop_connection_smoke_test.dart

client-get:
	cd apps/client && flutter pub get

client-analyze:
	cd apps/client && flutter analyze

client-test:
	cd apps/client && flutter test

core-test:
	cd services/core && go test ./...

core-run:
	cd services/core && OTO_CORE_ADDR=$(OTO_CORE_ADDR) go run ./cmd/oto-core

client-run-web:
	cd apps/client && flutter run -d chrome --web-port=$(OTO_WEB_PORT) \
		--dart-define=OTO_SERVER_HTTP_URL=$(OTO_SERVER_HTTP_URL)

proto: proto-go proto-dart

proto-go:
	mkdir -p services/core/oto
	protoc -I proto --go_out=services/core --go_opt=paths=source_relative proto/oto/runner.proto

proto-dart:
ifndef PROTOC_GEN_DART
	$(error "protoc-gen-dart not found. Please install it to generate Dart protobuf files.")
endif
	mkdir -p apps/runner/lib/oto/agent
	protoc -I proto --dart_out=apps/runner/lib/oto/agent proto/oto/runner.proto

test: runner-test client-test core-test
