class EvaluationResult:
    """
    Wrapper for evaluation metrics.

    - Renders as an HTML table in Jupyter environments.
    - Prints cleanly in terminals / scripts.
    - Supports dict-like access for backward compatibility.
    """

    def __init__(self, metrics: dict, model_type: str = ""):
        self._metrics = dict(metrics)
        self._model_type = model_type

    # ── Programmatic access ─────────────────────────────

    def to_dict(self) -> dict:
        """Return the raw metrics dictionary."""
        return dict(self._metrics)

    # ── Dict-like interface ─────────────────────────────

    def __getitem__(self, key):
        return self._metrics[key]

    def __contains__(self, key):
        return key in self._metrics

    def __iter__(self):
        return iter(self._metrics)

    def __len__(self):
        return len(self._metrics)

    def keys(self):
        return self._metrics.keys()

    def values(self):
        return self._metrics.values()

    def items(self):
        return self._metrics.items()

    def get(self, key, default=None):
        return self._metrics.get(key, default)

    # ── Terminal display ────────────────────────────────

    def __repr__(self):
        title = self._model_type.title() if self._model_type else "Evaluation"
        max_key = max(len(k) for k in self._metrics)
        lines = [f"{title} Results", "─" * (max_key + 14)]
        for key, value in self._metrics.items():
            lines.append(f"  {key:<{max_key}}  │  {value:.6f}")
        return "\n".join(lines)

    # ── Jupyter display ─────────────────────────────────

    def _repr_html_(self):
        title = self._model_type.title() if self._model_type else "Evaluation"

        rows = ""
        for key, value in self._metrics.items():
            rows += (
                f"<tr>"
                f'<td style="padding:6px 14px;font-weight:500">{key}</td>'
                f'<td style="padding:6px 14px;font-family:monospace">{value:.6f}</td>'
                f"</tr>"
            )

        return (
            f'<div style="margin:8px 0">'
            f'<table style="border-collapse:collapse;border:1px solid #ddd;'
            f'border-radius:6px;overflow:hidden;font-size:14px">'
            f"<thead>"
            f'<tr style="background:#f5f5f5">'
            f'<th style="padding:8px 14px;text-align:left" colspan="2">'
            f"{title} Results</th>"
            f"</tr>"
            f'<tr style="background:#fafafa">'
            f'<th style="padding:6px 14px;text-align:left">Metric</th>'
            f'<th style="padding:6px 14px;text-align:left">Value</th>'
            f"</tr>"
            f"</thead>"
            f"<tbody>{rows}</tbody>"
            f"</table>"
            f"</div>"
        )
