.PHONY: unit_tests clean push-image image

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")
SDK_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)

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.13.3
	(cd wallaroo && python3 -m openapi_python_client generate --path ../../global_wallaroo_api.json --meta none)

# 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.13.3
	python3 -m openapi_python_client generate --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 hatch==1.7.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
	SETUPTOOLS_SCM_PRETEND_VERSION="${SDK_VERSION}b1" python3 -m hatch build

image: build-sdk
	$(DOCKER) build \
	-t $(SDK_IMAGE):$(TAG) -t $(SDK_IMAGE):latest \
	--cache-to=type=registry,ref=$(SDK_IMAGE):cache,mode=max \
	--cache-from=type=registry,ref=$(SDK_IMAGE):cache \
	--label "org.opencontainers.image.revision=$(REVISION)" \
	--label "org.opencontainers.image.created=$(TIME)" \
	--load \
	-f Dockerfile .

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

# 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

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:
	-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

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
