# Copyright 2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

EAPI=8

DISTUTILS_USE_PEP517=standalone
PYTHON_COMPAT=( python3_{12..14} )

inherit distutils-r1

DESCRIPTION="Context optimization layer for LLM applications (prebuilt wheel)"
HOMEPAGE="https://headroom-docs.vercel.app https://github.com/headroomlabs-ai/headroom"

# Upstream ships maturin-built abi3 (cp310+) wheels only, no standalone
# binaries. PyPI dist name is headroom-ai, wheel name uses an underscore.
_WHL_AMD64="headroom_ai-${PV}-cp310-abi3-manylinux_2_28_x86_64.whl"
_WHL_ARM64="headroom_ai-${PV}-cp310-abi3-manylinux_2_28_aarch64.whl"
SRC_URI="
	amd64? ( https://github.com/headroomlabs-ai/headroom/releases/download/v${PV}/${_WHL_AMD64} )
	arm64? ( https://github.com/headroomlabs-ai/headroom/releases/download/v${PV}/${_WHL_ARM64} )
"
S="${WORKDIR}"

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="-* ~amd64 ~arm64"
IUSE="+proxy reports spreadsheet"
RESTRICT="mirror strip test"

# Notes on upstream deps that are deliberately not expressed here:
# - litellm is a core dep upstream only declares for python < 3.14 (lazily
#   imported and ImportError-guarded on 3.14), hence the target gate below.
# - dev-python/ast-grep-cli is a core dep upstream (bundled ast-grep binary);
#   not packaged here, AST code compression degrades at runtime.
# - sqlite-vec (proxy memory backend) has no ebuild; headroom ImportError-guards
#   it and falls back to the pure-python path
# - sci-ml/transformers is DISTUTILS_SINGLE_IMPL: no python_targets flags to
#   match, so its atom below is deliberately undecorated
RDEPEND="
	dev-python/click[${PYTHON_USEDEP}]
	dev-python/opentelemetry-api[${PYTHON_USEDEP}]
	dev-python/pydantic[${PYTHON_USEDEP}]
	dev-python/pyyaml[${PYTHON_USEDEP}]
	dev-python/rich[${PYTHON_USEDEP}]
	dev-python/tiktoken[${PYTHON_USEDEP}]
	dev-python/tomlkit[${PYTHON_USEDEP}]
	$(python_gen_cond_dep '
		>=dev-python/litellm-1.86.2[${PYTHON_USEDEP}]
	' 3.12 3.13)
	proxy? (
		dev-python/fastapi[${PYTHON_USEDEP}]
		dev-python/httpx[${PYTHON_USEDEP}]
		dev-python/magika[${PYTHON_USEDEP}]
		dev-python/mcp[${PYTHON_USEDEP}]
		dev-python/openai[${PYTHON_USEDEP}]
		dev-python/orjson[${PYTHON_USEDEP}]
		dev-python/starlette[${PYTHON_USEDEP}]
		dev-python/uvicorn[${PYTHON_USEDEP}]
		dev-python/watchdog[${PYTHON_USEDEP}]
		dev-python/websockets[${PYTHON_USEDEP}]
		dev-python/zstandard[${PYTHON_USEDEP}]
		>=sci-libs/onnxruntime-1.24.1[python,${PYTHON_USEDEP}]
		>=sci-ml/transformers-5.5.0
	)
	reports? (
		dev-python/jinja2[${PYTHON_USEDEP}]
	)
	spreadsheet? (
		dev-python/openpyxl[${PYTHON_USEDEP}]
		dev-python/xlrd[${PYTHON_USEDEP}]
	)
"

src_unpack() {
	:
}

src_compile() {
	:
}

python_install() {
	local whl
	case ${ARCH} in
		amd64) whl=${_WHL_AMD64} ;;
		arm64) whl=${_WHL_ARM64} ;;
		*) die "Unsupported architecture: ${ARCH}" ;;
	esac
	# the console script is generated per python target and the wheel
	# installer refuses to overwrite; drop the previous target's copy
	rm -f "${ED}/usr/bin/headroom" || die
	distutils_wheel_install "${ED}" "${DISTDIR}/${whl}"
}