## Test USE flag parsing

import src/nimpak/[config, use_flags]
import std/[tables, strformat, strutils]

echo "🧪 Testing USE Flag Parser"
echo "=========================="
echo ""

# Test 1: Parse single USE flags
echo "Test 1: Parse single USE flags"
echo "-------------------------------"
let flag1 = parseUseFlag("+lto")
echo fmt"  +lto -> name: {flag1.name}, enabled: {flag1.enabled}"

let flag2 = parseUseFlag("-systemd")
echo fmt"  -systemd -> name: {flag2.name}, enabled: {flag2.enabled}"

let flag3 = parseUseFlag("wayland")
echo fmt"  wayland -> name: {flag3.name}, enabled: {flag3.enabled}"
echo ""

# Test 2: Parse USE flag line
echo "Test 2: Parse USE flag line"
echo "----------------------------"
let flags = parseUseFlagLine("+lto -debug +wayland -X +gtk")
echo "  Input: +lto -debug +wayland -X +gtk"
echo "  Parsed:"
for flag in flags:
  echo fmt"    {formatUseFlag(flag)}"
echo ""

# Test 3: Merge USE flags
echo "Test 3: Merge USE flags"
echo "-----------------------"
let base = parseUseFlagLine("+lto +debug +X")
let override = parseUseFlagLine("-debug +wayland -X")
let merged = mergeUseFlags(base, override)
echo "  Base:     " & formatUseFlags(base)
echo "  Override: " & formatUseFlags(override)
echo "  Merged:   " & formatUseFlags(merged)
echo ""

# Test 4: Get effective USE flags
echo "Test 4: Get effective USE flags"
echo "--------------------------------"
var testConfig = defaultConfig()

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

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

let effectiveFlags = getEffectiveUseFlags(testConfig, "firefox")
echo "  Global:   " & formatUseFlags(testConfig.globalUseFlags)
echo "  Package:  " & formatUseFlags(testConfig.packageConfigs["firefox"].useFlags)
echo "  Effective: " & formatUseFlags(effectiveFlags)
echo ""

# Test 5: USE flag hash
echo "Test 5: USE flag hash (for variants)"
echo "-------------------------------------"
let variant1 = parseUseFlagLine("+wayland +lto +gtk")
let variant2 = parseUseFlagLine("+X +lto +qt")
echo "  Variant 1: " & formatUseFlags(variant1)
echo "  Hash:      " & useFlagsToHash(variant1)
echo "  Variant 2: " & formatUseFlags(variant2)
echo "  Hash:      " & useFlagsToHash(variant2)
echo ""

# Test 6: Standard categories
echo "Test 6: Standard categories"
echo "---------------------------"
let categories = getStandardCategories()
for name, cat in categories:
  echo "  " & name & ": " & cat.description
  echo "    Exclusive: " & $cat.exclusive
  echo "    Options: " & cat.options.join(", ")
echo ""

# Test 7: Validate exclusive categories
echo "Test 7: Validate exclusive categories"
echo "--------------------------------------"
var testFlags = parseUseFlagLine("+systemd +dinit")
# Set categories
for i in 0..<testFlags.len:
  if testFlags[i].name in ["systemd", "dinit", "openrc"]:
    testFlags[i].category = "init"

let (valid, errors) = validateUseFlags(testFlags, categories)
echo "  Flags: " & formatUseFlags(testFlags)
echo "  Valid: " & $valid
if errors.len > 0:
  echo "  Errors:"
  for err in errors:
    echo "    - " & err
echo ""

# Test 8: Display functions
echo "Test 8: Display functions"
echo "-------------------------"
let displayFlags = parseUseFlagLine("+lto +wayland -X -systemd +gtk")
displayUseFlags(displayFlags, "Example 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 tests completed!"
