VERSION := $(shell cat version.txt)
OPENAPI_GENERATOR_VERSION := $(shell grep '"version"' openapitools.json | head -1 | sed 's/.*: *"\(.*\)".*/\1/')
CLIENTS := configurations developer-controlled-wallets user-controlled-wallets smart-contract-platform

# Skip automatic project sync on every `uv run` invocation — we manage the env explicitly.
export UV_NO_SYNC := 1

# Extra args passed to pytest (e.g. --cov-report xml:coverage.xml from CI).
PYTEST_ARGS ?=

.PHONY: test test-unit test-e2e test-all lint lint-fix build install-dist publish clean-generated \
        $(addprefix test-circle-,$(CLIENTS)) \
        $(addprefix generate-,$(CLIENTS))

# ── Test targets ──────────────────────────────────────────────────────────────

test: test-unit

test-unit:
	uv run pytest generated/ -m unit $(PYTEST_ARGS)

test-e2e:
	uv run pytest generated/ -m e2e $(PYTEST_ARGS)

test-all:
	uv run pytest generated/ $(PYTEST_ARGS)

# Per-client test targets: `make test-circle-<client>` (e.g. test-circle-configurations)
define test-client
test-circle-$(1):
	uv run pytest generated/circle-$(1)/ -m unit $$(PYTEST_ARGS)
endef

$(foreach client,$(CLIENTS),$(eval $(call test-client,$(client))))

# ── Lint targets ──────────────────────────────────────────────────────────────

lint:
	uv run ruff check .

lint-fix:
	uv run ruff check --fix .

# ── Build targets ─────────────────────────────────────────────────────────────

clean-generated:
	mkdir -p generated
	find generated -not -regex 'generated/_*' -delete

# Per-client code generation (can run in parallel via make -j)
define generate-client
generate-$(1): clean-generated
	$$(eval CLIENT_UNDERSCORE := $$(subst -,_,$(1)))
	$$(eval CLIENT_DIR := ./generated/circle-$(1))
	uvx --from "openapi-generator-cli==$(OPENAPI_GENERATOR_VERSION)" openapi-generator-cli generate \
		-i "w3s-openapi-internal/dist/$(1).yaml" \
		-g python \
		-o "$$(CLIENT_DIR)" \
		-t .openapi-generator/templates-$(1) \
		-p packageName=circle.web3.$$(CLIENT_UNDERSCORE),packageVersion=$(VERSION),useInlineModelResolver=true,nonCompliantUseDiscriminatorIfCompositionFails=false,projectName="circle-$(1)" \
		--type-mappings UUID=str \
		--global-property apiTests=false,modelTests=false
	mkdir -p "$$(CLIENT_DIR)/test"
	find tests/ -maxdepth 1 -type f -exec cp {} $$(CLIENT_DIR)/test/ \;
	cp -r tests/circle-$(1) $$(CLIENT_DIR)/test/$$(CLIENT_UNDERSCORE)
	cp pytest.ini "$$(CLIENT_DIR)/pytest.ini"
	cp .coveragerc "$$(CLIENT_DIR)/.coveragerc"
	cp test-requirements.txt "$$(CLIENT_DIR)/test-requirements.txt"
	rm -rf "$$(CLIENT_DIR)/.github"
	rm -f "$$(CLIENT_DIR)/.gitlab-ci.yml"
	rm -f "$$(CLIENT_DIR)/.travis.yml"
endef

$(foreach client,$(CLIENTS),$(eval $(call generate-client,$(client))))

build: $(addprefix generate-,$(CLIENTS))

# ── Install targets ───────────────────────────────────────────────────────────

install-dist:
	uv pip install .
	@for client in $(CLIENTS); do \
		uv pip install "circle-$${client} @ generated/circle-$${client}"; \
	done

# ── Publish targets ───────────────────────────────────────────────────────────

publish:
	uv build --sdist --no-create-gitignore --out-dir dist/
	uv publish --check-url $(or $(UV_PUBLISH_CHECK_URL),https://pypi.org/simple/) dist/*
	@for client in $(CLIENTS); do \
		uv publish --check-url $(or $(UV_PUBLISH_CHECK_URL),https://pypi.org/simple/) generated/circle-$${client}/dist/*; \
	done
