///|
fn render_input_line(value : InputValue) -> String {
  let mut line = "- **\{value.name}**: \{value.value}\{unit_suffix(value.unit)}"
  line = append_if_some(line, "  ", value.note)
  line
}

///|
fn render_result_line(value : ResultValue) -> String {
  let mut line = "- **\{value.name}**: \{value.value}\{unit_suffix(value.unit)} (`\{result_status_name(value.status)}`)"
  line = append_if_some(line, "  ", value.note)
  line
}

///|
fn render_source_line(source : SourceRef) -> String {
  let mut line = "- **\{source.label}** (`\{source_kind_name(source.kind)}`)"
  line = append_if_some(line, " - ", source.url)
  line = append_if_some(line, " | license: ", source.license)
  line = append_if_some(line, " | note: ", source.note)
  line
}

///|
pub fn ChemReport::to_markdown(self : ChemReport) -> String {
  let sections : Array[String] = []
  sections.push("# \{self.metadata.title}")
  sections.push("")
  sections.push(self.summary)
  sections.push("")
  sections.push("## Metadata")
  sections.push("- Report ID: `\{self.metadata.report_id}`")
  sections.push("- Process unit: `\{self.metadata.process_unit}`")
  sections.push("- Scenario: `\{self.metadata.scenario}`")
  sections.push("- Generated at: `\{self.metadata.generated_at}`")
  sections.push("- Tool version: `\{self.metadata.tool_version}`")
  if self.tags.length() > 0 {
    sections.push("- Tags: `\{self.tags.join("`, `")}`")
  }
  sections.push("")
  sections.push("## Assumptions")
  for item in self.assumptions {
    sections.push("- \{item.statement}  Impact: \{item.impact}")
  }
  sections.push("")
  sections.push("## Inputs")
  for item in self.inputs {
    sections.push(render_input_line(item))
  }
  sections.push("")
  sections.push("## Formulas")
  for item in self.formulas {
    sections.push(
      "- **\{item.name}**: `\{item.expression}`  \{item.explanation}",
    )
  }
  sections.push("")
  sections.push("## Results")
  for item in self.results {
    sections.push(render_result_line(item))
  }
  sections.push("")
  sections.push("## Warnings")
  if self.warnings.length() == 0 {
    sections.push("- None")
  } else {
    for item in self.warnings {
      sections.push(
        "- **\{item.code}** (`\{severity_name(item.severity)}`): \{item.summary}  Action: \{item.action}",
      )
    }
  }
  sections.push("")
  sections.push("## Sources")
  for item in self.sources {
    sections.push(render_source_line(item))
  }
  sections.push("")
  sections.push("## Narrative Sections")
  for item in self.sections {
    sections.push("### \{item.title}")
    sections.push("_kind: \{section_kind_name(item.kind)}_")
    sections.push(item.body)
    sections.push("")
  }
  joined_lines(sections)
}