## Test suite for Nippels UTCP Protocol Integration (Task 8.5)
##
## Tests the integration of UTCP addressing with NippelManager

import std/[unittest, os, times, strutils, json]
import ../src/nimpak/nippels

suite "Nippels UTCP Protocol Integration (Task 8.5)":
  var manager: NippelManager
  let testRoot = getTempDir() / "nippels_utcp_test_" & $epochTime().int

  setup:
    createDir(testRoot)
    manager = newNippelManager(testRoot)

  teardown:
    for name in manager.getActiveNippels():
      discard manager.deactivateNippel(name)

    if dirExists(testRoot):
      removeDir(testRoot)

  test "Create Nippel assigns UTCP address":
    let result = manager.createNippel("test-utcp", Homestation)
    check result.isOk

    if result.isOk:
      let nippel = result.value

      # Verify UTCP address is assigned
      check nippel.utcpAddress.scheme.len > 0
      check nippel.utcpAddress.host.len > 0
      check nippel.utcpAddress.resource.len > 0

      echo "   ✅ UTCP address assigned:"
      echo "      Scheme: ", nippel.utcpAddress.scheme
      echo "      Host: ", nippel.utcpAddress.host
      echo "      Resource: ", nippel.utcpAddress.resource

  test "UTCP address contains correct resource path":
    let result = manager.createNippel("my-app", Workstation)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      check nippel.utcpAddress.resource == "nippel/my-app"
      echo "   ✅ Resource path correct: ", nippel.utcpAddress.resource

  test "UTCP address uses utcp:// scheme":
    let result = manager.createNippel("test-scheme", Server)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      check nippel.utcpAddress.scheme == "utcp://"
      echo "   ✅ Scheme correct: ", nippel.utcpAddress.scheme

  test "UTCP address can be formatted as string":
    let result = manager.createNippel("test-format", Homestation)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      let formatted = formatUTCPAddress(nippel.utcpAddress)

      check formatted.len > 0
      check formatted.startsWith("utcp://")
      check "nippel/test-format" in formatted

      echo "   ✅ Formatted address: ", formatted

  test "UTCP address is stored in configuration":
    let result = manager.createNippel("test-config", Homestation)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      let configPath = nippel.cellRoot / "cell.json"
      check fileExists(configPath)

      # Read and verify configuration contains UTCP address
      let config = parseJson(readFile(configPath))
      check config.hasKey("network")
      check config["network"].hasKey("utcp_address")

      let utcpAddr = config["network"]["utcp_address"].getStr()
      check utcpAddr.len > 0
      check utcpAddr.startsWith("utcp://")

      echo "   ✅ UTCP address in config: ", utcpAddr

  test "Multiple Nippels have unique UTCP addresses":
    let result1 = manager.createNippel("app-1", Homestation)
    let result2 = manager.createNippel("app-2", Workstation)
    let result3 = manager.createNippel("app-3", Server)

    check result1.isOk
    check result2.isOk
    check result3.isOk

    if result1.isOk and result2.isOk and result3.isOk:
      let nippel1 = result1.value
      let nippel2 = result2.value
      let nippel3 = result3.value

      # Verify each has unique resource path
      check nippel1.utcpAddress.resource == "nippel/app-1"
      check nippel2.utcpAddress.resource == "nippel/app-2"
      check nippel3.utcpAddress.resource == "nippel/app-3"

      # Verify all are different
      check nippel1.utcpAddress.resource != nippel2.utcpAddress.resource
      check nippel2.utcpAddress.resource != nippel3.utcpAddress.resource
      check nippel1.utcpAddress.resource != nippel3.utcpAddress.resource

      echo "   ✅ All Nippels have unique UTCP addresses"

  test "UTCP address uses localhost as default host":
    let result = manager.createNippel("test-host", Homestation)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      check nippel.utcpAddress.host == "localhost"
      echo "   ✅ Default host is localhost"

  test "UTCP address format is AI-addressable":
    let result = manager.createNippel("ai-test", Homestation)
    check result.isOk

    if result.isOk:
      let nippel = result.value
      let formatted = formatUTCPAddress(nippel.utcpAddress)

      # Verify format is suitable for AI addressing
      # Format: utcp://host/nippel/name
      check formatted.contains("://")
      check formatted.contains("/nippel/")
      check formatted.contains("ai-test")

      echo "   ✅ AI-addressable format: ", formatted

  test "UTCP address enables resource discovery":
    let result = manager.createNippel("discoverable", Satellite)
    check result.isOk

    if result.isOk:
      let nippel = result.value

      # Verify address components enable discovery
      check nippel.utcpAddress.scheme.len > 0  # Protocol
      check nippel.utcpAddress.host.len > 0    # Location
      check nippel.utcpAddress.resource.len > 0  # Resource identifier

      echo "   ✅ Resource discoverable via UTCP"

  test "UTCP address supports different profiles":
    let profiles = [Workstation, Homestation, Satellite, NetworkIOT, Server]

    for i, profile in profiles:
      let name = "test-profile-" & $i
      let result = manager.createNippel(name, profile)
      check result.isOk

      if result.isOk:
        let nippel = result.value
        check nippel.utcpAddress.resource == "nippel/" & name

    echo "   ✅ UTCP addressing works for all profiles"

echo "✅ All Task 8.5 tests completed"
