SHELL := /bin/bash

.PHONY: check-build test-dev test dev setup alembic-update db build coverage clean

all: dev check-build

# install pre-commit and install hooks from .pre-commit-config.yaml
init:
	brew install pre-commit || sudo apt install pre-commit
	pre-commit install
	# Make sure pycapo gets built
	git submodule update --init --recursive

# Check if local code will pass CI build
check-build: docker-dev-images-locally test-dev

# Run tests on Dockerfile images
test-dev:
	docker run nrao:workflow ./bin/run-tests.sh
	docker run nrao:capability ./bin/run-tests.sh
	docker run nrao:notification ./bin/run-tests.sh

# Run tests on Dockerfile.local containers
test:
	docker exec workspaces-workflow-1 su vlapipe -c "./bin/run-tests.sh"
	docker exec workspaces-capability-1 ./bin/run-tests.sh
	docker exec workspaces-notification-1 ./bin/run-tests.sh

test_cp:
	docker exec workspaces-capability-1 ./bin/run-tests.sh

test_nf:
	docker exec workspaces-notification-1 ./bin/run-tests.sh


# Setup local development environment
dev:
	@echo "starting docker compose up in the background"
	@echo "wait a few seconds and your environment should be done setting up"
	docker compose -f docker-compose.local.yml up -d

alembic-update:
	cd shared/workspaces/alembic; \
	env CAPO_PROFILE=local alembic upgrade head

# Build images from Dockerfile
docker-dev-images-locally:
	docker build -t nrao:workflow -f services/workflow/Dockerfile . --build-arg ENV=local
	docker build -t nrao:capability -f services/capability/Dockerfile . --build-arg ENV=local
	docker build -t nrao:notification -f services/notification/Dockerfile . --build-arg ENV=local

# Build DB image
db:
	docker build --no-cache -t ssa-containers.aoc.nrao.edu/ops/ci/db:workspaces -f ./ci/psql/Dockerfile.db .

# Build docker images
build: db
	docker compose -f docker-compose.local.yml build --no-cache

# Generate HTML coverage report
coverage:
	docker exec workspaces-workflow-1 ./bin/run-tests.sh -b
	cp services/workflow/.coverage ./.coverage.wf
	docker exec workspaces-capability-1 ./bin/run-tests.sh -b
	cp services/capability/.coverage ./.coverage.cap
	docker exec workspaces-notification-1 ./bin/run-tests.sh -b
	cp services/notification/.coverage ./.coverage.no
	coverage combine --append
	coverage html --omit="**/test_*.py,**/_version.py,**/conftest.py,**/*interfaces.py" --skip-empty

# Clean up environment
clean:
# Clean up Docker environment
	docker-compose -f docker-compose.local.yml down || true
# remove nrao: labeled containers
	@docker container list --all | grep nrao: | awk '{ print $$1 }' | xargs docker container rm -f
# remove workspaces and nrao labeled images
	@docker images | grep 'workspaces\|nrao\|cache' | awk '{ print $$3 }' | xargs docker rmi -f
# remove the python egg-info and pytest_cache folders
	@find . \( -name "*.egg-info" -o -name ".pytest_cache" \) -exec rm -r {} \; || true

# Clean things up in a very nasty and destructive way that is sure to derail the rest of your afternoon
reallyclean: clean
	docker system prune --volumes -f
	docker volume prune --filter all=1 -f

# Make the documentation
.PHONY: docs godocs
docs: godocs
	cd docs && make html

godocs:
	./build_go_docs.sh


build base images:
    docker build -t workflow:local -f services/workflow/Dockerfile . --build-arg ENV=local --target dev
    docker build -t workflow:local -f services/workflow/Dockerfile . --build-arg ENV=local --target dev
    docker build -t workflow:local -f services/workflow/Dockerfile . --build-arg ENV=local --target dev


poetrylockall: poetry-lock-pexes poetry-lock-utilities poetry-lock-shared poetry-lock-services

poetry-lock-pexes:
	 poetry lock --no-update -C apps/cli/executables/pexable/carta_envoy
	 poetry lock --no-update -C apps/cli/executables/pexable/casa_envoy
	 poetry lock --no-update -C apps/cli/executables/pexable/conveyor
	 poetry lock --no-update -C apps/cli/executables/pexable/data_annotator
	 poetry lock --no-update -C apps/cli/executables/pexable/deliver
	 poetry lock --no-update -C apps/cli/executables/pexable/ingest_envoy
	 poetry lock --no-update -C apps/cli/executables/pexable/mediator
	 poetry lock --no-update -C apps/cli/executables/pexable/null
	 poetry lock --no-update -C apps/cli/executables/pexable/productfetcher
	 poetry lock --no-update -C apps/cli/executables/pexable/update_stage
	 poetry lock --no-update -C apps/cli/executables/pexable/vela
	 poetry lock --no-update -C apps/cli/executables/pexable/wf_inspector
	 poetry lock --no-update -C apps/cli/executables/pexable/ws_annihilator
	 poetry lock --no-update -C apps/cli/executables/pexable/ws_metrics

poetry-lock-utilities:
	 poetry lock --no-update -C apps/cli/utilities/aat_wrest
	 poetry lock --no-update -C apps/cli/utilities/contacts_wrest
	 poetry lock --no-update -C apps/cli/utilities/core_sampler
	 poetry lock --no-update -C apps/cli/utilities/wf_monitor

poetry-lock-shared:
	 poetry lock --no-update -C shared/messaging
	 poetry lock --no-update -C shared/workspaces

poetry-lock-services:
	 poetry lock --no-update -C services/capability
	 poetry lock --no-update -C services/notification
	 poetry lock --no-update -C services/workflow