context shop.inventory

type Reservation = {
  id: String,
  qty: Int,
}

agent Inventory {
  key sku: String

  store reservations: Map[String, Reservation]

  -- builders + a materialising terminal
  on call bigQuantities() -> Effect[List[Int]] {
    reservations.filter((r) => r.qty > 0).map((r) => r.qty).collect()
  }

  -- ordering + paging
  on call pageById() -> Effect[List[Reservation]] {
    reservations.sortBy((r) => r.id).skip(1).take(10).collect()
  }

  -- aggregate terminals
  on call total() -> Effect[Int] {
    reservations.sum((r) => r.qty)
  }

  on call largest() -> Effect[Option[Int]] {
    reservations.max((r) => r.qty)
  }

  on call howMany() -> Effect[Int] {
    reservations.filter((r) => r.qty > 0).count()
  }

  on call anyBig() -> Effect[Bool] {
    reservations.any((r) => r.qty > 100)
  }

  -- a pure-built query, terminated later (the Query is a value)
  on call pending() -> Effect[List[Reservation]] {
    let q = reservations.filter((r) => r.qty > 0)
    q.distinctBy((r) => r.id).collect()
  }

  on call firstBig() -> Effect[Reservation] {
    reservations.filter((r) => r.qty > 50).firstOrElse(Reservation { id: "none", qty: 0 })
  }
}
