from fastpluggy.core.metrics import MetricEntry, MetricsProvider, MetricsResult


class TestMetricEntry:
    def test_required_fields(self):
        entry = MetricEntry(name="cpu_usage", value=42.5, type="gauge")
        assert entry.name == "cpu_usage"
        assert entry.value == 42.5
        assert entry.type == "gauge"
        assert entry.help == ""
        assert entry.labels == {}

    def test_optional_fields(self):
        entry = MetricEntry(
            name="request_count",
            value=100,
            type="counter",
            help="Total HTTP requests",
            labels={"method": "GET", "status": "200"},
        )
        assert entry.help == "Total HTTP requests"
        assert entry.labels == {"method": "GET", "status": "200"}

    def test_labels_not_shared_between_instances(self):
        a = MetricEntry(name="a", value=1, type="gauge")
        b = MetricEntry(name="b", value=2, type="gauge")
        a.labels["foo"] = "bar"
        assert b.labels == {}


class TestMetricsResult:
    def test_empty_by_default(self):
        result = MetricsResult()
        assert result.gauges == []
        assert result.counters == []
        assert result.histograms == []

    def test_all_entries_flat_list(self):
        g = MetricEntry(name="g", value=1, type="gauge")
        c = MetricEntry(name="c", value=2, type="counter")
        h = MetricEntry(name="h", value=3, type="histogram")
        result = MetricsResult(gauges=[g], counters=[c], histograms=[h])
        assert result.all_entries == [g, c, h]

    def test_all_entries_empty(self):
        assert MetricsResult().all_entries == []

    def test_lists_not_shared_between_instances(self):
        a = MetricsResult()
        b = MetricsResult()
        a.gauges.append(MetricEntry(name="x", value=1, type="gauge"))
        assert b.gauges == []


class TestMetricsProvider:
    def test_protocol_isinstance_check(self):
        class MyProvider:
            def get_metrics(self) -> MetricsResult:
                return MetricsResult()

        assert isinstance(MyProvider(), MetricsProvider)

    def test_non_provider_fails_check(self):
        class NotAProvider:
            pass

        assert not isinstance(NotAProvider(), MetricsProvider)

    def test_provider_returns_data(self):
        class BrokerMetrics:
            def get_metrics(self) -> MetricsResult:
                return MetricsResult(
                    gauges=[MetricEntry(name="queue_depth", value=5, type="gauge")],
                )

        provider = BrokerMetrics()
        result = provider.get_metrics()
        assert len(result.gauges) == 1
        assert result.gauges[0].name == "queue_depth"


class TestPublicImport:
    def test_import_from_core_package(self):
        from fastpluggy.core import MetricEntry, MetricsProvider, MetricsResult

        assert MetricEntry is not None
        assert MetricsResult is not None
        assert MetricsProvider is not None
