## Test KDL Parser with NIP configuration

import src/nimpak/kdl_parser
import std/[options, strformat]

proc testBasicParsing() =
  echo "=== Testing Basic KDL Parsing ==="

  let kdlContent = """
    nip {
        programs-dir "/Programs"
        links-dir "/System/Links"
        auto-symlink true
        verbose false
    }
  """

  let doc = parseKdlString(kdlContent)
  echo fmt"✓ Parsed {doc.len} top-level nodes"

  let nipNode = doc.findNode("nip")
  if nipNode.isSome:
    let node = nipNode.get
    echo fmt"✓ Found 'nip' node with {node.children.len} children"

    # Access properties
    if node.hasProp("programs-dir"):
      let progDir = node.getPropString("programs-dir")
      echo fmt"  programs-dir: {progDir}"
    if node.hasProp("auto-symlink"):
      let autoLink = node.getPropBool("auto-symlink")
      echo fmt"  auto-symlink: {autoLink}"

proc testFileParsing() =
  echo "\n=== Testing File Parsing ==="

  try:
    let doc = parseKdlFile("examples/nip-config-kdl-valid.kdl")
    echo fmt"✓ Parsed examples/nip-config-kdl-valid.kdl: {doc.len} top-level nodes"

    let nipNode = doc.findNode("nip")
    if nipNode.isSome:
      let node = nipNode.get
      echo fmt"✓ Found 'nip' node with {node.children.len} children"

      # Find use-flags section
      let useFlagsNode = node.findChild("use-flags")
      if useFlagsNode.isSome:
        let flags = useFlagsNode.get
        echo fmt"✓ Found 'use-flags' section with {flags.children.len} categories"

        # List some categories
        for i, category in flags.children:
          if i < 5:  # Show first 5
            echo fmt"  - {category.name}: {category.children.len} flags"

      # Find profiles section
      let profilesNode = node.findChild("profiles")
      if profilesNode.isSome:
        let profiles = profilesNode.get
        echo fmt"✓ Found 'profiles' section"

        # Find specific profiles
        let perfProfile = profiles.findChild("profile")
        if perfProfile.isSome:
          echo "  Found profile definitions"

      # Find package configurations
      let packages = node.findChildren("package")
      echo fmt"✓ Found {packages.len} package configurations"
      for i, pkg in packages:
        if i < 3 and pkg.args.len > 0:  # Show first 3
          let pkgName = pkg.getArgString(0)
          echo fmt"  - {pkgName}"

  except IOError as e:
    echo fmt"⚠ Could not read file: {e.msg}"
  except ValueError as e:
    echo fmt"✗ Parse error: {e.msg}"

proc testNestedStructures() =
  echo "\n=== Testing Nested Structures ==="

  let kdlContent = """
    package "firefox" {
        description "Firefox web browser"

        use-flags {
            +wayland
            -X
            +lto
        }

        compiler {
            CFLAGS "-O3 -march=native"
            MAKEFLAGS "-j8"
        }
    }
  """

  let doc = parseKdlString(kdlContent)
  let pkgNode = doc.findNode("package")

  if pkgNode.isSome:
    let pkg = pkgNode.get
    let pkgName = pkg.getArgString(0)
    echo fmt"✓ Package: {pkgName}"

    let descNode = pkg.findChild("description")
    if descNode.isSome and descNode.get.args.len > 0:
      echo fmt"  Description: {descNode.get.getArgString(0)}"

    let useFlagsNode = pkg.findChild("use-flags")
    if useFlagsNode.isSome:
      echo fmt"  USE flags: {useFlagsNode.get.children.len} flags defined"

    let compilerNode = pkg.findChild("compiler")
    if compilerNode.isSome:
      echo fmt"  Compiler settings: {compilerNode.get.children.len} options"

when isMainModule:
  echo "KDL Parser Test Suite for NIP\n"

  testBasicParsing()
  testFileParsing()
  testNestedStructures()

  echo "\n✅ All tests completed!"
