Public/Import-PCSecrets.ps1

function Import-PCSecrets {
    <#
    .SYNOPSIS
        Loads secrets into environment variables for the current session.

    .DESCRIPTION
        Reads secrets from the store and sets corresponding environment variables.
        This makes secrets available to tools that read $env:OPENAI_API_KEY etc.

        Environment variable names are resolved in order:
        1. Explicit 'envVar' property in the secret entry
        2. Default mapping (known services)
        3. Generated: SERVICE_NAME_API_KEY (uppercase, hyphens → underscores)

    .PARAMETER Name
        Optional list of specific service names to import. Imports all if omitted.

    .PARAMETER PassThru
        Output a summary of what was imported.

    .OUTPUTS
        [PSCustomObject[]] If -PassThru, returns objects with Name, EnvVar, Status.

    .EXAMPLE
        Import-PCSecrets
        # Sets all configured secrets as env vars

    .EXAMPLE
        Import-PCSecrets -Name 'openai', 'brave-search'
        # Sets only those two

    .EXAMPLE
        Import-PCSecrets -PassThru | Format-Table
        # Name EnvVar Status
        # ---- ------ ------
        # openai OPENAI_API_KEY Imported
        # brave-search BRAVE_SEARCH_API_KEY Imported
    #>

    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param(
        [Parameter(Position = 0)]
        [string[]]$Name,

        [Parameter()]
        [switch]$PassThru
    )

    $secrets = Read-PCSecretsFile
    $results = @()

    $servicesToImport = if ($Name) { $Name } else { @($secrets.Keys) }

    foreach ($service in $servicesToImport) {
        $entry = $secrets[$service]
        $envVarName = Resolve-PCEnvVarName -Name $service -SecretEntry $entry
        $status = 'Skipped'

        if ($entry -and $entry['apiKey']) {
            [Environment]::SetEnvironmentVariable($envVarName, $entry['apiKey'], 'Process')
            $status = 'Imported'
            Write-Verbose "Set `$env:$envVarName from secret '$service'"
        }
        elseif (-not $entry) {
            $status = 'NotFound'
            Write-Warning "Secret '$service' not found in store."
        }
        else {
            # Entry exists but no apiKey — check for other properties
            # For multi-property services (e.g., azure with speechKey + region),
            # import the first key-like property
            $keyProp = $entry.Keys | Where-Object { $_ -ne 'envVar' } | Select-Object -First 1
            if ($keyProp -and $entry[$keyProp]) {
                [Environment]::SetEnvironmentVariable($envVarName, $entry[$keyProp], 'Process')
                $status = 'Imported'
                Write-Verbose "Set `$env:$envVarName from secret '$service.$keyProp'"
            }
            else {
                $status = 'Empty'
            }
        }

        $results += [PSCustomObject]@{
            Name   = $service
            EnvVar = $envVarName
            Status = $status
        }
    }

    if ($PassThru) {
        return $results
    }
}