Public/Set-Account.ps1

function Set-Account {
    <#
    .SYNOPSIS
        Updates an existing account entry.
 
    .DESCRIPTION
        Modifies properties of an existing account identified by ID.
 
    .PARAMETER Id
        The ID of the account to update.
 
    .PARAMETER Name
        New name for the account.
 
    .PARAMETER Bank
        New bank name for the account.
 
    .PARAMETER Last4Digits
        New last 4 digits for the account.
 
    .PARAMETER Budget
        Optional budget name to target. Uses active budget if not specified.
 
    .PARAMETER DataPath
        Optional custom path for data storage. Overrides budget-based paths.
 
    .EXAMPLE
        Set-Account -Id "abc123" -Name "Primary Checking"
 
    .OUTPUTS
        Updated Account object
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]$Id,

        [Parameter()]
        [string]$Name,

        [Parameter()]
        [string]$Bank,

        [Parameter()]
        [ValidatePattern('^\d{4}$')]
        [string]$Last4Digits,

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

    process {
        $resolvedPath = Resolve-DataPath -DataPath $DataPath -Budget $Budget
        if (-not $resolvedPath) { return }

        $accounts = Read-EntityData -EntityType 'Account' -DataPath $resolvedPath
        $account = $accounts | Where-Object { $_.Id -eq $Id }

        if (-not $account) {
            Write-Error "Account with ID '$Id' not found."
            return
        }

        if ($PSBoundParameters.ContainsKey('Name')) { $account.Name = $Name }
        if ($PSBoundParameters.ContainsKey('Bank')) { $account.Bank = $Bank }
        if ($PSBoundParameters.ContainsKey('Last4Digits')) { $account.Last4Digits = $Last4Digits }

        if (Write-EntityData -EntityType 'Account' -Data $accounts -DataPath $resolvedPath) {
            Write-Verbose "Updated account: $($account.Name)"
            return $account
        }
    }
}