from dataclasses import dataclass


@dataclass(frozen=True)
class VendorMetric:
 """Atomic vendor-state metric.
 [C: src/gui_2.py:render_vendor_state]
 """
 key:     str
 label:   str
 value:   str
 state:   str
 tooltip: str

def get_vendor_state(app) -> list[VendorMetric]:
 """Aggregate per-vendor session state for the Operations Hub Vendor State tab.
 [C: src/gui_2.py:render_vendor_state]
 """
 out: list[VendorMetric] = []
 out.append(VendorMetric(
  key     = "provider_model",
  label   = "Provider / Model",
  value   = f"{app.current_provider} / {app.current_model}",
  state   = "info",
  tooltip = "The vendor and model that will handle the next request."
 ))
 ctrl = getattr(app, "controller", None)
 tt   = getattr(ctrl, "token_tracker", None) if ctrl else None
 if tt and getattr(tt, "limit", 0):
  pct   = 100.0 * getattr(tt, "used", 0) / tt.limit
  state = "warn" if pct > 75 else "ok"
  out.append(VendorMetric(
   key     = "context_window",
   label   = "Context Window",
   value   = f"{tt.used:,} / {tt.limit:,} ({pct:.0f}%)",
   state   = state,
   tooltip = "Used vs total context window for the current session."
  ))
 else:
  out.append(VendorMetric(
   key     = "context_window", label="Context Window", value="—", state="info",
   tooltip = "No token tracker attached for the current provider."
  ))
 if tt is not None:
  hits  = getattr(tt, "cache_hits", 0)
  miss  = getattr(tt, "cache_misses", 0)
  total = hits + miss
  rate  = (100.0 * hits / total) if total else 0.0
  out.append(VendorMetric(
   key     = "cache", label="Cache Hit Rate",
   value   = f"{rate:.0f}% ({hits:,}/{total:,})",
   state   = "ok" if rate > 50 else "info",
   tooltip = "Server-side prompt cache hit rate for the current session."
  ))
 else:
  out.append(VendorMetric(
   key     = "cache", label="Cache Hit Rate", value="—", state="info",
   tooltip = "No token tracker attached for the current provider."
  ))
 quota    = (getattr(ctrl, "vendor_quota", {}) or {}) if ctrl else {}
 pct_left = quota.get("remaining_pct")
 if pct_left is None:
  out.append(VendorMetric(
   key     = "quota", label="Vendor Quota", value="—", state="info",
   tooltip = "Vendor did not report quota for the current billing period."
  ))
 else:
  out.append(VendorMetric(
   key     = "quota", label="Vendor Quota",
   value   = f"{pct_left}% remaining",
   state   = "ok" if pct_left > 25 else "warn",
   tooltip = "Approximate quota remaining for the current billing period."
  ))
 err = getattr(ctrl, "last_error", None) if ctrl else None
 out.append(VendorMetric(
  key     = "last_error", label="Last Error",
  value   = err.get("class", "none") if err else "none",
  state   = "error" if err else "ok",
  tooltip = err.get("message", "No error since session start.") if err else "No error since session start."
 ))
 return out
