.PHONY: unit_tests clean push-image image image-x86_64 image-aarch64 push-image-x86_64 push-image-aarch64 pre-commit

TAG                      ?= $(shell git rev-parse --short  HEAD)
IMAGE_ROOT                = ghcr.io/wallaroolabs
SDK_IMAGE                 = $(IMAGE_ROOT)/sdk
DOCKER                    = docker buildx
REVISION                  = $(shell git describe --match="" --always --abbrev=40 --dirty)
TIME                      = $(shell date -u +"%Y-%m-%dT%H:%M:%SZ")
PRODUCT_VERSION          	= $(shell git describe --dirty --tags --long --match "v20[0-9]*.[0-9]*.[0-9]*" | grep -oE '20[0-9]{2}\.[0-9]+\.[0-9]+')
LOCAL_VERSION             = $(shell git rev-parse --short HEAD)
SDK_VERSION              ?= $(PRODUCT_VERSION)+$(LOCAL_VERSION)
DOCKER_PLATFORM_x86_64    = amd64
DOCKER_PLATFORM_aarch64   = arm64

default:
	echo "What do you want to do?"

# This runs the openapi client generator and puts the client library project folder in this directory.
generate-openapi: clean
	python3 -m pip install -q openapi-python-client==0.26.1
	(cd wallaroo && python3 -m openapi_python_client generate --fail-on-warning --path ../../global_wallaroo_api.json --meta none --config ../openapi-python-client-config.yaml)

# If we publish the MLOps client library separately for 3rd party Python consumers, we can use the autogenerated Poetry version.
generate-openapi-poetry: clean
	python3 -m pip install -q openapi-python-client==0.26.1
	python3 -m openapi_python_client generate --fail-on-warning --path ../../global_wallaroo_api.json --meta poetry

# Generate the OpenAPI client, build it, and install it in the local python3 pip environment.
build-openapi: generate-openapi-poetry
	python3 -m pip install -q poetry==1.1.15
	cd wallaroo-ml-ops-api-client && python3 -m poetry config virtualenvs.create false && python3 -m poetry build -vvv
	python3 -m pip install -q ./wallaroo-ml-ops-api-client/dist/wallaroo_ml_ops_api_client-*.whl

ensure_hatch:
	python3 -m pip install 'virtualenv>=20.26.6,<21' hatch==1.14.0

# This is a convenience for unit tests that will symlink into the SDK and need its dependencies.
install-sdk-requirements: ensure_hatch
	python3 -m hatch -e default dep show requirements | xargs python3 -m pip install

build-sdk: clean generate-openapi ensure_hatch
	python3 --version
	SETUPTOOLS_SCM_PRETEND_VERSION="${SDK_VERSION}" python3 -m hatch build

global_wallaroo_api.json:
	cp ../global_wallaroo_api.json .

build_image = \
	$(DOCKER) build \
	-t $(SDK_IMAGE):$(TAG)-$(1) \
	--label "org.opencontainers.image.revision=$(REVISION)" \
	--label "org.opencontainers.image.created=$(TIME)" \
	--platform "linux/$(DOCKER_PLATFORM_$(1))" \
	--progress=plain \
	--load \
	-f Dockerfile .

image: image-x86_64 image-aarch64

image-x86_64: build-sdk global_wallaroo_api.json
	$(call build_image,x86_64)

image-aarch64: build-sdk global_wallaroo_api.json
	$(call build_image,aarch64)

push-image: push-image-x86_64 push-image-aarch64
	docker manifest create "$(SDK_IMAGE):$(TAG)" \
		"$(SDK_IMAGE):$(TAG)-x86_64" \
		"$(SDK_IMAGE):$(TAG)-aarch64"
	docker manifest push "$(SDK_IMAGE):$(TAG)"

push-image-x86_64:
	docker push $(SDK_IMAGE):$(TAG)-x86_64

push-image-aarch64:
	docker push $(SDK_IMAGE):$(TAG)-aarch64

# TODO: Shouldn't need to build/install client for unit tests, need to refactor dependencies further.
unit_tests: generate-openapi ensure_hatch
	python3 -m hatch run test:unit

# In CI, pull openapi from cache
unit_tests-ci: ensure_hatch
	python3 -m hatch run test:unit

# TODO: generate-openapi might be cleaner hidden as a pre-build configuration in Hatch.
doc: generate-openapi ensure_hatch
	python3 -m hatch run pdoc:build

doc-serve: generate-openapi ensure_hatch
	python3 -m hatch run pdoc:serve

publish-test:
	python -m twine upload --config-file ~/.pypirc-rsolis --repository testpypi --skip-existing dist/*

publish:
	python -m twine upload --config-file ~/.pypirc-wl --skip-existing dist/*

clean: ensure_hatch
	-python3 -m hatch clean
	-python3 -m pip uninstall -y wallaroo-ml-ops-api-client
	-rm -rf dist build
	-rm -rf wallaroo.egg-info
	-rm -rf wallaroo/*.egg-info
	-rm -rf .pytest_cache
	-rm -rf .mypy_cache wallaroo/.mypy_cache
	-rm -rf wallaroo-ml-ops-api-client wallaroo/wallaroo_ml_ops_api_client
	-find . -name __pycache__ -type d -exec rm -rf {} \;

install-jupyter:
	kubectl apply -f /Users/julio/Wallaroo/local-jupyter.yml

uninstall-jupyter:
	kubectl delete -f /Users/julio/Wallaroo/local-jupyter.yml

portforward-jupyter:
	open  -a "Google Chrome" http://localhost:8888/
	kubectl port-forward deployment.apps/jupyter 8888:8888

portforward-console:
	open -a "Google Chrome" http://localhost:8080/
	kubectl port-forward service/graphql-api 8080:8080

portforward-lb:
	kubectl port-forward service/api-lb 8080:8080

portforward-rest-api:
	kubectl port-forward service/rest-api 3030:3030

portforward-graphql-api:
	kubectl port-forward service/graphql-api 8080:8080

portforward-dashboard:
	open -a "Google Chrome" http://localhost:4567/
	kubectl port-forward service/dashboard 4567:4567

check: ensure_hatch generate-openapi
	python3 -m hatch run test:typecheck
	python3 -m hatch run test:format
	python3 -m hatch run test:lint

pre-commit:
	cd .. && pre-commit run --config sdk/.pre-commit-config.yaml

copy_files_for_testing:
	cp ../../wallaroo-plus/fitzroy/test_resources/dev_smoke_test.json . 
	cp ../../wallaroo-plus/fitzroy/test_resources/keras_ccfraud.onnx . 
	cp ../../wallaroo-plus/apps/demos/abtest/modelA.onnx . 
	cp ../../wallaroo-plus/apps/demos/abtest/modelB.onnx .
	cp docs/source/experiments.ipynb . 

delete_testing_files:
	rm  dev_smoke_test.json keras_ccfraud.onnx modelA.onnx modelB.onnx
	cp experiments.ipynb ../..
	rm experiments.ipynb


mypy:
	mypy wallaroo
