VENV := .venv/bin
POLICY_AI_DIR := ../policy-ai
OPENAPI_URL ?= https://api.musubilabs.ai/policyai/openapi.json

.PHONY: generate generate-from-url generate-from-monorepo fix-spec install test lint clean

## Regenerate the SDK from the public OpenAPI URL.
## In CI / for users without a local policy-ai checkout, this is the canonical path.
generate: generate-from-url

generate-from-url: fetch-spec fix-spec generate-client copy-generated

## Legacy: regenerate from a sibling policy-ai/ checkout (internal Musubi devs).
generate-from-monorepo: extract-spec fix-spec generate-client copy-generated

fetch-spec:
	curl -sSL $(OPENAPI_URL) -o openapi.json
	@echo "OpenAPI spec version: $$(python3 -c "import json; print(json.load(open('openapi.json'))['info']['version'])")"

extract-spec:
	cd $(POLICY_AI_DIR) && \
		DOCS_VIEW=deployed INCLUDE_OPENAPI_ENDPOINTS=true ENABLE_V2_FUNCTIONALITY=true \
		.venv/bin/python ../sdk/scripts/extract_openapi.py

fix-spec:
	python3 scripts/fix_openapi_spec.py openapi.json

generate-client:
	rm -rf musubi-generated
	$(VENV)/openapi-python-client generate --path openapi.json --config generator-config.yaml

copy-generated:
	rm -rf src/musubi/_generated
	mkdir -p src/musubi/_generated
	touch src/musubi/_generated/__init__.py
	cp -r "musubi-generated/musubi._generated" src/musubi/_generated/policyai

## Install the SDK in editable mode with dev dependencies
install:
	$(VENV)/pip install -e ".[dev]"

## Run tests
test:
	$(VENV)/pytest tests/ -v

## Run linter
lint:
	$(VENV)/ruff check src/ tests/
	$(VENV)/ruff format --check src/ tests/

## Format code
format:
	$(VENV)/ruff check --fix src/ tests/
	$(VENV)/ruff format src/ tests/

## Remove generated artifacts
clean:
	rm -rf musubi-generated
	rm -rf src/musubi/_generated
	rm -f openapi.json
