"""Dense embeddings via Spark Control /v1/embeddings (BAAI/bge-m3, 1024-d)."""
import config
import http_util


def dense_embed(texts, batch=32):
    out = []
    for i in range(0, len(texts), batch):
        group = texts[i:i + batch]
        status, data = http_util.request(
            "POST", f"{config.SPARK_CONTROL_URL}/v1/embeddings",
            {"input": group, "model": config.EMBED_MODEL}, verify=config.SPARK_VERIFY_TLS)
        if status != 200:
            raise RuntimeError(f"/v1/embeddings -> {status}: {data}")
        rows = sorted(data["data"], key=lambda d: d["index"])
        out.extend(r["embedding"] for r in rows)
    return out
