from __future__ import annotations

import sys
from pathlib import Path


ROOT = Path(__file__).resolve().parents[2]
sys.path.insert(0, str(ROOT / "scripts"))

import embed_1c_code_vectors as worker  # noqa: E402


def test_embed_pending_code_vectors_upserts_with_precondition(monkeypatch) -> None:
    calls: list[tuple[str, dict]] = []

    def fake_adapter_call(
        adapter_url: str,
        method: str,
        payload: dict,
        *,
        timeout_seconds: int = 180,
    ) -> dict:
        calls.append((method, payload))
        if method == "metadata.code_vector.pending":
            assert payload["embedding_model"] == "local-hashing-v1"
            assert payload["chunk_kinds"] == ["routine"]
            assert payload["max_text_chars"] == 4000
            return {
                "status": "ok",
                "chunks": [
                    {
                        "chunk_id": "chunk-1",
                        "text_sha1": "a" * 40,
                        "text": "Процедура РассчитатьНалог()",
                    }
                ],
            }
        if method == "metadata.code_vector.embedding.upsert":
            assert payload["chunk_id"] == "chunk-1"
            assert payload["text_sha1"] == "a" * 40
            assert payload["embedding_model"] == "local-hashing-v1"
            assert len(payload["embedding"]) == 16
            return {"status": "ok", "dimensions": 16}
        raise AssertionError(method)

    monkeypatch.setattr(worker, "adapter_call", fake_adapter_call)

    result = worker.embed_pending_code_vectors(
        adapter_url="http://adapter/rpc",
        base_id="upo_test",
        limit=1,
        dimensions=16,
    )

    assert result["status"] == "ok"
    assert result["embedding"]["dimensions"] == 16
    assert result["counts"] == {
        "pending": 1,
        "processed": 1,
        "stored": 1,
        "conflicts": 0,
        "skipped": 0,
        "errors": 0,
    }
    assert [method for method, _payload in calls] == [
        "metadata.code_vector.pending",
        "metadata.code_vector.embedding.upsert",
    ]


def test_embed_pending_code_vectors_dry_run_does_not_upsert(monkeypatch) -> None:
    calls: list[str] = []

    def fake_adapter_call(
        adapter_url: str,
        method: str,
        payload: dict,
        *,
        timeout_seconds: int = 180,
    ) -> dict:
        calls.append(method)
        return {
            "status": "ok",
            "chunks": [
                {
                    "chunk_id": "chunk-1",
                    "text_sha1": "b" * 40,
                    "text": "Функция НайтиОбъект()",
                }
            ],
        }

    monkeypatch.setattr(worker, "adapter_call", fake_adapter_call)

    result = worker.embed_pending_code_vectors(
        adapter_url="http://adapter/rpc",
        base_id="upo_test",
        dimensions=8,
        dry_run=True,
    )

    assert result["counts"]["processed"] == 1
    assert result["counts"]["stored"] == 0
    assert result["upserts"][0]["status"] == "dry_run"
    assert calls == ["metadata.code_vector.pending"]
