"""Tests for the dataset catalog."""

from __future__ import annotations

from orxaq.fabric.catalog import DataCatalog
from orxaq.fabric.connectors import DictConnector


class TestDataCatalog:
    def setup_method(self):
        self.catalog = DataCatalog()
        self.ds = DictConnector().read([{"a": 1}, {"a": 2}])

    def test_register_returns_id(self):
        ds_id = self.catalog.register(self.ds, tags=["credit"])
        assert isinstance(ds_id, str)
        assert len(ds_id) > 0

    def test_get_returns_dataset(self):
        ds_id = self.catalog.register(self.ds)
        retrieved = self.catalog.get(ds_id)
        assert retrieved is not None
        assert retrieved.row_count == 2

    def test_get_nonexistent_returns_none(self):
        assert self.catalog.get("nonexistent") is None

    def test_search_by_tag(self):
        self.catalog.register(self.ds, tags=["credit", "raw"])
        self.catalog.register(self.ds, tags=["macro"])
        results = self.catalog.search("credit")
        assert len(results) == 1

    def test_search_no_match(self):
        self.catalog.register(self.ds, tags=["credit"])
        assert self.catalog.search("nonexistent") == []

    def test_list_all(self):
        self.catalog.register(self.ds)
        self.catalog.register(self.ds)
        assert len(self.catalog.list_all()) == 2

    def test_summary(self):
        self.catalog.register(self.ds, tags=["credit"])
        self.catalog.register(self.ds, tags=["macro"])
        summary = self.catalog.summary()
        assert summary["total_datasets"] == 2
        assert summary["total_rows"] == 4
        assert "credit" in summary["tags"]
        assert "macro" in summary["tags"]

    def test_empty_catalog(self):
        summary = self.catalog.summary()
        assert summary["total_datasets"] == 0
        assert summary["total_rows"] == 0
