## test_variant_database.nim
## Tests for variant database operations

import std/[unittest, tables, times, os]
import ../src/nimpak/variant_database
import ../src/nimpak/variant_types

suite "Variant Database Operations":

  setup:
    # Create temporary test database
    let testDbPath = "/tmp/test_variant_db_" & $epochTime().int
    let db = newVariantDatabase(testDbPath)

  teardown:
    # Clean up test database
    try:
      removeDir(testDbPath)
    except:
      discard

  test "Create and save variant record":
    var domains = initTable[string, seq[string]]()
    domains["init"] = @["dinit"]
    domains["graphics"] = @["wayland", "vulkan"]

    let toolchain = newToolchainInfo("gcc", "13.2.0")
    let target = newTargetInfo("x86_64", "linux")

    let success = db.createVariantRecord(
      "blake2b-abc123def456",
      "firefox",
      "118.0",
      domains,
      "/Programs/firefox/118.0-abc123def456",
      toolchain,
      target
    )

    check success == true
    check db.countVariants() == 1
    check db.hasVariant("blake2b-abc123def456")

  test "Query variant by fingerprint":
    var domains = initTable[string, seq[string]]()
    domains["init"] = @["systemd"]

    let toolchain = newToolchainInfo("clang", "16.0.0")
    let target = newTargetInfo("aarch64", "linux")

    discard db.createVariantRecord(
      "blake2b-xyz789abc012",
      "vim",
      "9.0.2",
      domains,
      "/Programs/vim/9.0.2-xyz789abc012",
      toolchain,
      target
    )

    let result = db.queryVariantByFingerprint("blake2b-xyz789abc012")

    check result.found == true
    check result.record.packageName == "vim"
    check result.record.version == "9.0.2"
    check result.record.fingerprint == "blake2b-xyz789abc012"
    check result.record.toolchain.name == "clang"
    check result.record.target.arch == "aarch64"

  test "Delete variant record":
    var domains = initTable[string, seq[string]]()
    domains["init"] = @["dinit"]

    let toolchain = newToolchainInfo("gcc", "13.2.0")
    let target = newTargetInfo("x86_64", "linux")

    discard db.createVariantRecord(
      "blake2b-delete001",
      "htop",
      "3.2.2",
      domains,
      "/Programs/htop/3.2.2-delete001",
      toolchain,
      target
    )

    check db.hasVariant("blake2b-delete001") == true

    let deleted = db.deleteVariantRecord("blake2b-delete001")

    check deleted == true
    check db.hasVariant("blake2b-delete001") == false

  test "Persistence - save and load":
    var domains = initTable[string, seq[string]]()
    domains["init"] = @["dinit"]
    domains["graphics"] = @["wayland"]

    let toolchain = newToolchainInfo("gcc", "13.2.0")
    let target = newTargetInfo("x86_64", "linux")

    discard db.createVariantRecord(
      "blake2b-persist001",
      "firefox",
      "118.0",
      domains,
      "/Programs/firefox/118.0-persist001",
      toolchain,
      target
    )

    # Create a new database instance and load
    let db2 = newVariantDatabase(db.dbPath)
    db2.loadVariants()

    check db2.countVariants() == 1
    check db2.hasVariant("blake2b-persist001")

    let result = db2.queryVariantByFingerprint("blake2b-persist001")
    check result.found == true
    check result.record.packageName == "firefox"

when isMainModule:
  echo "Running variant database tests..."
