from promrs import CollectorRegistry, Counter, Gauge, Histogram, generate_latest


def test_counter_namespace() -> None:
    registry = CollectorRegistry()
    c = Counter("requests", "Total requests", ["method"], namespace="http", registry=registry)
    c.labels(method="GET").inc()
    output = generate_latest(registry).decode()
    assert "# TYPE http_requests counter" in output
    assert 'http_requests_total{method="GET"} 1.0' in output


def test_counter_namespace_subsystem() -> None:
    registry = CollectorRegistry()
    c = Counter("requests", "Total requests", ["method"], namespace="http", subsystem="server", registry=registry)
    c.labels(method="GET").inc()
    output = generate_latest(registry).decode()
    assert "# TYPE http_server_requests counter" in output
    assert 'http_server_requests_total{method="GET"} 1.0' in output


def test_counter_subsystem_only() -> None:
    registry = CollectorRegistry()
    c = Counter("requests", "Total requests", ["method"], subsystem="server", registry=registry)
    c.labels(method="GET").inc()
    output = generate_latest(registry).decode()
    assert "# TYPE server_requests counter" in output


def test_gauge_with_unit() -> None:
    registry = CollectorRegistry()
    g = Gauge("temperature", "Current temperature", ["location"], unit="celsius", registry=registry)
    g.labels(location="office").set(22.5)
    output = generate_latest(registry).decode()
    assert "temperature_celsius" in output
    assert "22.5" in output


def test_histogram_namespace_and_unit() -> None:
    registry = CollectorRegistry()
    h = Histogram(
        "duration",
        "Request duration",
        ["method"],
        namespace="http",
        unit="seconds",
        registry=registry,
        buckets=[0.1, 0.5, 1.0],
    )
    h.labels(method="GET").observe(0.05)
    output = generate_latest(registry).decode()
    assert "http_duration_seconds" in output


def test_all_naming_parts() -> None:
    registry = CollectorRegistry()
    c = Counter(
        "requests", "Total requests", ["method"], namespace="myapp", subsystem="http", unit="total", registry=registry
    )
    c.labels(method="GET").inc()
    output = generate_latest(registry).decode()
    assert "myapp_http_requests_total" in output


def test_no_naming_parts() -> None:
    registry = CollectorRegistry()
    c = Counter("simple_counter", "Simple counter", ["k"], registry=registry)
    c.labels(k="v").inc()
    output = generate_latest(registry).decode()
    assert "# TYPE simple_counter counter" in output
