from __future__ import annotations

from dataclasses import dataclass, field
from pathlib import Path
from typing import Any


@dataclass
class Node:
    id: str
    kind: str
    impl: str
    attrs: dict[str, Any]
    pos_mm: tuple[float, float] | None  # (x_mm, y_mm); None for abstract nodes
    label: str = ""


@dataclass
class Edge:
    src: str                  # node id
    dst: str                  # node id
    distance_mm: float        # physical wire delay distance (ns = distance_mm * ns_per_mm)
    routing_weight_mm: float | None = None  # Dijkstra cost; None → use distance_mm
    bw_gbs: float | None = None
    n_connections: int | None = None  # multi-connection links; single request uses 1 connection
    kind: str = "link"


@dataclass
class ViewGraph:
    name: str                 # "system" | "sip" | "cube" | "pe"
    nodes: dict[str, Node]
    edges: list[Edge]
    width_mm: float
    height_mm: float


@dataclass
class TopologyGraph:
    spec: dict[str, Any]

    # Full instantiated flat graph (used by sim_engine)
    nodes: dict[str, Node] = field(default_factory=dict)
    edges: list[Edge] = field(default_factory=list)

    # Representative view projections (used by visualizer)
    system_view: ViewGraph | None = None
    sip_view:    ViewGraph | None = None
    cube_view:   ViewGraph | None = None
    pe_view:     ViewGraph | None = None


@dataclass(frozen=True)
class TopologyHandle:
    path: Path
    topology_obj: TopologyGraph | None  # None until _compile_graph is implemented
