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 \
        test-configurations test-developer_controlled_wallets \
        test-smart_contract_platform test-user_controlled_wallets \
        $(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)

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

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

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

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

# ── 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/$$(CLIENT_UNDERSCORE))
	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/$$(CLIENT_UNDERSCORE) $$(CLIENT_DIR)/test/
	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 .
	uv pip install "circle-configurations @ generated/configurations"
	uv pip install "circle-developer-controlled-wallets @ generated/developer_controlled_wallets"
	uv pip install "circle-smart-contract-platform @ generated/smart_contract_platform"
	uv pip install "circle-user-controlled-wallets @ generated/user_controlled_wallets"

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

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