///|
/// XDNA module demo - Geneious XDNA binary sequence format parser and writer.
///
/// Run: moon run examples/xdna_demo
fn main {
  println("=== XDNA Module Demo ===")
  println("Geneious XDNA binary sequence format (2-bit packed DNA)")
  println("Reference: BioPython Bio.SeqIO.XdnaIO")
  println("")

  // ------------------------------------------------------------------
  // 1. Load sample XDNA file
  // ------------------------------------------------------------------
  println("=== 1. Load sample XDNA file ===")
  let file = @bio.xdna_sample_file()
  println("Loaded sample XDNA file")
  println("")

  // ------------------------------------------------------------------
  // 2. Print file info (n_records, version)
  // ------------------------------------------------------------------
  println("=== 2. File info ===")
  println("n_records: \{file.n_records()}")
  println("version: \{file.version()}")
  println("")

  // ------------------------------------------------------------------
  // 3. Show all records (name, sequence, type, checksum)
  // ------------------------------------------------------------------
  println("=== 3. Records ===")
  let records = file.records()
  for i in 0..0, C->1, G->2, T->3)
  // ------------------------------------------------------------------
  println("=== 4. Base encoding (A->0, C->1, G->2, T->3) ===")
  let bases = ['A', 'C', 'G', 'T']
  for i in 0.. \{code}")
  }
  // Round-trip: code back to base.
  println("Reverse (code -> base):")
  for c in 0..<4 {
    let base = @bio.xdna_code_to_base(c)
    println("  \{c} -> \{base}")
  }
  // Invalid base handling.
  let bad_code = @bio.xdna_base_to_code('N')
  println("  N -> \{bad_code} (invalid, returns -1)")
  println("")

  // ------------------------------------------------------------------
  // 5. Pack/unpack round-trip for a DNA sequence
  // ------------------------------------------------------------------
  println("=== 5. Pack/unpack round-trip ===")
  let dna = "ATGCATGC"
  let packed = @bio.xdna_pack_sequence(dna)
  println("Sequence: \{dna}")
  println("Packed bytes (\{packed.length()} byte(s)):")
  let pack_sb = StringBuilder()
  pack_sb.write_string("  ")
  for i in 0.. 0 {
      pack_sb.write_string(", ")
    }
    pack_sb.write_string(packed[i].to_string())
  }
  println(pack_sb.to_string())
  let unpacked = @bio.xdna_unpack_sequence(packed, dna.length())
  println("Unpacked: \{unpacked}")
  println("Round-trip OK: \{unpacked == dna}")
  println("")

  // ------------------------------------------------------------------
  // 6. Serialize to bytes and back, verify
  // ------------------------------------------------------------------
  println("=== 6. Serialize to bytes and back ===")
  let bytes = @bio.xdna_to_bytes(file)
  println("Serialized to \{bytes.length()} byte(s)")
  // Show first 20 bytes as a preview.
  let preview_len = if bytes.length() < 20 { bytes.length() } else { 20 }
  let byte_sb = StringBuilder()
  byte_sb.write_string("  first ")
  byte_sb.write_string(preview_len.to_string())
  byte_sb.write_string(" byte(s): ")
  for i in 0.. 0 {
      byte_sb.write_string(", ")
    }
    byte_sb.write_string(bytes[i].to_string())
  }
  println(byte_sb.to_string())
  let file2 = @bio.xdna_from_bytes(bytes)
  println(
    "Deserialized: n_records=\{file2.n_records()}, version=\{file2.version()}",
  )
  let recs2 = file2.records()
  let mut byte_ok = true
  if recs2.length() != records.length() {
    byte_ok = false
  } else {
    for i in 0..