///|
fn xml_escape(s : String) -> String {
  let sb = StringBuilder()
  for ch in s {
    match ch {
      '&' => sb.write_string("&")
      '<' => sb.write_string("<")
      '>' => sb.write_string(">")
      '"' => sb.write_string(""")
      '\'' => sb.write_string("'")
      _ => sb.write_char(ch)
    }
  }
  sb.to_string()
}

///|
fn truncate_str(s : String, max_len : Int32) -> String {
  let chars = s.to_array()
  if chars.length() <= max_len {
    return s
  }
  let sb = StringBuilder()
  let lim = if max_len > 3 { max_len - 3 } else { max_len }
  for i = 0; i < lim; i = i + 1 {
    sb.write_char(chars[i])
  }
  sb.write_string("...")
  sb.to_string()
}

///|
fn status_palette(status : CheckStatus) -> (String, String, String) {
  match status {
    Ok => ("PASS", "#10b981", "#064e3b")
    InvariantViolation(_) => ("INVARIANT VIOLATION", "#f43f5e", "#4c0519")
    Deadlock => ("DEADLOCK", "#f59e0b", "#451a03")
    LivenessViolation(_) => ("LIVENESS VIOLATION", "#a855f7", "#3b0764")
    StateLimitReached(_) => ("STATE LIMIT", "#38bdf8", "#0c4a6e")
  }
}

///|
/// Render a native SVG counterexample sequence timeline with per-step variable diffs
/// (or a verification summary badge if no counterexample exists).
pub fn render_trace_svg(result : CheckResult) -> String {
  let width = 880
  let pad = 24
  let header_h = 84
  let (badge, accent, badge_bg) = status_palette(result.status)
  let sb = StringBuilder()

  if result.trace.is_empty() {
    let height = 230
    sb.write_string(
      "\n",
    )
    sb.write_string(
      "\n",
    )
    sb.write_string(
      "\n",
    )
    sb.write_string(
      "\n",
    )
    sb.write_string(
      "\{badge}\n",
    )
    sb.write_string(
      "\{xml_escape(result.module_name)} — Verified Safe\n",
    )
    sb.write_string(
      "\{xml_escape(result.status_text)}\n",
    )
    sb.write_string(
      "distinct_states=\{result.distinct_states}   diameter=\{result.diameter}   transitions=\{result.transitions}   generated=\{result.states_generated}\n",
    )
    let invs = if result.invariants_checked.is_empty() {
      "(none)"
    } else {
      result.invariants_checked.join(", ")
    }
    let props = if result.properties_checked.is_empty() {
      "(none)"
    } else {
      result.properties_checked.join(", ")
    }
    sb.write_string(
      "invariants: \{xml_escape(truncate_str(invs, 78))}\n",
    )
    sb.write_string(
      "properties: \{xml_escape(truncate_str(props, 78))}\n",
    )
    sb.write_string("\n")
    return sb.to_string()
  }

  // Pre-compute heights of each step card
  let card_x = 32
  let card_w = if result.lasso_loop_start is Some(_) { 740 } else { 816 }
  let arrow_gap = 42
  let card_ys : Array[Int32] = []
  let card_hs : Array[Int32] = []
  let mut cur_y = header_h + 18
  for i = 0; i < result.trace.length(); i = i + 1 {
    let step = result.trace[i]
    let rows = if i == 0 {
      step.bindings.length()
    } else if step.diffs.is_empty() {
      1
    } else {
      step.diffs.length()
    }
    let h = 44 + rows * 20 + 12
    card_ys.push(cur_y)
    card_hs.push(h)
    cur_y = cur_y + h + arrow_gap
  }
  let total_h = cur_y + 12

  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\n  \n  \n\n",
  )
  sb.write_string(
    "\n",
  )

  // Header bar
  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\{badge}\n",
  )
  sb.write_string(
    "\{xml_escape(result.module_name)} — \{xml_escape(truncate_str(result.status_text, 56))}\n",
  )
  sb.write_string(
    "trace=\{result.trace.length()} states (\{result.trace.length() - 1} steps)   distinct=\{result.distinct_states}   diameter=\{result.diameter}   transitions=\{result.transitions}\n",
  )

  // Render cards and arrows
  let n_steps = result.trace.length()
  for i = 0; i < n_steps; i = i + 1 {
    let step = result.trace[i]
    let y = card_ys[i]
    let h = card_hs[i]
    let is_last = i + 1 == n_steps
    let is_loop_anchor = match result.lasso_loop_start {
      Some(ls) => ls == step.step
      None => false
    }
    let stroke = if is_last {
      accent
    } else if i == 0 {
      "#38bdf8"
    } else if is_loop_anchor {
      "#a855f7"
    } else {
      "#334155"
    }
    let fill = if is_last { "#1f1220" } else { "#1e293b" }
    let stroke_w = if is_last || i == 0 || is_loop_anchor { "1.8" } else { "1" }

    // Transition arrow from previous card
    if i > 0 {
      let prev_bottom = card_ys[i - 1] + card_hs[i - 1]
      let mid_x = card_x + 36
      let mid_y = prev_bottom + arrow_gap / 2 + 4
      sb.write_string(
        "\n",
      )
      sb.write_string(
        "\n",
      )
      sb.write_string(
        "action: \{xml_escape(truncate_str(step.action, 40))}\n",
      )
    }

    sb.write_string(
      "\n",
    )
    let tag_text = if i == 0 {
      "INIT"
    } else if is_last {
      badge
    } else if is_loop_anchor {
      "LOOP START"
    } else {
      "STEP \{i}"
    }
    let tag_color = if i == 0 {
      "#38bdf8"
    } else if is_last {
      accent
    } else if is_loop_anchor {
      "#c084fc"
    } else {
      "#94a3b8"
    }
    sb.write_string(
      "State \{step.step}\n",
    )
    sb.write_string(
      "[\{tag_text}]\n",
    )
    sb.write_string(
      "id=#\{step.state_id}\n",
    )
    sb.write_string(
      "\n",
    )

    let mut row_y = y + 54
    if i == 0 {
      for b in step.bindings {
        let line = "\{b.name} = \{b.value}"
        sb.write_string(
          "\{xml_escape(truncate_str(line, 92))}\n",
        )
        row_y += 20
      }
    } else if step.diffs.is_empty() {
      sb.write_string(
        "(no variable changes — stutter step)\n",
      )
    } else {
      for d in step.diffs {
        let diff_line = "\{d.name}: \{d.before}  ->  \{d.after}"
        sb.write_string(
          "Δ \{xml_escape(truncate_str(diff_line, 90))}\n",
        )
        row_y += 20
      }
    }
  }

  // Draw lasso loop-back arc if liveness cycle
  match result.lasso_loop_start {
    Some(loop_step) if loop_step >= 1 && loop_step <= n_steps => {
      let from_idx = n_steps - 1
      let to_idx = loop_step - 1
      let x0 = card_x + card_w
      let y0 = card_ys[from_idx] + card_hs[from_idx] / 2
      let y1 = card_ys[to_idx] + card_hs[to_idx] / 2
      let rail_x = width - 38
      sb.write_string(
        "\n",
      )
      let label_y = (y0 + y1) / 2
      sb.write_string(
        "CYCLE\n",
      )
    }
    _ => ()
  }

  sb.write_string("\n")
  sb.to_string()
}

///|
/// Render a layered BFS state-space graph SVG highlighting initial states,
/// counterexample paths, and offending/deadlock states with hover snapshots.
pub fn render_state_graph_svg(result : CheckResult) -> String {
  let nodes = result.graph_nodes
  let edges = result.graph_edges
  if nodes.is_empty() {
    return "No states to display\n"
  }

  let mut max_depth = 0
  for n in nodes {
    if n.depth > max_depth {
      max_depth = n.depth
    }
  }
  let layers : Array[Array[GraphNode]] = []
  for _d = 0; _d <= max_depth; _d = _d + 1 {
    layers.push([])
  }
  for n in nodes {
    layers[n.depth].push(n)
  }

  let mut max_layer_size = 1
  for layer in layers {
    if layer.length() > max_layer_size {
      max_layer_size = layer.length()
    }
  }

  let col_w = 150
  let row_h = 68
  let left_pad = 64
  let top_pad = 72
  let bottom_pad = 54
  let width = {
    let raw = left_pad * 2 + (max_depth + 1) * col_w
    if raw < 760 {
      760
    } else {
      raw
    }
  }
  let height = {
    let raw = top_pad + bottom_pad + max_layer_size * row_h
    if raw < 320 {
      320
    } else {
      raw
    }
  }

  let pos_x : Map[Int32, Int32] = Map([])
  let pos_y : Map[Int32, Int32] = Map([])
  let usable_h = height - top_pad - bottom_pad
  for d = 0; d <= max_depth; d = d + 1 {
    let layer = layers[d]
    let x = left_pad + d * col_w + col_w / 2
    let cnt = layer.length()
    for i = 0; i < cnt; i = i + 1 {
      let y = top_pad + usable_h * (i + 1) / (cnt + 1)
      pos_x.set(layer[i].id, x)
      pos_y.set(layer[i].id, y)
    }
  }

  let sb = StringBuilder()
  sb.write_string(
    "\n",
  )
  sb.write_string(
    "\n  \n  \n\n",
  )
  sb.write_string(
    "\n",
  )
  let cap_note = if nodes.length() < result.distinct_states {
    "showing \{nodes.length()} / \{result.distinct_states} states (full trace preserved)"
  } else {
    "showing all \{nodes.length()} distinct states"
  }
  sb.write_string(
    "\{xml_escape(result.module_name)} State-Space Graph (BFS Layers 0..\{max_depth})\n",
  )
  sb.write_string(
    "\{cap_note}   diameter=\{result.diameter}   hover nodes for full state snapshot\n",
  )

  // Draw non-trace edges first, then trace edges on top
  for pass = 0; pass < 2; pass = pass + 1 {
    let want_trace = pass == 1
    for e in edges {
      if e.is_on_trace != want_trace {
        continue
      }
      match
        (pos_x.get(e.from), pos_y.get(e.from), pos_x.get(e.to), pos_y.get(e.to)) {
        (Some(x1), Some(y1), Some(x2), Some(y2)) => {
          let stroke = if want_trace { "#f43f5e" } else { "#334155" }
          let sw = if want_trace { "2.4" } else { "1.1" }
          let marker = if want_trace {
            "url(#g-arrow-trace)"
          } else {
            "url(#g-arrow)"
          }
          if e.from == e.to {
            sb.write_string(
              "\{xml_escape(e.action)}\n",
            )
          } else {
            let cx = (x1 + x2) / 2
            let cy = if x2 <= x1 { (y1 + y2) / 2 - 28 } else { (y1 + y2) / 2 }
            let dx = x2 - x1
            let dy = y2 - y1
            let ex = if dx > 20 {
              x2 - 20
            } else if dx < -20 {
              x2 + 20
            } else {
              x2
            }
            let ey = if dy > 18 {
              y2 - 14
            } else if dy < -18 {
              y2 + 14
            } else {
              y2
            }
            sb.write_string(
              "\{xml_escape(e.action)}\n",
            )
          }
        }
        _ => ()
      }
    }
  }

  // Draw nodes
  for n in nodes {
    match (pos_x.get(n.id), pos_y.get(n.id)) {
      (Some(x), Some(y)) => {
        let (fill, stroke) = if n.is_violation {
          ("#9f1239", "#fda4af")
        } else if n.is_init {
          ("#0369a1", "#7dd3fc")
        } else if n.is_on_trace {
          ("#881337", "#fb7185")
        } else {
          ("#1e293b", "#64748b")
        }
        if n.is_violation {
          sb.write_string(
            "\n",
          )
        }
        sb.write_string("\n")
        sb.write_string(
          "  State #\{n.id} (depth \{n.depth})\n\{xml_escape(n.detail)}\n",
        )
        sb.write_string(
          "  \n",
        )
        sb.write_string(
          "  s\{n.id}\n",
        )
        sb.write_string(
          "  \{xml_escape(truncate_str(n.label, 22))}\n",
        )
        sb.write_string("\n")
      }
      _ => ()
    }
  }

  // Legend
  let ly = height - 18
  sb.write_string(
    "Init\n",
  )
  sb.write_string(
    "Reachable\n",
  )
  sb.write_string(
    "Counterexample Path\n",
  )
  sb.write_string(
    "Violation / Deadlock\n",
  )
  sb.write_string("\n")
  sb.to_string()
}