## test_container_builds.nim
## End-to-end tests for container-based builds

import std/[unittest, os, osproc, strutils]

proc hasDocker(): bool =
  ## Check if Docker is available
  try:
    let output = execProcess("docker --version 2>&1")
    return output.contains("Docker version")
  except:
    return false

proc hasPodman(): bool =
  ## Check if Podman is available
  try:
    let output = execProcess("podman --version 2>&1")
    return output.contains("podman version")
  except:
    return false

proc hasContainerRuntime(): bool =
  ## Check if any container runtime is available
  return hasPodman() or hasDocker()

suite "Container Runtime Detection":
  test "Detect available runtime":
    if not hasContainerRuntime():
      skip()

    check:
      hasContainerRuntime() == true

  test "Docker or Podman available":
    if not hasContainerRuntime():
      skip()

    let dockerAvail = hasDocker()
    let podmanAvail = hasPodman()

    check:
      dockerAvail or podmanAvail

suite "Container Image Operations":
  test "Pull Gentoo image":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    echo "Pulling Gentoo stage3 image (this may take a while)..."
    let output = execProcess(runtime & " pull gentoo/stage3:latest 2>&1")

    check:
      output.len > 0

  test "Pull Nix image":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    echo "Pulling Nix image..."
    let output = execProcess(runtime & " pull nixos/nix:latest 2>&1")

    check:
      output.len > 0

suite "Container Build Tests":
  test "Simple container execution":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    # Test basic container execution
    let output = execProcess(runtime & " run --rm alpine:latest echo 'Hello from container' 2>&1")

    check:
      output.contains("Hello from container")

  test "Container with volume mount":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"
    let tempDir = getTempDir() / "test-container-mount"
    createDir(tempDir)

    writeFile(tempDir / "test.txt", "test content")

    # Mount directory and read file
    let cmd = runtime & " run --rm -v " & tempDir & ":/mnt alpine:latest cat /mnt/test.txt 2>&1"
    let output = execProcess(cmd)

    check:
      output.contains("test content")

    removeDir(tempDir)

  test "Container environment variables":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    let cmd = runtime & " run --rm -e TEST_VAR=hello alpine:latest sh -c 'echo $TEST_VAR' 2>&1"
    let output = execProcess(cmd)

    check:
      output.contains("hello")

suite "Gentoo Container Build":
  test "Gentoo emerge available in container":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    echo "Testing Gentoo emerge in container..."
    let cmd = runtime & " run --rm gentoo/stage3:latest emerge --version 2>&1"
    let output = execProcess(cmd)

    check:
      output.contains("Portage") or output.len > 0

  test "Gentoo portage tree available":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    let cmd = runtime & " run --rm gentoo/stage3:latest ls /var/db/repos/gentoo 2>&1"
    let output = execProcess(cmd)

    # May not have portage tree in minimal image, that's ok
    check:
      output.len >= 0

suite "Nix Container Build":
  test "Nix available in container":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    echo "Testing Nix in container..."
    let cmd = runtime & " run --rm nixos/nix:latest nix --version 2>&1"
    let output = execProcess(cmd)

    check:
      output.contains("nix") or output.len > 0

  test "Nix store accessible":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    let cmd = runtime & " run --rm nixos/nix:latest ls /nix/store 2>&1"
    let output = execProcess(cmd)

    # Store should exist even if empty
    check:
      output.len >= 0

suite "Container Cleanup":
  test "Remove test containers":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    # Clean up any stopped containers
    discard execProcess(runtime & " container prune -f 2>&1")

    check:
      true  # Cleanup always succeeds

  test "List images":
    if not hasContainerRuntime():
      skip()

    let runtime = if hasPodman(): "podman" else: "docker"

    let output = execProcess(runtime & " images 2>&1")

    check:
      output.len > 0

echo ""
if hasContainerRuntime():
  echo "✅ Container build tests completed"
  if hasPodman():
    echo "   Using: Podman"
  elif hasDocker():
    echo "   Using: Docker"
else:
  echo "⚠️  No container runtime available - tests skipped"
  echo "   Install Docker or Podman to run container tests"
