///|
/// HCL Body - top-level container for attributes and blocks with decoration
///
/// ```mbt check
/// test {
///   let b = Body::new().add_attr("x", expr_int(1))
///   assert_eq(b.len(), 1)
/// }
/// ```
pub struct Body {
  items : Array[BodyItem]
  decor : Decor
  span : Span?
} derive(Debug, Eq)

///|
/// Body item can be either an attribute or a block
///
/// ```mbt check
/// test {
///   let it = BodyItem::Attribute(Attr::new("x", expr_int(1)))
///   let b = Body::from_items([it])
///   assert_eq(b.len(), 1)
/// }
/// ```
pub(all) enum BodyItem {
  /// Key-value attribute
  Attribute(Attr)
  /// Named block with labels
  Block(Block)
} derive(Debug, Eq)

///|
/// HCL Attribute - key-value pair with decoration
///
/// ```mbt check
/// test {
///   let a = Attr::new("port", expr_int(80))
///   assert_eq(a.get_key(), "port")
/// }
/// ```
pub struct Attr {
  key : Decorated[String]
  value : Spanned[Expression]
  decor : Decor
} derive(Debug, Eq)

///|
/// HCL Block - named container with optional labels and decoration
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new())
///   assert_eq(blk.get_type_name(), "server")
/// }
/// ```
pub struct Block {
  type_name : Decorated[String]
  labels : Array[Decorated[String]]
  body : Body
  decor : Decor
  span : Span?
} derive(Debug, Eq)

///|
/// Create a new empty Body with decoration
///
/// ```mbt check
/// test {
///   let b = Body::new()
///   assert_eq(b.len(), 0)
///   assert_true(b.is_empty())
/// }
/// ```
pub fn Body::new() -> Body {
  { items: [], decor: Decor::empty(), span: None }
}

///|
/// Create a Body from items with decoration
///
/// ```mbt check
/// test {
///   let b = Body::from_items([BodyItem::Attribute(Attr::new("x", expr_int(1)))])
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn Body::from_items(items : Array[BodyItem]) -> Body {
  { items, decor: Decor::empty(), span: None }
}

///|
/// Create a Body with full decoration
///
/// ```mbt check
/// test {
///   let b = Body::with_decor(
///     [BodyItem::Attribute(Attr::new("x", expr_int(1)))],
///     Decor::empty(),
///   )
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn Body::with_decor(items : Array[BodyItem], decor : Decor) -> Body {
  { items, decor, span: None }
}

///|
/// Get the items of the body
///
/// ```mbt check
/// test {
///   let b = Body::new().add_attr("x", expr_int(1))
///   assert_eq(b.get_items().length(), 1)
/// }
/// ```
pub fn Body::get_items(self : Body) -> Array[BodyItem] {
  self.items
}

///|
/// Get the number of items in the body
///
/// ```mbt check
/// test {
///   let b = Body::new().add_attr("x", expr_int(1))
///   assert_eq(b.len(), 1)
/// }
/// ```
pub fn Body::len(self : Body) -> Int {
  self.items.length()
}

///|
/// Check if body is empty
///
/// ```mbt check
/// test {
///   assert_eq(Body::new().is_empty(), true)
///   assert_eq(Body::new().add_attr("x", expr_int(1)).is_empty(), false)
/// }
/// ```
pub fn Body::is_empty(self : Body) -> Bool {
  self.items.is_empty()
}

///|
/// Get body decoration
///
/// ```mbt check
/// test {
///   let b = Body::new()
///   assert_eq(b.get_decor(), Decor::empty())
/// }
/// ```
pub fn Body::get_decor(self : Body) -> Decor {
  self.decor
}

///|
/// Set span on body
///
/// ```mbt check
/// test {
///   let b = Body::new().set_span(Span::new(1, 1, 0, 1, 2, 1))
///   assert_eq(b.get_span().unwrap(), Span::new(1, 1, 0, 1, 2, 1))
/// }
/// ```
pub fn Body::set_span(self : Body, span : Span) -> Body {
  { items: self.items, decor: self.decor, span: Some(span) }
}

///|
/// Get body span
///
/// ```mbt check
/// test {
///   assert_eq(Body::new().get_span(), None)
/// }
/// ```
pub fn Body::get_span(self : Body) -> Span? {
  self.span
}

///|
/// Add an attribute to the body (returns new body)
///
/// ```mbt check
/// test {
///   let b = Body::new().add_attr("x", expr_int(1))
///   assert_eq(b.find_attr("x").unwrap().get_key(), "x")
/// }
/// ```
pub fn Body::add_attr(self : Body, key : String, value : Expression) -> Body {
  let new_items = self.items.copy()
  new_items.push(
    Attribute({
      key: Decorated::bare(key),
      value: Spanned::bare(value),
      decor: Decor::empty(),
    }),
  )
  { items: new_items, decor: self.decor, span: self.span }
}

///|
/// Add a block to the body (returns new body)
///
/// ```mbt check
/// test {
///   let b = Body::new().add_block(Block::simple("server", Body::new()))
///   assert_eq(b.find_block("server").unwrap().get_type_name(), "server")
/// }
/// ```
pub fn Body::add_block(self : Body, block : Block) -> Body {
  let new_items = self.items.copy()
  new_items.push(Block(block))
  { items: new_items, decor: self.decor, span: self.span }
}

///|
/// Find an attribute by key
///
/// ```mbt check
/// test {
///   let b = Body::new().add_attr("x", expr_int(1))
///   assert_eq(expr_to_string(b.find_attr("x").unwrap().get_value()), "1")
/// }
/// ```
pub fn Body::find_attr(self : Body, key : String) -> Attr? {
  for item in self.items {
    match item {
      Attribute(attr) if attr.key.get_value() == key => return Some(attr)
      _ => ()
    }
  }
  None
}

///|
/// Find all blocks of a given type
///
/// ```mbt check
/// test {
///   let b = Body::new().add_block(Block::simple("server", Body::new()))
///   assert_eq(b.find_blocks("server").length(), 1)
///   assert_eq(b.find_blocks("other").length(), 0)
/// }
/// ```
pub fn Body::find_blocks(self : Body, type_name : String) -> Array[Block] {
  let result = []
  for item in self.items {
    match item {
      Block(block) if block.type_name.get_value() == type_name =>
        result.push(block)
      _ => ()
    }
  }
  result
}

///|
/// Find first block of a given type
///
/// ```mbt check
/// test {
///   let b = Body::new().add_block(Block::simple("server", Body::new()))
///   assert_true(b.find_block("server") is Some(_))
///   assert_eq(b.find_block("other"), None)
/// }
/// ```
pub fn Body::find_block(self : Body, type_name : String) -> Block? {
  for item in self.items {
    match item {
      Block(block) if block.type_name.get_value() == type_name =>
        return Some(block)
      _ => ()
    }
  }
  None
}

///|
/// Create a new attribute with decoration
///
/// ```mbt check
/// test {
///   let a = Attr::new("port", expr_int(80))
///   assert_eq(a.get_key(), "port")
///   assert_eq(expr_to_string(a.get_value()), "80")
/// }
/// ```
pub fn Attr::new(key : String, value : Expression) -> Attr {
  {
    key: Decorated::bare(key),
    value: Spanned::bare(value),
    decor: Decor::empty(),
  }
}

///|
/// Create a new attribute with full decoration
///
/// ```mbt check
/// test {
///   let a = Attr::with_decor(
///     Decorated::bare("x"),
///     Spanned::bare(expr_int(1)),
///     Decor::empty(),
///   )
///   assert_eq(a.get_key(), "x")
/// }
/// ```
pub fn Attr::with_decor(
  key : Decorated[String],
  value : Spanned[Expression],
  decor : Decor,
) -> Attr {
  { key, value, decor }
}

///|
/// Get attribute key
///
/// ```mbt check
/// test {
///   assert_eq(Attr::new("x", expr_int(1)).get_key(), "x")
/// }
/// ```
pub fn Attr::get_key(self : Attr) -> String {
  self.key.get_value()
}

///|
/// Get attribute key with decoration
///
/// ```mbt check
/// test {
///   let a = Attr::new("x", expr_int(1))
///   assert_eq(a.get_decorated_key().get_value(), "x")
/// }
/// ```
pub fn Attr::get_decorated_key(self : Attr) -> Decorated[String] {
  self.key
}

///|
/// Get attribute decoration
///
/// ```mbt check
/// test {
///   assert_eq(Attr::new("x", expr_int(1)).get_decor(), Decor::empty())
/// }
/// ```
pub fn Attr::get_decor(self : Attr) -> Decor {
  self.decor
}

///|
/// Get attribute value
///
/// ```mbt check
/// test {
///   let a = Attr::new("x", expr_int(1))
///   assert_eq(expr_to_string(a.get_value()), "1")
/// }
/// ```
pub fn Attr::get_value(self : Attr) -> Expression {
  self.value.unwrap()
}

///|
/// Get attribute spanned value
///
/// ```mbt check
/// test {
///   let a = Attr::new("x", expr_int(1))
///   assert_eq(expr_to_string(a.get_spanned_value().unwrap()), "1")
/// }
/// ```
pub fn Attr::get_spanned_value(self : Attr) -> Spanned[Expression] {
  self.value
}

///|
/// Set span on attribute value
///
/// ```mbt check
/// test {
///   let a = Attr::new("x", expr_int(1)).set_span(Span::new(1, 1, 0, 1, 2, 1))
///   assert_true(a.get_spanned_value().get_span() is Some(_))
/// }
/// ```
pub fn Attr::set_span(self : Attr, span : Span) -> Attr {
  {
    key: self.key,
    value: Spanned::new(self.value.unwrap(), span),
    decor: self.decor,
  }
}

///|
/// Create a new block with decoration
///
/// ```mbt check
/// test {
///   let blk = Block::new("server", ["web"], Body::new())
///   assert_eq(blk.get_type_name(), "server")
///   assert_eq(blk.get_labels(), ["web"])
/// }
/// ```
pub fn Block::new(
  type_name : String,
  labels : Array[String],
  body : Body,
) -> Block {
  {
    type_name: Decorated::bare(type_name),
    labels: labels.map(Decorated::bare),
    body,
    decor: Decor::empty(),
    span: None,
  }
}

///|
/// Create a new block with full decoration
///
/// ```mbt check
/// test {
///   let blk = Block::with_decor(
///     Decorated::bare("server"),
///     [Decorated::bare("web")],
///     Body::new(),
///     Decor::empty(),
///   )
///   assert_eq(blk.get_type_name(), "server")
/// }
/// ```
pub fn Block::with_decor(
  type_name : Decorated[String],
  labels : Array[Decorated[String]],
  body : Body,
  decor : Decor,
) -> Block {
  { type_name, labels, body, decor, span: None }
}

///|
/// Create a block without labels
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new())
///   assert_eq(blk.get_type_name(), "server")
///   assert_eq(blk.get_labels().length(), 0)
/// }
/// ```
pub fn Block::simple(type_name : String, body : Body) -> Block {
  {
    type_name: Decorated::bare(type_name),
    labels: [],
    body,
    decor: Decor::empty(),
    span: None,
  }
}

///|
/// Get block type name
///
/// ```mbt check
/// test {
///   assert_eq(Block::simple("server", Body::new()).get_type_name(), "server")
/// }
/// ```
pub fn Block::get_type_name(self : Block) -> String {
  self.type_name.get_value()
}

///|
/// Get block type name with decoration
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new())
///   assert_eq(blk.get_decorated_type_name().get_value(), "server")
/// }
/// ```
pub fn Block::get_decorated_type_name(self : Block) -> Decorated[String] {
  self.type_name
}

///|
/// Get block decoration
///
/// ```mbt check
/// test {
///   assert_eq(Block::simple("server", Body::new()).get_decor(), Decor::empty())
/// }
/// ```
pub fn Block::get_decor(self : Block) -> Decor {
  self.decor
}

///|
/// Set span on block
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new()).set_span(
///     Span::new(1, 1, 0, 1, 2, 1),
///   )
///   assert_true(blk.get_span() is Some(_))
/// }
/// ```
pub fn Block::set_span(self : Block, span : Span) -> Block {
  {
    type_name: self.type_name,
    labels: self.labels,
    body: self.body,
    decor: self.decor,
    span: Some(span),
  }
}

///|
/// Get block span
///
/// ```mbt check
/// test {
///   assert_eq(Block::simple("server", Body::new()).get_span(), None)
/// }
/// ```
pub fn Block::get_span(self : Block) -> Span? {
  self.span
}

///|
/// Get block labels
///
/// ```mbt check
/// test {
///   let blk = Block::new("server", ["web", "prod"], Body::new())
///   assert_eq(blk.get_labels(), ["web", "prod"])
/// }
/// ```
pub fn Block::get_labels(self : Block) -> Array[String] {
  self.labels.map(fn(l) { l.get_value() })
}

///|
/// Get block labels with decoration
///
/// ```mbt check
/// test {
///   let blk = Block::new("server", ["web"], Body::new())
///   assert_eq(blk.get_decorated_labels().length(), 1)
///   assert_eq(blk.get_decorated_labels()[0].get_value(), "web")
/// }
/// ```
pub fn Block::get_decorated_labels(self : Block) -> Array[Decorated[String]] {
  self.labels
}

///|
/// Get block body
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new().add_attr("x", expr_int(1)))
///   assert_eq(blk.get_body().len(), 1)
/// }
/// ```
pub fn Block::get_body(self : Block) -> Body {
  self.body
}

///|
/// Find an attribute in block body
///
/// ```mbt check
/// test {
///   let blk = Block::simple("server", Body::new().add_attr("port", expr_int(80)))
///   assert_eq(blk.find_attr("port").unwrap().get_key(), "port")
/// }
/// ```
pub fn Block::find_attr(self : Block, key : String) -> Attr? {
  self.body.find_attr(key)
}