///|
pub struct ChapterCount {
  chapter_id : String
  total : Int
  valid : Int
  unknown : Int
  invalid : Int
} derive(Debug, Eq, ToJson)

///|
pub struct BatchSummary {
  total : Int
  valid : Int
  unknown : Int
  invalid : Int
  chapters : Array[ChapterCount]
} derive(Debug, Eq, ToJson)

///|
pub fn summarize(reports : Array[ValidationReport]) -> BatchSummary {
  let mut valid = 0
  let mut unknown = 0
  let mut invalid = 0
  for report in reports {
    match report.status {
      Valid => valid += 1
      Unknown => unknown += 1
      Invalid => invalid += 1
    }
  }
  let chapters = []
  for chapter in icd10_chapters() {
    let mut total = 0
    let mut chapter_valid = 0
    let mut chapter_unknown = 0
    let mut chapter_invalid = 0
    for report in reports {
      match report.chapter {
        Some(found) if found.id == chapter.id => {
          total += 1
          match report.status {
            Valid => chapter_valid += 1
            Unknown => chapter_unknown += 1
            Invalid => chapter_invalid += 1
          }
        }
        _ => ()
      }
    }
    if total > 0 {
      chapters.push({
        chapter_id: chapter.id,
        total,
        valid: chapter_valid,
        unknown: chapter_unknown,
        invalid: chapter_invalid,
      })
    }
  }
  { total: reports.length(), valid, unknown, invalid, chapters }
}