///|
/// A structured event report assembled from processing artifacts.
pub struct EventReportDocument {
  event_id : String
  title : String
  generated_at : Double
  location_line : String
  magnitude_line : String
  quality_line : String
  processing_line : String
  sections : Array[String]
}

///|
/// Create an empty event report document.
pub fn EventReportDocument::new(
  event_id : String,
  title : String,
  generated_at : Double,
) -> EventReportDocument {
  {
    event_id,
    title,
    generated_at,
    location_line: "",
    magnitude_line: "",
    quality_line: "",
    processing_line: "",
    sections: [],
  }
}

///|
/// Add a named report section.
pub fn EventReportDocument::with_section(
  self : EventReportDocument,
  heading : String,
  body : String,
) -> EventReportDocument {
  let sections = self.sections.copy()
  sections.push("## " + heading + "\n" + body)
  {
    event_id: self.event_id,
    title: self.title,
    generated_at: self.generated_at,
    location_line: self.location_line,
    magnitude_line: self.magnitude_line,
    quality_line: self.quality_line,
    processing_line: self.processing_line,
    sections,
  }
}

///|
/// Attach a refined location headline.
pub fn EventReportDocument::with_location(
  self : EventReportDocument,
  location : RefinedLocation,
) -> EventReportDocument {
  {
    event_id: self.event_id,
    title: self.title,
    generated_at: self.generated_at,
    location_line: refined_location_text(location),
    magnitude_line: self.magnitude_line,
    quality_line: self.quality_line,
    processing_line: self.processing_line,
    sections: self.sections.copy(),
  }
}

///|
/// Attach a network magnitude headline.
pub fn EventReportDocument::with_magnitude(
  self : EventReportDocument,
  magnitude : NetworkMagnitudeSummary,
) -> EventReportDocument {
  {
    event_id: self.event_id,
    title: self.title,
    generated_at: self.generated_at,
    location_line: self.location_line,
    magnitude_line: network_magnitude_text(magnitude),
    quality_line: self.quality_line,
    processing_line: self.processing_line,
    sections: self.sections.copy(),
  }
}

///|
/// Attach network audit information.
pub fn EventReportDocument::with_quality(
  self : EventReportDocument,
  audit : NetworkAudit,
) -> EventReportDocument {
  {
    event_id: self.event_id,
    title: self.title,
    generated_at: self.generated_at,
    location_line: self.location_line,
    magnitude_line: self.magnitude_line,
    quality_line: network_audit_text(audit),
    processing_line: self.processing_line,
    sections: self.sections.copy(),
  }
}

///|
/// Attach processing graph information.
pub fn EventReportDocument::with_processing(
  self : EventReportDocument,
  result : ProcessingGraphResult,
) -> EventReportDocument {
  {
    event_id: self.event_id,
    title: self.title,
    generated_at: self.generated_at,
    location_line: self.location_line,
    magnitude_line: self.magnitude_line,
    quality_line: self.quality_line,
    processing_line: processing_graph_result_text(result),
    sections: self.sections.copy(),
  }
}

///|
/// Render a complete Markdown report.
pub fn render_event_report(document : EventReportDocument) -> String {
  let mut output = "# " +
    document.title +
    "\n\n" +
    "Event ID: `" +
    document.event_id +
    "`\n\n" +
    "Generated: " +
    document.generated_at.to_string() +
    "\n\n"
  output += "- Location: " + document.location_line + "\n"
  output += "- Magnitude: " + document.magnitude_line + "\n"
  output += "- Quality: " + document.quality_line + "\n"
  output += "- Processing: " + document.processing_line + "\n\n"
  for section in document.sections {
    output += section + "\n\n"
  }
  output
}

///|
/// Render a one-line report suitable for a network feed.
pub fn event_report_feed_line(document : EventReportDocument) -> String {
  document.event_id +
  "|" +
  document.generated_at.to_string() +
  "|" +
  document.location_line +
  "|" +
  document.magnitude_line +
  "|" +
  document.quality_line
}