///|
/// HCL Template System - handles string interpolation, heredoc, and template directives
/// Implements ${expr}, < Template {
  { parts: [] }
}

///|
/// Extract substring from string
pub fn substring(s : String, start : Int, end : Int) -> String {
  let mut result = ""
  let mut i = start
  while i < end && i < s.length() {
    result = result + s.code_unit_at(i).to_int().unsafe_to_char().to_string()
    i = i + 1
  }
  result
}

///|
/// Parse string with template expressions
pub fn Template::from_string(input : String) -> HCLResult[Template] {
  let template = Template::new()
  let mut i = 0
  let len = input.length()
  let mut text_start = 0

  while i < len {
    let c = input.code_unit_at(i)

    if c == '$'.to_int().to_uint16() &&
      i + 1 < len &&
      input.code_unit_at(i + 1) == '{'.to_int().to_uint16() {
      // Add text before interpolation
      if i > text_start {
        let text = substring(input, text_start, i)
        template.parts.push(Text(text))
      }

      // Parse interpolation
      match parse_interpolation(input, i + 2) {
        Ok((expr, end_pos)) => {
          template.parts.push(Interpolation(expr))
          i = end_pos
          text_start = i
        }
        Err(e) => return Err(e)
      }
    } else if c == '%'.to_int().to_uint16() &&
      i + 1 < len &&
      input.code_unit_at(i + 1) == '{'.to_int().to_uint16() {
      // Add text before directive
      if i > text_start {
        let text = substring(input, text_start, i)
        template.parts.push(Text(text))
      }

      // Parse directive
      match parse_directive(input, i + 2) {
        Ok((part, end_pos)) => {
          template.parts.push(part)
          i = end_pos
          text_start = i
        }
        Err(e) => return Err(e)
      }
    } else {
      i += 1
    }
  }

  // Add remaining text
  if text_start < len {
    let text = substring(input, text_start, len)
    template.parts.push(Text(text))
  }

  Ok(template)
}

///|
/// Parse interpolation expression ${expr}
fn parse_interpolation(
  input : String,
  start : Int,
) -> HCLResult[(HCLValue, Int)] {
  let mut i = start
  let len = input.length()
  let mut depth = 1
  let expr_start = i

  while i < len && depth > 0 {
    let c = input.code_unit_at(i)

    if c == '{'.to_int().to_uint16() {
      depth += 1
    } else if c == '}'.to_int().to_uint16() {
      depth -= 1
      if depth == 0 {
        // Extract expression
        let expr_str = substring(input, expr_start, i)
        // Parse expression
        match parse_template_expression(expr_str) {
          Ok(expr) => return Ok((expr, i + 1))
          Err(e) => return Err(e)
        }
      }
    }
    i += 1
  }

  if depth > 0 {
    Err(
      HCLError::parse_error_with_context(
        "Unclosed interpolation ${", 0, 0, start, "",
      ),
    )
  } else {
    Err(
      HCLError::parse_error_with_context(
        "Unexpected end of interpolation", 0, 0, i, "",
      ),
    )
  }
}

///|
/// Parse template directive %{directive}
fn parse_directive(
  input : String,
  start : Int,
) -> HCLResult[(TemplatePart, Int)] {
  let mut i = start
  let len = input.length()
  let mut depth = 1
  let content_start = i

  while i < len && depth > 0 {
    let c = input.code_unit_at(i)

    if c == '{'.to_int().to_uint16() {
      depth += 1
    } else if c == '}'.to_int().to_uint16() {
      depth -= 1
      if depth == 0 {
        // Extract directive content
        let content = substring(input, content_start, i)
        // Parse directive
        match parse_directive_content(content) {
          Ok(part) => return Ok((part, i + 1))
          Err(e) => return Err(e)
        }
      }
    }
    i += 1
  }

  if depth > 0 {
    Err(
      HCLError::parse_error_with_context(
        "Unclosed directive %{", 0, 0, start, "",
      ),
    )
  } else {
    Err(
      HCLError::parse_error_with_context(
        "Unexpected end of directive", 0, 0, i, "",
      ),
    )
  }
}

///|
/// Parse directive content
fn parse_directive_content(content : String) -> HCLResult[TemplatePart] {
  let trimmed = content.trim().to_string()

  if has_prefix(trimmed, "if ") {
    // %{if cond}
    let cond_str = substring(trimmed, 3, trimmed.length())
    match parse_template_expression(cond_str) {
      Ok(cond) => Ok(ConditionalStart(cond))
      Err(e) => Err(e)
    }
  } else if trimmed == "endif" {
    // %{endif}
    Ok(ConditionalEnd)
  } else if has_prefix(trimmed, "for ") {
    // %{for var in collection}
    match parse_for_directive(trimmed) {
      Ok((var_name, collection)) => Ok(ForStart(var_name, collection))
      Err(e) => Err(e)
    }
  } else if trimmed == "endfor" {
    // %{endfor}
    Ok(ForEnd)
  } else {
    Err(
      HCLError::parse_error_with_context(
        "Unknown directive: " + trimmed,
        0,
        0,
        0,
        "",
      ),
    )
  }
}

///|
/// Parse for directive %{for var in collection}
fn parse_for_directive(content : String) -> HCLResult[(String, HCLValue)] {
  // Expected format: "for var_name in collection_expr"
  let parts = []
  let mut current = ""
  for c in content {
    if c == ' ' {
      if current.length() > 0 {
        parts.push(current)
        current = ""
      }
    } else {
      current = current + c.to_string()
    }
  }
  if current.length() > 0 {
    parts.push(current)
  }

  if parts.length() < 4 || parts[0] != "for" || parts[2] != "in" {
    return Err(
      HCLError::parse_error_with_context(
        "Invalid for directive format", 0, 0, 0, "",
      ),
    )
  }

  let var_name = parts[1]
  let collection_str = parts[3]

  match parse_template_expression(collection_str) {
    Ok(collection) => Ok((var_name, collection))
    Err(e) => Err(e)
  }
}

///|
/// Check if string has prefix
fn has_prefix(s : String, prefix : String) -> Bool {
  if prefix.length() > s.length() {
    return false
  }
  let mut i = 0
  while i < prefix.length() {
    if s.code_unit_at(i) != prefix.code_unit_at(i) {
      return false
    }
    i += 1
  }
  true
}

///|
/// Check if string has suffix
fn has_suffix(s : String, suffix : String) -> Bool {
  if suffix.length() > s.length() {
    return false
  }
  let offset = s.length() - suffix.length()
  let mut i = 0
  while i < suffix.length() {
    if s.code_unit_at(offset + i) != suffix.code_unit_at(i) {
      return false
    }
    i += 1
  }
  true
}

///|
/// Parse simple template expression (variable or literal)
fn parse_template_expression(expr_str : String) -> HCLResult[HCLValue] {
  let trimmed = expr_str.trim().to_string()

  // Check for string literal
  if has_prefix(trimmed, "\"") && has_suffix(trimmed, "\"") {
    let content = substring(trimmed, 1, trimmed.length() - 1)
    return Ok(String(content, Decor::empty()))
  }

  // Check for number
  let mut is_number = true
  for c in trimmed {
    if !(c >= '0' && c <= '9') && c != '.' && c != '-' {
      is_number = false
      break
    }
  }
  if is_number && trimmed.length() > 0 {
    // Simple number parsing
    if trimmed.contains(".") {
      // Float - simplified
      return Ok(Number(Number::from_finite_f64(0.0), Decor::empty()))
    } else {
      // Integer
      let mut result = 0
      let mut negative = false
      let mut start = 0
      if trimmed.code_unit_at(0) == '-'.to_int().to_uint16() {
        negative = true
        start = 1
      }
      let mut i = start
      while i < trimmed.length() {
        let c = trimmed.code_unit_at(i)
        if c >= '0'.to_int().to_uint16() && c <= '9'.to_int().to_uint16() {
          result = result * 10 + (c.to_int() - '0'.to_int())
        }
        i += 1
      }
      return Ok(
        Number(
          Number::from_int(if negative { -result } else { result }),
          Decor::empty(),
        ),
      )
    }
  }

  // Check for boolean
  if trimmed == "true" {
    return Ok(Bool(true, Decor::empty()))
  }
  if trimmed == "false" {
    return Ok(Bool(false, Decor::empty()))
  }

  // Check for null
  if trimmed == "null" {
    return Ok(Null(Decor::empty()))
  }

  // Variable reference
  Ok(
    Object(
      {
        "type": String("variable", Decor::empty()),
        "name": String(trimmed, Decor::empty()),
      },
      Decor::empty(),
    ),
  )
}

///|
/// Evaluate template with given context
pub fn Template::evaluate(
  self : Template,
  variables : Map[String, HCLValue],
  functions : Map[String, (Array[HCLValue]) -> HCLResult[HCLValue]],
) -> HCLResult[String] {
  let mut result = ""
  let mut i = 0
  let parts = self.parts

  while i < parts.length() {
    let part = parts[i]
    match part {
      Text(text) => {
        result = result + text
        i += 1
      }
      Interpolation(expr) =>
        match eval_expr(expr, variables, functions) {
          Ok(value) => {
            result = result + value_to_string(value)
            i += 1
          }
          Err(e) => return Err(e)
        }
      ConditionalStart(condition) =>
        match eval_expr(condition, variables, functions) {
          Ok(cond_value) =>
            match cond_value {
              Bool(b, _) =>
                if b {
                  // Evaluate conditional body
                  match
                    evaluate_conditional_body(
                      parts,
                      i + 1,
                      variables,
                      functions,
                    ) {
                    Ok((body_result, end_pos)) => {
                      result = result + body_result
                      i = end_pos + 1
                    }
                    Err(e) => return Err(e)
                  }
                } else {
                  // Skip conditional body
                  match skip_conditional_body(parts, i + 1) {
                    Ok(end_pos) => i = end_pos + 1
                    Err(e) => return Err(e)
                  }
                }
              _ => return Err(HCLError::type_error("Condition must be boolean"))
            }
          Err(e) => return Err(e)
        }
      ForStart(var_name, collection) =>
        match eval_expr(collection, variables, functions) {
          Ok(collection_value) =>
            match collection_value {
              Array(arr, _) =>
                // Evaluate for loop body for each item
                match
                  evaluate_for_body(
                    parts,
                    i + 1,
                    var_name,
                    arr,
                    variables,
                    functions,
                  ) {
                  Ok((body_result, end_pos)) => {
                    result = result + body_result
                    i = end_pos + 1
                  }
                  Err(e) => return Err(e)
                }
              _ =>
                return Err(HCLError::type_error("For collection must be array"))
            }
          Err(e) => return Err(e)
        }
      ConditionalEnd | ForEnd =>
        // These should be handled by their respective start directives
        i += 1
    }
  }

  Ok(result)
}

///|
/// Evaluate conditional body
fn evaluate_conditional_body(
  parts : Array[TemplatePart],
  start : Int,
  variables : Map[String, HCLValue],
  functions : Map[String, (Array[HCLValue]) -> HCLResult[HCLValue]],
) -> HCLResult[(String, Int)] {
  let mut result = ""
  let mut i = start
  let mut depth = 0

  while i < parts.length() {
    let part = parts[i]
    match part {
      ConditionalStart(_) => {
        depth += 1
        i += 1
      }
      ConditionalEnd => {
        if depth == 0 {
          return Ok((result, i))
        }
        depth -= 1
        i += 1
      }
      _ => {
        // Evaluate part
        let temp_template = { parts: [part] }
        match temp_template.evaluate(variables, functions) {
          Ok(text) => {
            result = result + text
            i += 1
          }
          Err(e) => return Err(e)
        }
      }
    }
  }

  Err(
    HCLError::parse_error_with_context("Unclosed conditional", 0, 0, start, ""),
  )
}

///|
/// Skip conditional body (when condition is false)
fn skip_conditional_body(
  parts : Array[TemplatePart],
  start : Int,
) -> HCLResult[Int] {
  let mut i = start
  let mut depth = 0

  while i < parts.length() {
    let part = parts[i]
    match part {
      ConditionalStart(_) => depth += 1
      ConditionalEnd => {
        if depth == 0 {
          return Ok(i)
        }
        depth -= 1
      }
      _ => ignore(part)
    }
    i += 1
  }

  Err(
    HCLError::parse_error_with_context("Unclosed conditional", 0, 0, start, ""),
  )
}

///|
/// Evaluate for loop body
fn evaluate_for_body(
  parts : Array[TemplatePart],
  start : Int,
  var_name : String,
  arr : Array[HCLValue],
  variables : Map[String, HCLValue],
  functions : Map[String, (Array[HCLValue]) -> HCLResult[HCLValue]],
) -> HCLResult[(String, Int)] {
  let mut result = ""

  // Find end of for loop
  let end_pos = find_for_end(parts, start)
  if end_pos < 0 {
    return Err(
      HCLError::parse_error_with_context("Unclosed for loop", 0, 0, start, ""),
    )
  }

  // Evaluate for each item
  for item in arr {
    // Create new context with loop variable
    let loop_vars = variables.copy()
    loop_vars[var_name] = item

    // Evaluate body
    let mut j = start
    while j < end_pos {
      let part = parts[j]
      match part {
        Text(text) => {
          result = result + text
          j += 1
        }
        Interpolation(expr) =>
          match eval_expr(expr, loop_vars, functions) {
            Ok(value) => {
              result = result + value_to_string(value)
              j += 1
            }
            Err(e) => return Err(e)
          }
        _ => {
          // Handle nested structures
          let temp_template = { parts: [part] }
          match temp_template.evaluate(loop_vars, functions) {
            Ok(text) => {
              result = result + text
              j += 1
            }
            Err(e) => return Err(e)
          }
        }
      }
    }
  }

  Ok((result, end_pos))
}

///|
/// Find end position of for loop
fn find_for_end(parts : Array[TemplatePart], start : Int) -> Int {
  let mut i = start
  let mut depth = 0

  while i < parts.length() {
    let part = parts[i]
    match part {
      ForStart(_, _) => depth += 1
      ForEnd => {
        if depth == 0 {
          return i
        }
        depth -= 1
      }
      _ => ignore(part)
    }
    i += 1
  }

  -1
}

///|
/// Parse heredoc content
pub fn Template::from_heredoc(
  input : String,
  _delimiter : String,
  strip : Bool,
) -> HCLResult[Template] {
  let processed_input = if strip { strip_heredoc_indent(input) } else { input }

  let template = Template::new()
  let mut i = 0
  let len = processed_input.length()
  let mut text_start = 0

  // Parse content directly (input is already the content between delimiters)
  while i < len {
    let c = processed_input.code_unit_at(i)

    // Handle interpolation in heredoc
    if c == '$'.to_int().to_uint16() &&
      i + 1 < len &&
      processed_input.code_unit_at(i + 1) == '{'.to_int().to_uint16() {
      // Add text before interpolation
      if i > text_start {
        let text = substring(processed_input, text_start, i)
        template.parts.push(Text(text))
      }

      // Parse interpolation
      match parse_interpolation(processed_input, i + 2) {
        Ok((expr, end_pos)) => {
          template.parts.push(Interpolation(expr))
          i = end_pos
          text_start = i
        }
        Err(e) => return Err(e)
      }
    } else {
      i += 1
    }
  }

  // Add remaining text
  if text_start < len {
    let text = substring(processed_input, text_start, len)
    template.parts.push(Text(text))
  }

  Ok(template)
}

///|
/// Compute minimum indentation across all lines and strip it
fn strip_heredoc_indent(text : String) -> String {
  let len = text.length()
  if len == 0 {
    return text
  }

  // Pass 1: Find minimum indentation across non-empty lines
  let mut min_indent = -1
  let mut line_indent = 0
  let mut line_has_content = false
  let mut at_line_start = true

  let mut i = 0
  while i < len {
    let c = text.code_unit_at(i)
    if c == '\n'.to_int().to_uint16() {
      if line_has_content {
        if min_indent < 0 || line_indent < min_indent {
          min_indent = line_indent
        }
      }
      line_indent = 0
      line_has_content = false
      at_line_start = true
    } else if at_line_start &&
      (c == ' '.to_int().to_uint16() || c == '\t'.to_int().to_uint16()) {
      line_indent += 1
    } else {
      at_line_start = false
      line_has_content = true
    }
    i += 1
  }
  // Handle last line (no trailing newline)
  if line_has_content {
    if min_indent < 0 || line_indent < min_indent {
      min_indent = line_indent
    }
  }

  if min_indent <= 0 {
    return text
  }

  // Pass 2: Strip min_indent characters from beginning of each line
  let mut result = ""
  let mut chars_stripped = 0
  let mut is_start_of_line = true

  i = 0
  while i < len {
    let c = text.code_unit_at(i)
    if c == '\n'.to_int().to_uint16() {
      result = result + "\n"
      is_start_of_line = true
      chars_stripped = 0
    } else if is_start_of_line &&
      chars_stripped < min_indent &&
      (c == ' '.to_int().to_uint16() || c == '\t'.to_int().to_uint16()) {
      chars_stripped += 1
    } else {
      is_start_of_line = false
      result = result + c.to_int().unsafe_to_char().to_string()
    }
    i += 1
  }

  result
}

///|
/// Convert HCLValue to string
fn value_to_string(value : HCLValue) -> String {
  match value {
    Null(_) => "null"
    Bool(b, _) => if b { "true" } else { "false" }
    Number(n, _) => n.to_string()
    String(s, _) => s
    Array(_, _) => "[array]"
    Object(_, _) => "[object]"
  }
}

///|
/// Parse template from string (simple version)
pub fn parse_template(input : String) -> HCLResult[Template] {
  Template::from_string(input)
}

///|
/// Evaluate template string directly
pub fn evaluate_template(
  input : String,
  variables : Map[String, HCLValue],
  functions : Map[String, (Array[HCLValue]) -> HCLResult[HCLValue]],
) -> HCLResult[String] {
  match parse_template(input) {
    Ok(template) => template.evaluate(variables, functions)
    Err(e) => Err(e)
  }
}