///|
/// Keep the wire contract explicit across the pre-1.0 and 1.0 compilers.
fn json_fields(
  value : Json,
  path : @json.JsonPath,
) -> Map[String, Json] raise @json.JsonDecodeError {
  guard value is Object(fields) else {
    raise @json.JsonDecodeError((path, "expected object"))
  }
  fields
}

///|
fn[T : @json.FromJson] required_field(
  fields : Map[String, Json],
  key : String,
  path : @json.JsonPath,
) -> T raise @json.JsonDecodeError {
  let context = path.add_key(key)
  guard fields.get(key) is Some(value) else {
    raise @json.JsonDecodeError((context, "missing field"))
  }
  @json.from_json(value, path=context)
}

///|
pub impl @json.FromJson for Feeder with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    id: required_field(fields, "id", path),
    width: required_field(fields, "width", path),
    starts: required_field(fields, "starts", path),
    load: required_field(fields, "load", path),
    unload: required_field(fields, "unload", path),
    locked: required_field(fields, "locked", path),
  }
}

///|
pub impl @json.FromJson for Job with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    id: required_field(fields, "id", path),
    need: required_field(fields, "need", path),
  }
}

///|
pub impl @json.FromJson for Shift with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    slots: required_field(fields, "slots", path),
    blocked: required_field(fields, "blocked", path),
    feeders: required_field(fields, "feeders", path),
    initial: required_field(fields, "initial", path),
    jobs: required_field(fields, "jobs", path),
  }
}

///|
pub impl @json.FromJson for Action with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    kind: required_field(fields, "kind", path),
    feeder: required_field(fields, "feeder", path),
    slot: required_field(fields, "slot", path),
  }
}

///|
pub impl @json.FromJson for Step with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    job: required_field(fields, "job", path),
    positions: required_field(fields, "positions", path),
    actions: required_field(fields, "actions", path),
    cost: required_field(fields, "cost", path),
  }
}

///|
pub impl @json.FromJson for Plan with fn from_json(value, path) {
  let fields = json_fields(value, path)
  {
    steps: required_field(fields, "steps", path),
    cost: required_field(fields, "cost", path),
  }
}

///|
pub impl ToJson for Feeder with fn to_json(self) {
  {
    "id": Json(self.id),
    "width": Json(self.width),
    "starts": Json(self.starts),
    "load": Json(self.load),
    "unload": Json(self.unload),
    "locked": Json(self.locked),
  }
}

///|
pub impl ToJson for Job with fn to_json(self) {
  { "id": Json(self.id), "need": Json(self.need) }
}

///|
pub impl ToJson for Shift with fn to_json(self) {
  {
    "slots": Json(self.slots),
    "blocked": Json(self.blocked),
    "feeders": Json(self.feeders),
    "initial": Json(self.initial),
    "jobs": Json(self.jobs),
  }
}

///|
pub impl ToJson for Action with fn to_json(self) {
  {
    "kind": Json(self.kind),
    "feeder": Json(self.feeder),
    "slot": Json(self.slot),
  }
}

///|
pub impl ToJson for Step with fn to_json(self) {
  {
    "job": Json(self.job),
    "positions": Json(self.positions),
    "actions": Json(self.actions),
    "cost": Json(self.cost),
  }
}

///|
pub impl ToJson for Plan with fn to_json(self) {
  { "steps": Json(self.steps), "cost": Json(self.cost) }
}

///|
pub impl ToJson for Diagnosis with fn to_json(self) {
  {
    "status": Json(self.status),
    "job": Json(self.job),
    "conflict": Json(self.conflict),
    "minimal": Json(self.minimal),
    "probes": Json(self.probes),
  }
}

///|
pub impl ToJson for Finding with fn to_json(self) {
  {
    "code": Json(self.code),
    "step": Json(self.step),
    "action": Json(self.action),
    "detail": Json(self.detail),
  }
}

///|
pub impl ToJson for Verification with fn to_json(self) {
  match (self.cost, self.finding) {
    (Some(cost), None) => { "cost": Json(cost) }
    (None, Some(finding)) => { "finding": Json(finding) }
    (Some(cost), Some(finding)) =>
      { "cost": Json(cost), "finding": Json(finding) }
    (None, None) => Json::empty_object()
  }
}

///|
pub impl ToJson for Outcome with fn to_json(self) {
  let document : Json = {
    "status": Json(self.status),
    "job": Json(self.job),
    "probes": Json(self.probes),
    "edges": Json(self.edges),
    "states": Json(self.states),
  }
  if document is Object(fields) && self.plan is Some(plan) {
    fields["plan"] = Json(plan)
  }
  document
}

///|
pub impl @debug.Debug for InputError with fn to_repr(self) {
  match self {
    Invalid(message) => @debug.Repr(("Invalid", message))
  }
}