///|
pub(all) struct RowSummary {
  rows : Int32
  error_count : Int64
  errors : Array[Diagnostic]
}

///|
struct ErrorSample {
  limit : Int32
  mut latest : Diagnostic?
  mut count : Int64
  errors : Array[Diagnostic]
}

///|
fn ErrorSample::record(self : ErrorSample, error : Diagnostic) -> Unit {
  self.count += 1
  self.latest = Some(error)
  if self.errors.length() < self.limit {
    self.errors.push(error)
  }
}

///|
/// Ordinary validation needs membership only. Impact samples can also identify the
/// first physical record for the same normalized value, without indexing it twice.
priv enum SeenIndex[K] {
  Membership(@hashset.HashSet[K])
  Located(Map[K, (Int32, Int32)?])
}

///|
fn[K : Eq + Hash] SeenIndex::new(located : Bool) -> SeenIndex[K] {
  if located {
    Located({})
  } else {
    Membership(@hashset.HashSet([]))
  }
}

///|
fn[K : Eq + Hash] SeenIndex::observe(
  self : SeenIndex[K],
  key : K,
  lines : (Int32, Int32)?,
) -> (Bool, (Int32, Int32)?) {
  match self {
    Membership(seen) => (!seen.add_and_check(key), None)
    Located(seen) =>
      match seen.get(key) {
        Some(first) => (true, first)
        None => {
          seen[key] = lines
          (false, None)
        }
      }
  }
}

///|
pub struct RowValidator {
  priv name : String
  priv fields : Array[Field]
  priv header : Array[String]
  priv row_rules : Array[BoundRowRule]
  priv missing : Array[String]
  priv positions : Array[Int32]
  priv unique_seen : Array[SeenIndex[Cell]?]
  priv keys : SeenIndex[Array[Cell]]
  priv mut unique_origin : (Int32, Int32)?
  priv mut key_origin : (Int32, Int32)?
  priv field_errors : ErrorSample
  priv key_errors : ErrorSample
  priv mut rows : Int32
  priv mut json_mode : JsonRowMode?
}

///|
/// Accepts decoded records. All foreign keys require batch validation.
pub fn RowValidator::new(
  name : String,
  schema : TableSchema,
  header : Array[String],
  max_errors~ : Int32 = 100,
) -> Result[RowValidator, Array[Diagnostic]] {
  RowValidator::create(name, schema, header, max_errors, false)
}

///|
fn RowValidator::create(
  name : String,
  schema : TableSchema,
  header : Array[String],
  max_errors : Int32,
  locate_duplicates : Bool,
) -> Result[RowValidator, Array[Diagnostic]] {
  let errors = []
  check_field_definitions(name, schema.fields, errors)
  let row_rules = bind_row_rules(name, schema.fields, schema.row_rules, errors)
  if header != schema.fields.map(f => f.name) {
    errors.push(
      issue("header", name, 0, "", "Header must match schema fields in order"),
    )
  }
  if max_errors < 0 {
    errors.push(issue("input", name, 0, "", "max_errors must be nonnegative"))
  }
  if !schema.foreign_keys.is_empty() {
    errors.push(
      issue(
        "foreign-key-scope", name, 0, "", "Row validation requires batch mode for foreign keys",
      ),
    )
  }
  let positions = if schema.primary_key.is_empty() {
    []
  } else {
    match
      field_positions(
        { name, fields: schema.fields, rows: [], },
        schema.primary_key,
      ) {
      Ok(indexes) => indexes
      Err(message) => {
        errors.push(issue("primary-key-schema", name, 0, "", message))
        []
      }
    }
  }
  if !errors.is_empty() {
    return Err(errors)
  }
  let fields = schema.fields.map(f => {
    ..f,
    enum_values: f.enum_values.map(v => v.copy()),
    boolean_tokens: f.boolean_tokens.map(t => {
      truthy: t.truthy.copy(),
      falsy: t.falsy.copy(),
    }),
  })
  Ok({
    name,
    fields,
    header: header.copy(),
    row_rules,
    missing: schema.missing_values.copy(),
    positions,
    unique_seen: fields.map(f => {
      if f.unique {
        Some(SeenIndex::new(locate_duplicates))
      } else {
        None
      }
    }),
    keys: SeenIndex::new(locate_duplicates),
    unique_origin: None,
    key_origin: None,
    field_errors: { limit: max_errors, latest: None, count: 0, errors: [], },
    key_errors: { limit: max_errors, latest: None, count: 0, errors: [], },
    rows: 0,
    json_mode: None,
  })
}

///|
pub fn RowValidator::feed(self : RowValidator, row : Array[String]) -> Unit {
  self.feed_at(row, None)
}

///|
fn RowValidator::feed_at(
  self : RowValidator,
  row : Array[String],
  lines : (Int32, Int32)?,
) -> Unit {
  self.rows += 1
  self.consume(lines, row.length(), "Row width differs from schema", (i, f) => {
    cast_field_text(row[i], f, self.missing)
  })
}

///|
pub fn RowValidator::feed_json(self : RowValidator, row : Json) -> Unit {
  self.rows += 1
  let mode = match self.json_mode {
    Some(mode) => mode
    None => {
      let mode : JsonRowMode = match row {
        Array(_) => ArrayRows
        Object(_) => ObjectRows
        _ => {
          self.field_errors.record(
            issue(
              "json-rows",
              self.name,
              self.rows,
              "",
              "Expected a JSON array or object row",
            ),
          )
          return
        }
      }
      self.json_mode = Some(mode)
      mode
    }
  }
  let errors = []
  let values = json_row_values(
    row,
    mode,
    self.header,
    self.name,
    self.rows,
    errors,
  )
  for e in errors {
    self.field_errors.record(e)
  }
  if values is Some(v) {
    self.consume(None, v.length(), "JSON row width differs from schema", (i, f) => {
      cast_field_json(v[i], f, self.missing)
    })
  }
}

///|
fn RowValidator::consume(
  self : RowValidator,
  lines : (Int32, Int32)?,
  width : Int32,
  width_message : String,
  cast : (Int32, Field) -> Result[Cell, String],
) -> Unit {
  self.unique_origin = None
  self.key_origin = None
  if width != self.fields.length() {
    self.field_errors.record(
      issue("row-width", self.name, self.rows, "", width_message),
    )
    return
  }
  let cells = []
  let errors = []
  for i, f in self.fields {
    match cast(i, f) {
      Err(message) => {
        errors.push(issue("type", self.name, self.rows, f.name, message))
        cells.push(Missing)
      }
      Ok(value) => {
        let cell = match value {
          NumberValue(n) if n == 0.0 => NumberValue(0.0)
          _ => value
        }
        let duplicate = if self.unique_seen[i] is Some(seen) && cell != Missing {
          let (repeated, origin) = seen.observe(cell, lines)
          if repeated {
            self.unique_origin = origin
          }
          repeated
        } else {
          false
        }
        check_logical_cell(self.name, f, cell, self.rows, duplicate, errors)
        cells.push(cell)
      }
    }
  }
  check_row_rules(self.name, self.rows, cells, self.row_rules, errors)
  for error in errors {
    self.field_errors.record(error)
  }
  // Match batch semantics: key diagnostics apply only if every row passed field validation.
  if self.field_errors.count == 0 && !self.positions.is_empty() {
    let key = self.positions.map(i => cells[i])
    if key.contains(Missing) {
      self.key_errors.record(
        issue(
          "primary-key-null",
          self.name,
          self.rows,
          "",
          "Primary key contains a missing value",
        ),
      )
    } else {
      let (duplicate, origin) = self.keys.observe(key, lines)
      if !duplicate {
        return
      }
      self.key_origin = origin
      self.key_errors.record(
        issue(
          "primary-key-duplicate",
          self.name,
          self.rows,
          self.positions.map(i => self.header[i]).join(", "),
          "Duplicate primary key",
        ),
      )
    }
  }
}

///|
/// A detached snapshot. Repeated calls do not consume or reset validation state.
pub fn RowValidator::finish(self : RowValidator) -> RowSummary {
  let sample = if self.field_errors.count > 0 {
    self.field_errors
  } else {
    self.key_errors
  }
  { rows: self.rows, error_count: sample.count, errors: sample.errors.copy(), }
}

///|
/// True when a retained diagnostic refers to the most recently fed row.
/// Hosts can retain metadata for sampled rows without indexing the whole stream.
pub fn RowValidator::current_row_sampled(self : RowValidator) -> Bool {
  (self.field_errors.errors.last() is Some(e) && e.row == self.rows) ||
  (self.key_errors.errors.last() is Some(e) && e.row == self.rows)
}