SHELL := /usr/bin/env bash
.DEFAULT_GOAL := help

API_DIR := $(CURDIR)
ROOT_DIR := $(abspath $(API_DIR)/..)
CODEGEN_PYTHON ?= $(API_DIR)/.venv-codegen/bin/python
BOOTSTRAP_PYTHON ?= python3
REDOCLY ?= $(API_DIR)/node_modules/.bin/redocly

##@ API contracts

.PHONY: install
install: ## Install pinned OpenAPI tooling
	@if [ ! -x "$(CODEGEN_PYTHON)" ]; then $(BOOTSTRAP_PYTHON) -m venv "$(API_DIR)/.venv-codegen"; fi
	"$(CODEGEN_PYTHON)" -m pip install --disable-pip-version-check -r "$(ROOT_DIR)/scripts/requirements-codegen.txt"
	npm ci --prefix "$(API_DIR)" --ignore-scripts --no-audit --no-fund

.PHONY: bundle
bundle: ## Merge split schemas and create public, ML, code runner, and lab provisioner bundles
	"$(CODEGEN_PYTHON)" "$(ROOT_DIR)/scripts/merge-schemas.py" "$(API_DIR)/core"
	"$(CODEGEN_PYTHON)" "$(ROOT_DIR)/scripts/merge-schemas.py" "$(API_DIR)/ml"
	"$(CODEGEN_PYTHON)" "$(ROOT_DIR)/scripts/merge-schemas.py" "$(API_DIR)/runner"
	"$(CODEGEN_PYTHON)" "$(ROOT_DIR)/scripts/merge-schemas.py" "$(API_DIR)/labd"
	"$(REDOCLY)" bundle "$(API_DIR)/core/openapi.yml" --output "$(API_DIR)/core/openapi.bundle.yml"
	"$(REDOCLY)" bundle "$(API_DIR)/ml/openapi.yml" --output "$(API_DIR)/ml/openapi.bundle.yml"
	"$(REDOCLY)" bundle "$(API_DIR)/runner/openapi.yml" --output "$(API_DIR)/runner/openapi.bundle.yml"
	"$(REDOCLY)" bundle "$(API_DIR)/labd/openapi.yml" --output "$(API_DIR)/labd/openapi.bundle.yml"

.PHONY: lint
lint: bundle ## Lint every bundled contract
	"$(REDOCLY)" lint --config "$(API_DIR)/redocly.yaml" "$(API_DIR)/core/openapi.bundle.yml"
	"$(REDOCLY)" lint --config "$(API_DIR)/redocly.yaml" "$(API_DIR)/ml/openapi.bundle.yml"
	"$(REDOCLY)" lint --config "$(API_DIR)/redocly.yaml" "$(API_DIR)/runner/openapi.bundle.yml"
	"$(REDOCLY)" lint --config "$(API_DIR)/redocly.yaml" "$(API_DIR)/labd/openapi.bundle.yml"

.PHONY: check
check: lint ## Build and lint every contract bundle and run the contract tests
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_merge_schemas.py'
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_contract_copies.py'
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_assessment_contract.py'
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_match_contract.py'
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_runner_contract.py'
	PYTHONDONTWRITEBYTECODE=1 "$(CODEGEN_PYTHON)" -m unittest discover -s "$(ROOT_DIR)/scripts" -p 'test_lab_contract.py'

.PHONY: check-compat
check-compat: bundle ## Compare the public contract with API_BASE_REF
	API_BASE_REF="$(API_BASE_REF)" REDOCLY="$(REDOCLY)" CODEGEN_PYTHON="$(CODEGEN_PYTHON)" bash "$(ROOT_DIR)/scripts/check-api-compat.sh"

.PHONY: check-toolchain
check-toolchain: ## Check OpenAPI 3.2.0 support with ogen, Orval, and Pydantic on the compatibility fixture
	"$(REDOCLY)" lint --config "$(API_DIR)/redocly.yaml" "$(API_DIR)/testdata/openapi-3.2.0.yml"
	$(MAKE) -C "$(ROOT_DIR)/backend" check-ogen-compat
	$(MAKE) -C "$(ROOT_DIR)/frontend" check-orval-compat
	$(MAKE) -C "$(ROOT_DIR)/ml" check-pydantic-compat

.PHONY: fmt
fmt: ## Format handwritten OpenAPI YAML files
	"$(API_DIR)/node_modules/.bin/prettier" --write "$(API_DIR)/**/*.yml" "$(API_DIR)/redocly.yaml"

.PHONY: fmt-check
fmt-check: ## Check OpenAPI YAML formatting
	"$(API_DIR)/node_modules/.bin/prettier" --check "$(API_DIR)/**/*.yml" "$(API_DIR)/redocly.yaml"

.PHONY: verify
verify: check check-toolchain ## Run contract lint and generator compatibility checks

##@ Help

.PHONY: help
help: ## Show available targets
	@awk 'BEGIN{FS=":.*##"; printf "\nUsage:\n  make -C api <target>\n"} /^[a-zA-Z0-9_-]+:.*##/ {printf "  %-16s %s\n", $$1, $$2} /^##@/ {printf "\n%s\n", substr($$0, 5)}' $(MAKEFILE_LIST)
