.PHONY: help install-dev build-collection import-galaxy docs docs-serve test test-units test-sanity \
	test-setup test-integration test-integration-sros test-integration-srlinux install-artifact

PYTHON ?= python3
PYTHON_VERSION = $$($(PYTHON) -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
# Test inputs from .github/test-matrix.yml. An integration case sets SROS_VERSION or SRL_VERSION,
# which picks the lab (SR Linux when neither is set). REGISTRY is the image path of the lab node;
# a release without a patch number, e.g. 26.10, runs the newest patch release in it.
PLATFORM := $(if $(SROS_VERSION),sros,srlinux)
ANSIBLE_VERSION ?= 2.22
SRL_VERSION ?= 25.10
SROS_VERSION ?= 25.10.R1
SRL_REGISTRY = $(or $(REGISTRY),ghcr.io/nokia/srlinux)
SROS_REGISTRY = $(or $(REGISTRY),registry.srlinux.dev/pub/nokia_srsim)
# The integration tests use the built artifact, installed here, not the checkout
COLLECTIONS := $(CURDIR)/.tmp/collections
JUNIT_DIR := tests/output/junit

help:
	@echo "OpenConfig Ansible collection — common tasks"
	@echo ""
	@echo "  make install-dev      Install requirements-dev.txt"
	@echo "  make build-collection Build collection tarball (protos + ansible-galaxy collection build)"
	@echo "  make import-galaxy    Run galaxy-importer on built tarball (requires build-collection)"
	@echo "  make test             The suites that need no lab (test-sanity, test-units)"
	@echo "  make docs             Generate reference + build MkDocs site (site/)"
	@echo "  make docs-serve       Serve docs at http://127.0.0.1:8000"
	@echo ""
	@echo "Test suites (.github/test-matrix.yml), JUnit in $(JUNIT_DIR):"
	@echo "  make test-setup       Install ansible-core \$$ANSIBLE_VERSION, requirements-dev.txt, protos"
	@echo "  make test-sanity      ansible-test sanity"
	@echo "  make test-units       ansible-test units and a syntax check of the shipped playbooks"
	@echo "  make test-integration Containerlab, tests/playbooks.yml: SR OS when \$$SROS_VERSION is set, else SR Linux"
	@echo "  make test-integration-sros     SR OS \$$SROS_VERSION from \$$REGISTRY (needs an SR-SIM license)"
	@echo "  make test-integration-srlinux  SR Linux \$$SRL_VERSION from \$$REGISTRY"

install-dev:
	$(PYTHON) -m pip install --upgrade pip setuptools wheel
	$(PYTHON) -m pip install -r requirements-dev.txt

build-collection:
	tools/build_openconfig_protos.sh
	ansible-galaxy collection build .

import-galaxy:
	@VER=$$(grep '^version:' galaxy.yml | awk '{print $$2}'); \
	ARTIFACT="nokia-openconfig-$$VER.tar.gz"; \
	test -f "$$ARTIFACT" || (echo "Artifact $$ARTIFACT not found; run make build-collection first" >&2; exit 1); \
	tools/import_galaxy.sh "$$ARTIFACT"

test: test-sanity test-units

test-setup:
	$(PYTHON) -m pip install --upgrade pip
ifeq ($(ANSIBLE_VERSION),devel)
	$(PYTHON) -m pip install -r requirements-dev.txt https://github.com/ansible/ansible/archive/devel.tar.gz
else
	$(PYTHON) -m pip install -r requirements-dev.txt "ansible-core==$(ANSIBLE_VERSION).*"
endif
	tools/build_openconfig_protos.sh

test-sanity:
	tools/build_openconfig_protos.sh
	rm -rf tests/output site
	tools/ansible_test.sh sanity --venv --python $(PYTHON_VERSION) -v --junit

test-units: install-artifact
	rm -rf tests/output site
	tools/ansible_test.sh units --venv --python $(PYTHON_VERSION) -v
	ansible-inventory -i playbooks/examples/inventory.yml --graph
	for playbook in playbooks/*.yml; do \
		ANSIBLE_COLLECTIONS_PATH=$(COLLECTIONS) ansible-playbook --syntax-check -i playbooks/examples/inventory.yml \
			"nokia.openconfig.$$(basename "$$playbook" .yml)" || exit 1; \
	done

test-integration: test-integration-$(PLATFORM)

test-integration-sros: install-artifact
	tag=$$($(PYTHON) tools/resolve_image.py "$(SROS_REGISTRY)" "$(SROS_VERSION)") && \
	SROS_IMAGE="$(SROS_REGISTRY)" SROS_VERSION="$$tag" ANSIBLE_COLLECTIONS_PATH=$(COLLECTIONS) \
		tests/run.sh integration --topo sros.clab.yml --suite integration

test-integration-srlinux: install-artifact
	tag=$$($(PYTHON) tools/resolve_image.py "$(SRL_REGISTRY)" "$(SRL_VERSION)") && \
	SRL_IMAGE="$(SRL_REGISTRY)" SRL_VERSION="$$tag" ANSIBLE_COLLECTIONS_PATH=$(COLLECTIONS) \
		tests/run.sh integration --topo srlinux.clab.yml --suite integration

install-artifact:
	tools/build_openconfig_protos.sh
	rm -rf .tmp/dist $(COLLECTIONS)
	ansible-galaxy collection build . --output-path .tmp/dist
	ANSIBLE_COLLECTIONS_PATH=$(COLLECTIONS) \
		ansible-galaxy collection install --force -p $(COLLECTIONS) .tmp/dist/nokia-openconfig-*.tar.gz

docs:
	@echo "Generating module and connection reference..."
	$(PYTHON) tools/generate_docs.py
	mkdocs build --clean

docs-serve:
	$(PYTHON) tools/generate_docs.py
	mkdocs serve
