## Test graft command with USE flags

import src/nimpak/[config, use_flags]
import src/nimpak/cli/graft_commands_enhanced
import std/[strformat]

echo "🧪 Testing Graft Command with USE Flags"
echo "========================================"
echo ""

# Test 1: Parse USE flags from command line
echo "Test 1: Parsing USE flags"
echo "-------------------------"
let useFlagsStr = "+wayland -X +lto +gtk"
echo fmt"Input: {useFlagsStr}"

try:
  let flags = parseUseFlagLine(useFlagsStr)
  echo "Parsed successfully:"
  for flag in flags:
    echo fmt"  {formatUseFlag(flag)}"
except UseFlagParseError as e:
  echo fmt"Error: {e.msg}"

echo ""

# Test 2: Get effective USE flags for a package
echo "Test 2: Effective USE flags resolution"
echo "---------------------------------------"
var testConfig = defaultConfig()

# Add some global flags
testConfig.globalUseFlags = parseUseFlagLine("+lto +ipv6 +ssl")

# Add package-specific flags
testConfig.packageConfigs["firefox"] = PackageConfig(
  name: "firefox",
  useFlags: parseUseFlagLine("+wayland -X"),
  compilerFlags: CompilerFlags()
)

echo "Global flags:  " & formatUseFlags(testConfig.globalUseFlags)
echo "Package flags: " & formatUseFlags(testConfig.packageConfigs["firefox"].useFlags)

let effectiveFlags = getEffectiveUseFlags(testConfig, "firefox")
echo "Effective:     " & formatUseFlags(effectiveFlags)

echo ""

# Test 3: Merge with CLI flags
echo "Test 3: Merging CLI flags"
echo "-------------------------"
let cliFlags = parseUseFlagLine("+lto +gtk -ssl")
echo "CLI flags:     " & formatUseFlags(cliFlags)

let mergedFlags = mergeUseFlags(effectiveFlags, cliFlags)
echo "After merge:   " & formatUseFlags(mergedFlags)

echo ""

# Test 4: Generate variant hash
echo "Test 4: Variant hash generation"
echo "--------------------------------"
let variantHash = useFlagsToHash(mergedFlags)
echo fmt"Variant hash: {variantHash}"
echo ""
echo "This would create: /Programs/Firefox/120.0-{variantHash}/"

echo ""

# Test 5: Validate USE flags
echo "Test 5: USE flag validation"
echo "---------------------------"
var testFlags = parseUseFlagLine("+systemd +dinit")  # Conflicting init systems
for i in 0..<testFlags.len:
  if testFlags[i].name in ["systemd", "dinit"]:
    testFlags[i].category = "init"

let categories = getStandardCategories()
let (valid, errors) = validateUseFlags(testFlags, categories)
echo fmt"Flags: {formatUseFlags(testFlags)}"
echo fmt"Valid: {valid}"
if not valid:
  echo "Errors:"
  for err in errors:
    echo fmt"  - {err}"

echo ""

# Test 6: Display functions
echo "Test 6: Display formatting"
echo "--------------------------"
let displayFlags = parseUseFlagLine("+wayland +lto +gtk +nexus-fleet +nipcells")
displayUseFlags(displayFlags, "Example Package USE Flags")

let compFlags = CompilerFlags(
  cflags: "-O3 -march=native -flto",
  cxxflags: "-O3 -march=native -flto",
  ldflags: "-Wl,-O1 -flto",
  makeflags: "-j8"
)
displayCompilerFlags(compFlags, "Example Compiler Flags")

echo "✅ All USE flag integration tests completed!"
echo ""
echo "Next: Integrate with actual graft command"
echo "  nip graft firefox --use=\"+wayland -X +lto\""
