project_root := justfile_directory()
env_file := project_root + "/Env/.env.production"
backend_dir := project_root + "/Backend"
backend_bake := project_root + "/Backend/docker-bake.hcl"
proxy_bake := project_root + "/Proxy/docker-bake.hcl"
local_compose := project_root + "/Docker/docker-compose.local.prod.yaml"
api_image := "goko/royal-pop/api:local-prod"
proxy_image := "goko/royal-pop/proxy:local-prod"

# Default flow: rebuild the local production images and recreate the full stack.
rebuild:
	set -a && source '{{env_file}}' && set +a && cd '{{backend_dir}}' && docker buildx bake -f '{{backend_bake}}' --set '*.no-cache=true' prod && cd '{{project_root}}' && docker buildx bake -f '{{proxy_bake}}' --set '*.no-cache=true' prod && docker compose --env-file '{{env_file}}' -f '{{local_compose}}' up -d --remove-orphans --force-recreate

# Build the local production API and proxy images.
build:
	set -a && source '{{env_file}}' && set +a && cd '{{backend_dir}}' && docker buildx bake -f '{{backend_bake}}' prod && cd '{{project_root}}' && docker buildx bake -f '{{proxy_bake}}' prod

# Start the local production stack in the background using the current image.
up:
	docker compose --env-file '{{env_file}}' -f '{{local_compose}}' up -d --remove-orphans --force-recreate

# Build first, then start the local production stack in the background.
start: build up

# Stop and remove the local production stack.
down:
	docker compose --env-file '{{env_file}}' -f '{{local_compose}}' down --remove-orphans --volumes

# Follow logs for the local production stack.
logs:
	docker compose --env-file '{{env_file}}' -f '{{local_compose}}' logs -f

# Restart the local production stack.
restart:
	docker compose --env-file '{{env_file}}' -f '{{local_compose}}' restart

# Stop the local production stack and remove local images.
clean:
	docker compose --env-file '{{env_file}}' -f '{{local_compose}}' down --remove-orphans --volumes
	docker image rm -f '{{api_image}}' >/dev/null 2>&1 || true
	docker image rm -f '{{proxy_image}}' >/dev/null 2>&1 || true
