## Test suite for Pacman Adapter functionality

import unittest
import std/[os, strutils, times, tables, options]
import ../src/nimpak/adapters/pacman

suite "Pacman Adapter Tests":
  setup:
    # Create a mock pacman database for testing
    let testDir = getTempDir() / "nip_test_pacman"
    let mockPacmanDb = testDir / "pacman_db"
    createDir(mockPacmanDb)

    # Create a mock package directory
    let mockPkg = mockPacmanDb / "test-package-1.0.0-1"
    createDir(mockPkg)

    # Create mock desc file
    let descContent = """%NAME%
test-package

%VERSION%
1.0.0-1

%DESC%
A test package for NIP pacman integration

%ARCH%
x86_64

%URL%
https://example.com/test-package

%LICENSE%
MIT

%DEPENDS%
glibc
bash

%OPTDEPENDS%
optional-dep: for optional functionality

%INSTALLDATE%
1640995200

%REASON%
1

%SIZE%
1048576

%PACKAGER%
Test Packager <test@example.com>

%BUILDDATE%
1640995100
"""

    writeFile(mockPkg / "desc", descContent)

    # Create mock files file
    let filesContent = """%FILES%
usr/
usr/bin/
usr/bin/test-package
usr/share/
usr/share/doc/
usr/share/doc/test-package/
usr/share/doc/test-package/README
"""

    writeFile(mockPkg / "files", filesContent)

  teardown:
    if dirExists(testDir):
      removeDir(testDir)

  test "Initialize pacman adapter":
    let adapter = initPacmanAdapter(mockPacmanDb)
    check adapter.database.dbPath == mockPacmanDb
    check len(adapter.database.packages) == 0

  test "Load pacman database":
    var adapter = initPacmanAdapter(mockPacmanDb)
    let result = adapter.loadPacmanDatabase()

    check result.isOk
    check len(adapter.database.packages) == 1
    check "test-package" in adapter.database.packages

  test "Parse package information":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let pkgOpt = adapter.getPackage("test-package")
    check pkgOpt.isSome

    let pkg = pkgOpt.get()
    check pkg.name == "test-package"
    check pkg.version == "1.0.0-1"
    check pkg.description == "A test package for NIP pacman integration"
    check pkg.architecture == "x86_64"
    check pkg.url == "https://example.com/test-package"
    check pkg.licenses == @["MIT"]
    check pkg.depends == @["glibc", "bash"]
    check pkg.installSize == 1048576

  test "List packages":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let packages = adapter.listPackages()
    check packages.len == 1
    check packages[0].name == "test-package"

  test "Search packages":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let results = adapter.searchPackages("test")
    check results.len == 1
    check results[0].name == "test-package"

    let noResults = adapter.searchPackages("nonexistent")
    check noResults.len == 0

  test "Convert to NIP package format":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let pkgOpt = adapter.getPackage("test-package")
    check pkgOpt.isSome

    let pacmanPkg = pkgOpt.get()
    let nipPkg = convertToNipPackage(pacmanPkg)

    check nipPkg.name == "test-package"
    check nipPkg.version == "1.0.0-1"
    check nipPkg.description == "A test package for NIP pacman integration"
    check nipPkg.dependencies.len == 2
    check nipPkg.dependencies[0].name == "glibc"
    check nipPkg.dependencies[1].name == "bash"

  test "Get package info":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let info = adapter.getPackageInfo("test-package")
    check "test-package 1.0.0-1" in info
    check "A test package for NIP pacman integration" in info
    check "Dependencies: glibc, bash" in info

  test "Get system statistics":
    var adapter = initPacmanAdapter(mockPacmanDb)
    discard adapter.loadPacmanDatabase()

    let stats = adapter.getSystemStats()
    check stats.totalPackages == 1
    check stats.totalSize == 1048576

suite "Pacman CLI Integration":
  test "Pacman list command (mock)":
    # This would test against a real pacman database if available
    # For now, we test the error handling
    let result = nipPacmanList()
    # Should either succeed or fail gracefully
    check result.isOk or result.isErr

  test "Pacman info command (mock)":
    let result = nipPacmanInfo("nonexistent-package")
    # Should handle non-existent packages gracefully
    check result.isOk or result.isErr