///|
/// HCL Expression types - represents the expression sub-language
/// Based on hcl-rs design: https://github.com/nickelchenwealth/hcl-rs

///|
/// Object key type - can be identifier or expression
pub(all) enum ObjectKey {
  /// Unquoted identifier used as object key
  Identifier(String)
  /// Any valid HCL expression can be an object key
  Expression(Expression)
} derive(Debug, Eq)

///|
/// Object type for expressions (preserves insertion order via array of pairs)
pub type ExprObject = Array[(ObjectKey, Expression)]

///|
/// Represents an HCL expression
pub(all) enum Expression {
  /// Null value
  ExprNull
  /// Boolean value
  ExprBool(Bool)
  /// Number (int)
  ExprInt(Int)
  /// 64-bit integer
  ExprInt64(Int64)
  /// Floating point number
  ExprFloat(Double)
  /// String without template interpolations
  ExprString(String)
  /// Array of expressions
  ExprArray(Array[Expression])
  /// Object (key-value pairs)
  ExprObject(ExprObject)
  /// Template expression (string with interpolations or heredoc)
  ExprTemplate(TemplateExpr)
  /// Variable reference
  ExprVariable(Variable)
  /// Property/element traversal
  ExprTraversal(Traversal)
  /// Function call
  ExprFuncCall(FuncCall)
  /// Parenthesized expression
  ExprParenthesis(Expression)
  /// Conditional expression (cond ? true_expr : false_expr)
  ExprConditional(Conditional)
  /// Binary or unary operation
  ExprOperation(Operation)
  /// For expression (list/object comprehension)
  ExprFor(ForExpr)
  /// Unknown value (Terraform plan-stage)
  ExprUnknown
} derive(Debug, Eq)

///|
/// Template expression (heredoc or interpolated string)
pub(all) enum TemplateExpr {
  /// Heredoc template
  Heredoc(Heredoc)
  /// Interpolated string template
  InterpolatedString(String)
} derive(Debug, Eq)

///|
/// Heredoc template
pub(all) struct Heredoc {
  /// Delimiter (e.g., "EOF")
  delimiter : String
  /// Template content
  content : String
  /// Strip indentation mode
  strip_mode : HeredocStripMode
} derive(Debug, Eq)

///|
/// Heredoc strip mode
pub(all) enum HeredocStripMode {
  /// No stripping
  NoStrip
  /// Strip leading whitespace (<<-EOF)
  StripMode
} derive(Debug, Eq)

///|
/// Variable reference
pub(all) struct Variable {
  /// Variable name
  name : String
} derive(Debug, Eq)

///|
/// Traversal (property/element access)
pub(all) struct Traversal {
  /// Base expression
  base : Expression
  /// Traversal operators
  operators : Array[TraversalOperator]
} derive(Debug, Eq)

///|
/// Traversal operator
pub(all) enum TraversalOperator {
  /// Attribute access (obj.attr)
  Attr(String)
  /// Index access (obj[index])
  Index(Expression)
  /// Splat access (obj.* or obj[*])
  Splat
} derive(Debug, Eq)

///|
/// Builder for constructing Traversal expressions incrementally
pub struct TraversalBuilder {
  base : Expression
  operators : Array[TraversalOperator]
}

///|
/// Create a new TraversalBuilder from a base expression
pub fn TraversalBuilder::new(base : Expression) -> TraversalBuilder {
  { base, operators: [] }
}

///|
/// Add an attribute access operator (.attr)
pub fn TraversalBuilder::attr(
  self : TraversalBuilder,
  name : String,
) -> TraversalBuilder {
  self.operators.push(Attr(name))
  self
}

///|
/// Add an index access operator ([expr])
pub fn TraversalBuilder::index(
  self : TraversalBuilder,
  expr : Expression,
) -> TraversalBuilder {
  self.operators.push(Index(expr))
  self
}

///|
/// Add a splat access operator (.*)
pub fn TraversalBuilder::splat(self : TraversalBuilder) -> TraversalBuilder {
  self.operators.push(Splat)
  self
}

///|
/// Build the Traversal expression
pub fn TraversalBuilder::build(self : TraversalBuilder) -> Traversal {
  { base: self.base, operators: self.operators }
}

///|
/// Create a TraversalBuilder starting from this Traversal
pub fn Traversal::builder(base : Expression) -> TraversalBuilder {
  TraversalBuilder::new(base)
}

///|
/// Function call
pub(all) struct FuncCall {
  /// Function name
  name : String
  /// Arguments
  args : Array[Expression]
} derive(Debug, Eq)

///|
/// Builder for constructing FuncCall expressions incrementally
pub struct FuncCallBuilder {
  name : String
  args : Array[Expression]
}

///|
/// Create a new FuncCallBuilder with a function name
pub fn FuncCallBuilder::new(name : String) -> FuncCallBuilder {
  { name, args: [] }
}

///|
/// Add an argument expression
pub fn FuncCallBuilder::arg(
  self : FuncCallBuilder,
  expr : Expression,
) -> FuncCallBuilder {
  self.args.push(expr)
  self
}

///|
/// Build the FuncCall expression
pub fn FuncCallBuilder::build(self : FuncCallBuilder) -> FuncCall {
  { name: self.name, args: self.args }
}

///|
/// Create a FuncCallBuilder for this function name
pub fn FuncCall::builder(name : String) -> FuncCallBuilder {
  FuncCallBuilder::new(name)
}

///|
/// Conditional expression (cond ? true_expr : false_expr)
pub(all) struct Conditional {
  /// Condition expression
  condition : Expression
  /// True branch expression
  true_expr : Expression
  /// False branch expression
  false_expr : Expression
} derive(Debug, Eq)

///|
/// Operation (binary or unary)
pub(all) enum Operation {
  /// Binary operation (left op right)
  Binary(BinaryOp)
  /// Unary operation (op operand)
  Unary(UnaryOp)
} derive(Debug, Eq)

///|
/// Binary operation
pub(all) struct BinaryOp {
  /// Left operand
  left : Expression
  /// Operator
  op : BinaryOperator
  /// Right operand
  right : Expression
} derive(Debug, Eq)

///|
/// Binary operator
pub(all) enum BinaryOperator {
  /// Addition (+)
  Plus
  /// Subtraction (-)
  Minus
  /// Multiplication (*)
  Mul
  /// Division (/)
  Div
  /// Modulo (%)
  Mod
  /// Equality (==)
  Eq
  /// Inequality (!=)
  Neq
  /// Less than (<)
  Lt
  /// Greater than (>)
  Gt
  /// Less than or equal (<=)
  Lte
  /// Greater than or equal (>=)
  Gte
  /// Logical AND (&&)
  And
  /// Logical OR (||)
  Or
} derive(Debug, Eq)

///|
/// Unary operation
pub(all) struct UnaryOp {
  /// Operator
  op : UnaryOperator
  /// Operand
  operand : Expression
} derive(Debug, Eq)

///|
/// Unary operator
pub(all) enum UnaryOperator {
  /// Negation (-)
  Negate
  /// Logical NOT (!)
  Not
} derive(Debug, Eq)

///|
/// For expression (list/object comprehension)
pub(all) enum ForExpr {
  /// List comprehension: [for x in list: expr]
  List(ForListExpr)
  /// Object comprehension: {for k, v in obj: k => v}
  Object(ForObjectExpr)
} derive(Debug, Eq)

///|
/// List comprehension
pub(all) struct ForListExpr {
  /// Loop variable name
  var_name : String
  /// Collection expression
  collection : Expression
  /// Value expression
  value_expr : Expression
  /// Optional filter expression
  filter : Expression?
} derive(Debug, Eq)

///|
/// Object comprehension
pub(all) struct ForObjectExpr {
  /// Key variable name
  key_var : String
  /// Value variable name
  value_var : String
  /// Collection expression
  collection : Expression
  /// Key expression
  key_expr : Expression
  /// Value expression
  value_expr : Expression
  /// Optional filter expression
  filter : Expression?
} derive(Debug, Eq)

///|
/// Convert ObjectKey to string representation
pub fn ObjectKey::to_string(self : ObjectKey) -> String {
  match self {
    Identifier(name) => name
    Expression(expr) => expr_to_string(expr)
  }
}

///|
/// Convert Expression to string representation
pub fn expr_to_string(expr : Expression) -> String {
  match expr {
    ExprNull => "null"
    ExprBool(b) => if b { "true" } else { "false" }
    ExprInt(i) => i.to_string()
    ExprInt64(i) => i.to_string()
    ExprFloat(f) => f.to_string()
    ExprString(s) => "\"" + s + "\""
    ExprArray(arr) => {
      let items = arr.map(expr_to_string)
      "[" + items.join(", ") + "]"
    }
    ExprObject(obj) =>
      if obj.is_empty() {
        "{}"
      } else {
        let items = obj.map(fn(pair) {
          let (k, v) = pair
          ObjectKey::to_string(k) + " = " + expr_to_string(v)
        })
        "{ " + items.join(", ") + " }"
      }
    ExprTemplate(_) => "