///|
/// An actionable first failure. Step and action are one-based. Zero denotes
/// a plan-wide finding or one found after all actions for the step.
pub(all) struct Finding {
  code : String
  step : Int32
  action : Int32
  detail : String
}

///|
/// Only a verified plan has a recomputed total.
pub(all) struct Verification {
  cost : Int32?
  finding : Finding?
}

///|
pub extend Finding with ToJson::{to_json}

///|
pub extend Verification with ToJson::{to_json}

///|
fn failure(
  code : String,
  step : Int32,
  action : Int32,
  detail : String,
) -> Verification {
  { cost: None, finding: Some({ code, step, action, detail, }), }
}

///|
/// Independently replay the declared actions against a copy of the initial rack.
/// Accepts manually prepared plans, including feasible plans with extra actions.
pub fn verify(s : Shift, plan : Plan) -> Verification raise InputError {
  validate(s)
  if plan.steps.length() != s.jobs.length() {
    return failure("step_count", 0, 0, "expected one step per job")
  }
  let feeders : Map[String, Int32] = Map([])
  let positions = s.initial.copy()
  let rack = Array::make(s.slots + 1, -1)
  for b in s.blocked {
    rack[b] = -2
  }
  for index, feeder in s.feeders {
    feeders[feeder.id] = index
    let p = positions[index]
    if p > 0 {
      for slot in p..<(p + feeder.width) {
        rack[slot] = index
      }
    }
  }
  let mut total = 0
  for step_index, step in plan.steps {
    let row = step_index + 1
    let job = s.jobs[step_index]
    if job.id != step.job {
      return failure("job_order", row, 0, "expected job " + job.id)
    }
    if step.actions.length() > 64 {
      return failure("action_count", row, 0, "at most 64 actions per step")
    }
    let mut subtotal = 0
    for action_index, operation in step.actions {
      let column = action_index + 1
      guard feeders.get(operation.feeder) is Some(index) else {
        return failure("unknown_feeder", row, column, operation.feeder)
      }
      let feeder = s.feeders[index]
      if feeder.locked {
        return failure("locked_feeder", row, column, feeder.id)
      }
      let slot = operation.slot
      match operation.kind {
        "unload" => {
          if slot < 1 || positions[index] != slot {
            return failure("wrong_unload_slot", row, column, feeder.id)
          }
          for k in slot..<(slot + feeder.width) {
            rack[k] = -1
          }
          positions[index] = 0
          subtotal += feeder.unload
        }
        "load" => {
          if positions[index] != 0 {
            return failure("already_mounted", row, column, feeder.id)
          }
          if slot < 1 ||
            slot > s.slots - feeder.width + 1 ||
            (!feeder.starts.is_empty() && !feeder.starts.contains(slot)) {
            return failure("illegal_start", row, column, feeder.id)
          }
          for k in slot..<(slot + feeder.width) {
            if rack[k] != -1 {
              return failure(
                "occupied_slot",
                row,
                column,
                "\{feeder.id} cannot cover slot \{k}",
              )
            }
          }
          for k in slot..<(slot + feeder.width) {
            rack[k] = index
          }
          positions[index] = slot
          subtotal += feeder.load
        }
        _ => return failure("action_kind", row, column, operation.kind)
      }
    }
    for id in job.need {
      if positions[feeders[id]] == 0 {
        return failure("missing_required", row, 0, id)
      }
    }
    if positions != step.positions {
      return failure(
        "positions", row, 0, "declared positions disagree with replay",
      )
    }
    if subtotal != step.cost {
      return failure("step_cost", row, 0, "recomputed step cost is \{subtotal}")
    }
    total += subtotal
  }
  if total != plan.cost {
    return failure("total_cost", 0, 0, "recomputed total is \{total}")
  }
  { cost: Some(total), finding: None, }
}