## test_variant_config.nim
## Tests for variant system configuration (Task 13)

import std/[unittest, os, strutils, sequtils, tables]
import ../src/nimpak/config

suite "Variant Configuration System":

  test "Default config includes variant settings":
    let cfg = defaultConfig()

    # Check variant fields exist and have defaults
    check cfg.defaultToolchain.len > 0
    check cfg.defaultTarget.len > 0
    check cfg.profileSearchPaths.len > 0

    # Check default values
    check cfg.defaultToolchain == "gcc-13.2.0"
    check cfg.defaultTarget == "x86_64-linux"
    check cfg.profileSearchPaths.len == 3

  test "Default profile search paths are set":
    let cfg = defaultConfig()

    # Should have system, user, and project paths
    check "/etc/nip/profiles" in cfg.profileSearchPaths
    check cfg.profileSearchPaths.anyIt(".nip/profiles" in it)
    check ".kiro/nip/profiles" in cfg.profileSearchPaths

  test "Parse default-toolchain from config":
    let testConfig = """
default-toolchain = "clang-17.0.0"
"""
    let tempFile = getTempDir() / "test-nip-config-toolchain.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.defaultToolchain == "clang-17.0.0"

    removeFile(tempFile)

  test "Parse default-target from config":
    let testConfig = """
default-target = "aarch64-linux"
"""
    let tempFile = getTempDir() / "test-nip-config-target.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.defaultTarget == "aarch64-linux"

    removeFile(tempFile)

  test "Parse profile-search-paths with comma separator":
    let testConfig = """
profile-search-paths = "/opt/profiles,/usr/local/profiles,~/my-profiles"
"""
    let tempFile = getTempDir() / "test-nip-config-paths-comma.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.profileSearchPaths.len == 3
    check "/opt/profiles" in cfg.profileSearchPaths
    check "/usr/local/profiles" in cfg.profileSearchPaths
    check "~/my-profiles" in cfg.profileSearchPaths

    removeFile(tempFile)

  test "Parse profile-search-paths with colon separator":
    let testConfig = """
profile-search-paths = "/opt/profiles:/usr/local/profiles:~/my-profiles"
"""
    let tempFile = getTempDir() / "test-nip-config-paths-colon.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.profileSearchPaths.len == 3
    check "/opt/profiles" in cfg.profileSearchPaths
    check "/usr/local/profiles" in cfg.profileSearchPaths
    check "~/my-profiles" in cfg.profileSearchPaths

    removeFile(tempFile)

  test "Parse profile-search-paths handles whitespace":
    let testConfig = """
profile-search-paths = " /opt/profiles , /usr/local/profiles , ~/my-profiles "
"""
    let tempFile = getTempDir() / "test-nip-config-paths-whitespace.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.profileSearchPaths.len == 3
    check "/opt/profiles" in cfg.profileSearchPaths
    check "/usr/local/profiles" in cfg.profileSearchPaths
    check "~/my-profiles" in cfg.profileSearchPaths

    removeFile(tempFile)

  test "Parse all variant settings together":
    let testConfig = """
# Variant system settings
default-toolchain = "clang-17.0.0"
default-target = "aarch64-linux"
profile-search-paths = "/opt/profiles,/usr/local/profiles"
"""
    let tempFile = getTempDir() / "test-nip-config-all-variant.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.defaultToolchain == "clang-17.0.0"
    check cfg.defaultTarget == "aarch64-linux"
    check cfg.profileSearchPaths.len == 2
    check "/opt/profiles" in cfg.profileSearchPaths
    check "/usr/local/profiles" in cfg.profileSearchPaths

    removeFile(tempFile)

  test "Underscore variants of config keys work":
    let testConfig = """
default_toolchain = "gcc-14.0.0"
default_target = "riscv64-linux"
profile_search_paths = "/custom/path"
"""
    let tempFile = getTempDir() / "test-nip-config-underscore.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    check cfg.defaultToolchain == "gcc-14.0.0"
    check cfg.defaultTarget == "riscv64-linux"
    check "/custom/path" in cfg.profileSearchPaths

    removeFile(tempFile)

  test "Example config includes variant settings":
    let exampleConfig = generateExampleConfig()

    # Check that variant settings are documented
    check "default-toolchain" in exampleConfig
    check "default-target" in exampleConfig
    check "profile-search-paths" in exampleConfig

    # Check that comments explain the settings
    check "Variant System Configuration" in exampleConfig
    check "toolchain" in exampleConfig.toLower()
    check "target" in exampleConfig.toLower()

  test "Config merging preserves variant settings":
    # Create a global config
    let globalConfig = """
default-toolchain = "gcc-13.2.0"
default-target = "x86_64-linux"
"""
    let globalFile = getTempDir() / "test-nip-global.conf"
    writeFile(globalFile, globalConfig)

    # Create a user config that overrides
    let userConfig = """
default-toolchain = "clang-17.0.0"
profile-search-paths = "/my/custom/path"
"""
    let userFile = getTempDir() / "test-nip-user.conf"
    writeFile(userFile, userConfig)

    # Parse both
    let globalCfg = parseConfigFile(globalFile)
    let userCfg = parseConfigFile(userFile)

    # Verify global config
    check globalCfg.defaultToolchain == "gcc-13.2.0"
    check globalCfg.defaultTarget == "x86_64-linux"

    # Verify user config overrides toolchain but not target
    check userCfg.defaultToolchain == "clang-17.0.0"
    check "/my/custom/path" in userCfg.profileSearchPaths

    removeFile(globalFile)
    removeFile(userFile)

  test "Empty profile-search-paths handled gracefully":
    let testConfig = """
profile-search-paths = ""
"""
    let tempFile = getTempDir() / "test-nip-config-empty-paths.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)
    # Should fall back to defaults
    check cfg.profileSearchPaths.len >= 0

    removeFile(tempFile)

  test "Variant settings work with other config options":
    let testConfig = """
programs-dir = "/Programs"
verbose = true
default-toolchain = "gcc-13.2.0"
nix-enabled = true
default-target = "x86_64-linux"
profile-search-paths = "/etc/nip/profiles"
"""
    let tempFile = getTempDir() / "test-nip-config-mixed.conf"
    writeFile(tempFile, testConfig)

    let cfg = parseConfigFile(tempFile)

    # Check regular settings still work
    check cfg.programsDir == "/Programs"
    check cfg.verbose == true
    check cfg.adapters.hasKey("nix")
    if cfg.adapters.hasKey("nix"):
      check cfg.adapters["nix"].enabled == true

    # Check variant settings work
    check cfg.defaultToolchain == "gcc-13.2.0"
    check cfg.defaultTarget == "x86_64-linux"
    check "/etc/nip/profiles" in cfg.profileSearchPaths

    removeFile(tempFile)
