from ims_mcp.clients.document import DocumentClient
from ims_mcp.services.bundler import Bundler
from ims_mcp.services.query_builder import QueryBuilder


class _Doc:
    def __init__(self, doc_id, name, content, meta_fields):
        self.id = doc_id
        self.name = name
        self._content = content
        self.meta_fields = meta_fields

    def download(self):
        return self._content.encode("utf-8")


class _DocClient(DocumentClient):
    def download_content(self, doc):
        return doc._content


def test_bundler_sorts_and_wraps():
    bundler = Bundler(_DocClient())
    docs = [
        _Doc("2", "z.md", "Z", {"sort_order": 2, "tags": ["t2"]}),
        _Doc("1", "a.md", "A", {"sort_order": 1, "tags": ["t1"]}),
    ]

    xml = bundler.bundle(docs, "aia-r1")
    assert xml.index('id="1"') < xml.index('id="2"')
    assert '<rosetta:file id="1" dataset="aia-r1" name="a.md" tags="t1">' in xml


def test_query_builder_tag_format():
    import json
    qb = QueryBuilder()
    params = qb.build_list_params(tags=["agents", "r1"])
    assert "keywords" not in params
    mc = json.loads(params["metadata_condition"])
    assert mc["logic"] == "or"
    assert len(mc["conditions"]) == 2
    assert mc["conditions"][0] == {"name": "tags", "comparison_operator": "contains", "value": "agents"}
    assert mc["conditions"][1] == {"name": "tags", "comparison_operator": "contains", "value": "r1"}


def test_query_builder_query_as_keywords():
    qb = QueryBuilder()
    params = qb.build_list_params(query="bootstrap")
    assert params["keywords"] == "bootstrap"
    assert "metadata_condition" not in params


def test_query_builder_tags_and_query():
    import json
    qb = QueryBuilder()
    params = qb.build_list_params(tags=["r1"], query="bootstrap")
    assert params["keywords"] == "bootstrap"
    mc = json.loads(params["metadata_condition"])
    assert mc["conditions"][0]["value"] == "r1"
