Public/Set-ActiveBudget.ps1

function Set-ActiveBudget {
    <#
    .SYNOPSIS
        Sets the active budget.
     
    .DESCRIPTION
        Changes the active budget context and persists it to preferences.
        All entity management commands will use this budget by default.
     
    .PARAMETER Name
        The name of the budget to set as active.
     
    .PARAMETER WorkspacePath
        Optional custom workspace path.
     
    .EXAMPLE
        Set-ActiveBudget -Name "japan-holiday-2026"
         
        Sets the japan-holiday-2026 budget as active
     
    .OUTPUTS
        PSCustomObject - Updated preferences
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]$Name,
        
        [Parameter()]
        [string]$WorkspacePath
    )
    
    # Check if workspace is initialized
    if (-not (Test-WorkspaceInitialized -WorkspacePath $WorkspacePath)) {
        Write-Error "Budget workspace not initialized. Run Initialize-BudgetWorkspace first."
        return
    }
    
    # Get workspace path
    if (-not $WorkspacePath) {
        $WorkspacePath = Get-WorkspacePath
    }
    
    # Verify budget exists
    $budgetPath = Get-BudgetPath -BudgetName $Name -WorkspacePath $WorkspacePath
    
    if (-not (Test-Path $budgetPath)) {
        Write-Error "Budget '$Name' not found. Use Get-Budget to see available budgets."
        return
    }
    
    if ($PSCmdlet.ShouldProcess($Name, "Set as active budget")) {
        # Load and update preferences
        $preferences = Get-Preferences -WorkspacePath $WorkspacePath
        $previousActive = $preferences.activeBudget
        $preferences.activeBudget = $Name
        
        if (Set-Preferences -Preferences $preferences -WorkspacePath $WorkspacePath) {
            Write-Verbose "Active budget changed from '$previousActive' to '$Name'"
            Write-Host "Active budget set to: $Name" -ForegroundColor Green
            
            return $preferences
        }
    }
}