///|
#derive(Debug, Eq)
pub(all) struct Evaluation {
  questions : Int32
  auto_questions : Int32
  auto_errors : Int32
  review_questions : Int32
  reviewed_correct : Int32
  reviewed_wrong : Int32
  unresolved : Int32
}

///|
pub fn Evaluation::empty() -> Evaluation {
  {
    questions: 0,
    auto_questions: 0,
    auto_errors: 0,
    review_questions: 0,
    reviewed_correct: 0,
    reviewed_wrong: 0,
    unresolved: 0,
  }
}

///|
pub fn Evaluation::combine(self : Evaluation, other : Evaluation) -> Evaluation {
  {
    questions: self.questions + other.questions,
    auto_questions: self.auto_questions + other.auto_questions,
    auto_errors: self.auto_errors + other.auto_errors,
    review_questions: self.review_questions + other.review_questions,
    reviewed_correct: self.reviewed_correct + other.reviewed_correct,
    reviewed_wrong: self.reviewed_wrong + other.reviewed_wrong,
    unresolved: self.unresolved + other.unresolved,
  }
}

///|
pub fn evaluate(
  sheet : Sheet,
  truth : Array[Array[Int32]],
) -> Evaluation raise MarkError {
  if truth.length() != sheet.scan.readings.length() {
    raise Invalid("truth must provide one option list for every question")
  }
  let mut auto_questions = 0
  let mut auto_errors = 0
  let mut review_questions = 0
  let mut reviewed_correct = 0
  let mut reviewed_wrong = 0
  let mut unresolved = 0
  for index in 0.. Json {
  if denominator == 0 {
    Json::null()
  } else {
    Json::number(numerator.to_double() / denominator.to_double())
  }
}

///|
pub fn Evaluation::json(self : Evaluation) -> Json {
  Json::object(
    Map([
      ("questions", jint(self.questions)),
      ("auto_questions", jint(self.auto_questions)),
      ("auto_errors", jint(self.auto_errors)),
      ("auto_error_rate", fraction(self.auto_errors, self.auto_questions)),
      ("review_questions", jint(self.review_questions)),
      ("review_rate", fraction(self.review_questions, self.questions)),
      ("reviewed_correct", jint(self.reviewed_correct)),
      ("reviewed_wrong", jint(self.reviewed_wrong)),
      ("unresolved", jint(self.unresolved)),
    ]),
  )
}

///|
pub fn truth_from_json(value : Json) -> Array[Array[Int32]] raise MarkError {
  let answers = json_array(value, "ground truth")
  if answers.length() > 60 {
    raise Invalid("ground truth exceeds 60 questions")
  }
  let outputs : Array[Array[Int32]] = []
  for answer in answers {
    outputs.push(json_ints(answer, "truth options"))
  }
  outputs
}