///|
/// Builder for constructing Body incrementally
///
/// ```mbt check
/// test {
///   let b = Body::builder().attr("x", expr_int(1)).build()
///   assert_eq(b.len(), 1)
/// }
/// ```
pub struct BodyBuilder {
  items : Array[BodyItem]
}

///|
/// Create a new BodyBuilder
///
/// ```mbt check
/// test {
///   let b = Body::builder()
///   assert_eq(b.build().len(), 0)
/// }
/// ```
pub fn Body::builder() -> BodyBuilder {
  { items: [] }
}

///|
/// Add an attribute with Expression
///
/// ```mbt check
/// test {
///   let b = Body::builder().attr("x", expr_int(1)).build()
///   assert_eq(b.find_attr("x").unwrap().get_key(), "x")
/// }
/// ```
pub fn BodyBuilder::attr(
  self : BodyBuilder,
  key : String,
  value : Expression,
) -> BodyBuilder {
  self.items.push(
    Attribute({
      key: Decorated::bare(key),
      value: Spanned::bare(value),
      decor: Decor::empty(),
    }),
  )
  self
}

///|
/// Add an attribute from ToHCL value
///
/// ```mbt check
/// test {
///   let b = Body::builder().attr_hcl("port", 8080).build()
///   assert_eq(expr_to_string(b.find_attr("port").unwrap().get_value()), "8080")
/// }
/// ```
pub fn[T : ToHCL] BodyBuilder::attr_hcl(
  self : BodyBuilder,
  key : String,
  value : T,
) -> BodyBuilder {
  self.items.push(
    Attribute({
      key: Decorated::bare(key),
      value: Spanned::bare(hcl_value_to_expression(value.to_hcl())),
      decor: Decor::empty(),
    }),
  )
  self
}

///|
/// Add an attribute conditionally
///
/// ```mbt check
/// test {
///   let b = Body::builder()
///     .attr_if("a", expr_int(1), true)
///     .attr_if("b", expr_int(2), false)
///     .build()
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn BodyBuilder::attr_if(
  self : BodyBuilder,
  key : String,
  value : Expression,
  cond : Bool,
) -> BodyBuilder {
  if cond {
    self.items.push(
      Attribute({
        key: Decorated::bare(key),
        value: Spanned::bare(value),
        decor: Decor::empty(),
      }),
    )
  }
  self
}

///|
/// Add an attribute from Option (skip if None)
///
/// ```mbt check
/// test {
///   let some : Int? = Some(5)
///   let none : Int? = None
///   let b = Body::builder().attr_option("a", some).attr_option("b", none).build()
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn[T : ToHCL] BodyBuilder::attr_option(
  self : BodyBuilder,
  key : String,
  value : T?,
) -> BodyBuilder {
  match value {
    Some(v) =>
      self.items.push(
        Attribute({
          key: Decorated::bare(key),
          value: Spanned::bare(hcl_value_to_expression(v.to_hcl())),
          decor: Decor::empty(),
        }),
      )
    None => ()
  }
  self
}

///|
/// Add a block
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server").build()
///   let b = Body::builder().block(blk).build()
///   assert_eq(b.find_block("server").unwrap().get_type_name(), "server")
/// }
/// ```
pub fn BodyBuilder::block(self : BodyBuilder, blk : Block) -> BodyBuilder {
  self.items.push(Block(blk))
  self
}

///|
/// Build the Body
///
/// ```mbt check
/// test {
///   let b = Body::builder().attr("x", expr_int(1)).build()
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn BodyBuilder::build(self : BodyBuilder) -> Body {
  { items: self.items, decor: Decor::empty(), span: None }
}

///|
/// Builder for constructing Block incrementally
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server")
///     .label("web")
///     .attr("port", expr_int(80))
///     .build()
///   assert_eq(blk.get_type_name(), "server")
///   assert_eq(blk.get_labels(), ["web"])
/// }
/// ```
pub struct BlockBuilder {
  type_name : String
  labels : Array[String]
  items : Array[BodyItem]
}

///|
/// Create a new BlockBuilder
///
/// ```mbt check
/// test {
///   let b = Block::builder("resource")
///   let blk = b.build()
///   assert_eq(blk.get_type_name(), "resource")
/// }
/// ```
pub fn Block::builder(type_name : String) -> BlockBuilder {
  { type_name, labels: [], items: [] }
}

///|
/// Add a label
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server").label("web").label("prod").build()
///   assert_eq(blk.get_labels(), ["web", "prod"])
/// }
/// ```
pub fn BlockBuilder::label(self : BlockBuilder, lbl : String) -> BlockBuilder {
  self.labels.push(lbl)
  self
}

///|
/// Add an attribute with Expression
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server").attr("port", expr_int(80)).build()
///   assert_eq(blk.find_attr("port").unwrap().get_key(), "port")
/// }
/// ```
pub fn BlockBuilder::attr(
  self : BlockBuilder,
  key : String,
  value : Expression,
) -> BlockBuilder {
  self.items.push(
    Attribute({
      key: Decorated::bare(key),
      value: Spanned::bare(value),
      decor: Decor::empty(),
    }),
  )
  self
}

///|
/// Add an attribute from ToHCL value
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server").attr_hcl("port", 8080).build()
///   assert_eq(expr_to_string(blk.find_attr("port").unwrap().get_value()), "8080")
/// }
/// ```
pub fn[T : ToHCL] BlockBuilder::attr_hcl(
  self : BlockBuilder,
  key : String,
  value : T,
) -> BlockBuilder {
  self.items.push(
    Attribute({
      key: Decorated::bare(key),
      value: Spanned::bare(hcl_value_to_expression(value.to_hcl())),
      decor: Decor::empty(),
    }),
  )
  self
}

///|
/// Add an attribute conditionally
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server")
///     .attr_if("a", expr_int(1), true)
///     .attr_if("b", expr_int(2), false)
///     .build()
///   assert_eq(blk.get_body().len(), 1)
/// }
/// ```
pub fn BlockBuilder::attr_if(
  self : BlockBuilder,
  key : String,
  value : Expression,
  cond : Bool,
) -> BlockBuilder {
  if cond {
    self.items.push(
      Attribute({
        key: Decorated::bare(key),
        value: Spanned::bare(value),
        decor: Decor::empty(),
      }),
    )
  }
  self
}

///|
/// Set the body from an existing Body
///
/// ```mbt check
/// test {
///   let inner = Body::builder().attr("x", expr_int(1)).build()
///   let blk = Block::builder("server").body(inner).build()
///   assert_eq(blk.get_body().len(), 1)
/// }
/// ```
pub fn BlockBuilder::body(self : BlockBuilder, b : Body) -> BlockBuilder {
  for item in b.get_items() {
    self.items.push(item)
  }
  self
}

///|
/// Build the Block
///
/// ```mbt check
/// test {
///   let blk = Block::builder("server").label("web").build()
///   assert_eq(blk.get_type_name(), "server")
///   assert_eq(blk.get_labels().length(), 1)
/// }
/// ```
pub fn BlockBuilder::build(self : BlockBuilder) -> Block {
  {
    type_name: Decorated::bare(self.type_name),
    labels: self.labels.map(Decorated::bare),
    body: { items: self.items, decor: Decor::empty(), span: None },
    decor: Decor::empty(),
    span: None,
  }
}