## Tests for Repository Hierarchy & Configuration
## Task 1: Config Schema (Sub-Feature 1, Milestone m1)

import std/[unittest, os, strutils, tables]
import ../src/nimpak/repo/config
import ../src/nimpak/repo/resolver
import ../src/nimpak/repo/overrides
import ../src/nip/types

suite "Repository Configuration Parsing":
  
  test "Parse repos.kdl with Native, Git, and Graft repos":
    let repoConfigPath = getAppDir() / "fixtures" / "repos.kdl"
    
    let repos = parseRepoConfig(repoConfigPath)
    
    # Should have 3 repos
    check repos.len == 3
    
    # Check Native repo
    let nativeRepo = repos[0]
    check nativeRepo.name == "nexus-core"
    check nativeRepo.kind == Native
    check nativeRepo.url == "https://repo.nexus.nexus/v1"
    check nativeRepo.key == "ed25519-test-key-abc123"
    check nativeRepo.priority == 100
    
    # Check Git repo
    let gitRepo = repos[1]
    check gitRepo.name == "nexus-toolkit"
    check gitRepo.kind == Git
    check gitRepo.url == "https://git.maiwald.work/Nexus/NexusToolKit.git"
    check gitRepo.branch == "main"
    check gitRepo.priority == 90
    
    # Check Graft repo
    let graftRepo = repos[2]
    check graftRepo.name == "nix-unstable"
    check graftRepo.kind == Graft
    check graftRepo.backend == Nix
    check graftRepo.url == "https://nixos.org/channels/nixpkgs-unstable"
    check graftRepo.priority == 10

  test "Parse empty repos.kdl":
    let tempFile = getTempDir() / "empty_repos.kdl"
    writeFile(tempFile, "")
    
    let repos = parseRepoConfig(tempFile)
    check repos.len == 0
    
    removeFile(tempFile)

  test "Invalid repo type raises error":
    let tempFile = getTempDir() / "invalid_repos.kdl"
    writeFile(tempFile, """
repo "bad" {
    type "invalid"
    url "http://example.com"
}
""")
    
    expect ValueError:
      discard parseRepoConfig(tempFile)
    
    removeFile(tempFile)

suite "Resolution Engine":
  
  test "Sort repos by priority (highest first)":
    let repos = @[
      RepoConfig(name: "low", priority: 10),
      RepoConfig(name: "high", priority: 100),
      RepoConfig(name: "medium", priority: 50)
    ]
    
    let sorted = sortReposByPriority(repos)
    
    check sorted[0].name == "high"
    check sorted[1].name == "medium"
    check sorted[2].name == "low"
  
  test "User override takes precedence":
    let repos = @[
      RepoConfig(name: "native", kind: Native, priority: 100)
    ]
    
    var ctx = initResolutionContext("nginx", repos)
    
    # Add user override
    let userFragment = Fragment(
      id: PackageId(name: "nginx", version: "1.25.0-custom", stream: Dev),
      source: Source(hash: "user-override-hash")
    )
    ctx.addUserOverride("nginx", userFragment)
    
    # Resolve should return user override
    let result = ctx.resolvePackage()
    
    check result.found == true
    check result.source.name == "user-override"
    check result.version == "1.25.0-custom"
    check result.cid == "user-override-hash"
  
  test "Resolution trace formatting":
    let repos = @[
      RepoConfig(name: "core", kind: Native, priority: 100),
      RepoConfig(name: "git", kind: Git, priority: 50)
    ]
    
    let ctx = initResolutionContext("htop", repos)
    let result = ResolutionResult(found: false, packageName: "htop")
    
    let trace = formatResolutionTrace(ctx, result)
    
    check trace.contains("NOT FOUND")
    check trace.contains("core")
    check trace.contains("git")

suite "User Override Hooks":
  
  test "Load user overrides from directory":
    let overrideDir = getAppDir() / "fixtures" / "overrides"
    let overrides = loadUserOverrides(overrideDir)
    
    check len(overrides) == 1
    check overrides.hasKey("nginx")
    
    let nginxOverride = overrides["nginx"]
    check nginxOverride.id.name == "nginx"
    check nginxOverride.id.version == "1.25.0-custom"
    check nginxOverride.id.stream == Dev
    check nginxOverride.source.hash == "xxh3-user-override-nginx-abc123"
    check nginxOverride.source.url == "file:///home/user/nginx-custom-build"
    check nginxOverride.source.sourceMethod == Local
  
  test "User override takes absolute precedence in resolution":
    let repos = @[
      RepoConfig(name: "native", kind: Native, priority: 100),
      RepoConfig(name: "git", kind: Git, priority: 50)
    ]
    
    var ctx = initResolutionContext("nginx", repos)
    
    # Load overrides
    let overrideDir = getAppDir() / "fixtures" / "overrides"
    let overrides = loadUserOverrides(overrideDir)
    
    for pkg, fragment in overrides:
      ctx.addUserOverride(pkg, fragment)
    
    # Resolve should return user override
    let result = ctx.resolvePackage()
    
    check result.found == true
    check result.source.name == "user-override"
    check result.version == "1.25.0-custom"
    check result.cid == "xxh3-user-override-nginx-abc123"

when isMainModule:
  echo "Repository Hierarchy Tests"
