VENV       := .venv
PYTHON     := $(VENV)/bin/python
PIP        := $(VENV)/bin/pip
RUFF       := $(VENV)/bin/ruff
MYPY       := $(VENV)/bin/mypy
PYTEST     := $(VENV)/bin/pytest
PRE_COMMIT := $(VENV)/bin/pre-commit
BUILD      := $(VENV)/bin/python -m build

.PHONY: install lint format typecheck test test-offline clean build all update-hooks

install:
	$(PIP) install -e ".[dev]"
	$(PIP) install build 2>/dev/null || true

lint:
	$(RUFF) check .

format:
	$(RUFF) format .

typecheck:
	$(MYPY) openscire-core/src/openscire

test:
	$(PYTEST) -v; st=$$?; if [ $$st -eq 5 ]; then exit 0; else exit $$st; fi

test-offline:
	$(PYTEST) -v -m "not network" --no-header; st=$$?; if [ $$st -eq 5 ]; then exit 0; else exit $$st; fi

clean:
	rm -rf __pycache__ .mypy_cache .pytest_cache .ruff_cache build/ dist/ *.egg-info
	find . -type d -name __pycache__ -exec rm -rf {} + 2>/dev/null || true

build:
	$(BUILD)

update-hooks:
	$(PRE_COMMIT) autoupdate

# ── Jupyter Extension Targets ─────────────────────────────────────────────────

JUPYTER_DIR   := openscire-jupyter
JUPYTER_VENV  := $(JUPYTER_DIR)/.venv
JUPYTER_PY    := $(JUPYTER_VENV)/bin/python
JUPYTER_PIP   := $(JUPYTER_VENV)/bin/pip

install-jupyter:
	test -d $(JUPYTER_VENV) || python3 -m venv $(JUPYTER_VENV)
	$(JUPYTER_PIP) install -e ".[dev]"
	$(JUPYTER_PIP) install -e "$(JUPYTER_DIR)[dev]"
	cd $(JUPYTER_DIR) && npm install && npm run build:labextension

test-jupyter:
	$(JUPYTER_PY) -m pytest $(JUPYTER_DIR)/tests -v --no-cov

lint-jupyter:
	$(RUFF) check $(JUPYTER_DIR)/openscire_jupyter $(JUPYTER_DIR)/tests

all: lint format typecheck test build
