PaperinikDB.psm1

# PaperinikDB Module Loader
# Detects .NET version and loads appropriate assemblies

$libPath = "$PSScriptRoot\lib"

# Determine the best .NET runtime available
$runtimeVersion = [System.Environment]::Version
$targetFramework = if ($runtimeVersion.Major -ge 8) {
    'net8'
} elseif ($runtimeVersion.Major -ge 6) {
    'net6'
} else {
    # Fallback to root lib (backward compatibility)
    $null
}

# Set up native library path for duckdb.dll
# DuckDB.NET needs to find the native library at runtime
$nativeLibPath = if (Test-Path "$libPath\core") {
    "$libPath\core"
} else {
    $libPath
}

# Add native library path to PATH so DuckDB.NET can find duckdb.dll
if ($env:PATH -notlike "*$nativeLibPath*") {
    $env:PATH = "$nativeLibPath;$env:PATH"
}

# Load DuckDB.NET assemblies from appropriate location
if ($targetFramework -and (Test-Path "$libPath\$targetFramework\DuckDB.NET.Data.dll")) {
    # Load from versioned subdirectory
    Add-Type -Path "$libPath\$targetFramework\DuckDB.NET.Data.dll"
    Add-Type -Path "$libPath\$targetFramework\DuckDB.NET.Bindings.dll"
    Write-Verbose "Loaded DuckDB.NET assemblies from lib/$targetFramework (native from lib/core)"
} else {
    # Fallback to root lib directory (backward compatibility)
    Add-Type -Path "$libPath\DuckDB.NET.Data.dll"
    Add-Type -Path "$libPath\DuckDB.NET.Bindings.dll"
    Write-Verbose "Loaded DuckDB.NET assemblies from lib/"
}

. $PSScriptRoot\Public\New-DuckDBConnectionWithFunctions.ps1