Private/Resolve-AccountParameter.ps1

function Resolve-AccountParameter {
    <#
    .SYNOPSIS
        Resolves the Account parameter for billers and earnings.
 
    .DESCRIPTION
        Encapsulates the account resolution logic:
        1. No accounts exist: Returns error
        2. Exactly one account exists: Auto-selects it
        3. Multiple accounts exist and Account specified: Validates or creates
        4. Multiple accounts exist and Account not specified: Returns error
 
    .PARAMETER Account
        The account name specified by the user (optional).
 
    .PARAMETER DataPath
        The data path for reading/writing account data.
 
    .PARAMETER PSCmdlet
        The calling cmdlet's $PSCmdlet for proper error handling.
 
    .OUTPUTS
        String - The AccountId to use, or $null if resolution failed.
 
    .NOTES
        This is a private helper function used by New-Biller, Set-Biller,
        New-Earning, and Set-Earning.
    #>

    [CmdletBinding()]
    param(
        [Parameter()]
        [string]$Account,

        [Parameter(Mandatory)]
        [string]$DataPath,

        [Parameter()]
        [System.Management.Automation.PSCmdlet]$CallingCmdlet
    )

    # Get all existing accounts
    $accounts = Read-EntityData -EntityType 'Account' -DataPath $DataPath

    # Case 1: No accounts exist
    if ($accounts.Count -eq 0) {
        $errorMessage = 'No accounts found. Please create an account first using New-Account.'
        if ($CallingCmdlet) {
            $errorRecord = [System.Management.Automation.ErrorRecord]::new(
                [System.InvalidOperationException]::new($errorMessage),
                'NoAccountsExist',
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $null
            )
            $CallingCmdlet.ThrowTerminatingError($errorRecord)
        }
        else {
            Write-Error $errorMessage
        }
        return $null
    }

    # Case 2: Exactly one account exists - auto-select it
    if ($accounts.Count -eq 1) {
        $selectedAccount = $accounts[0]
        if ([string]::IsNullOrEmpty($Account)) {
            Write-Verbose "Auto-selecting the only available account: $($selectedAccount.Name)"
        }
        elseif ($Account -ne $selectedAccount.Name) {
            # User specified a different account name - create it
            Write-Warning "Account '$Account' not found. Creating new account..."
            $newAccount = New-Account -Name $Account -Bank 'Unknown' -Last4Digits '0000' -DataPath $DataPath
            if ($newAccount) {
                return $newAccount.Id
            }
            return $null
        }
        return $selectedAccount.Id
    }

    # Case 3: Multiple accounts exist
    if ([string]::IsNullOrEmpty($Account)) {
        # Account parameter not specified - error
        $accountNames = ($accounts | ForEach-Object { $_.Name }) -join ', '
        $errorMessage = "Multiple accounts exist. Please specify one using -Account parameter. Available accounts: $accountNames"
        if ($CallingCmdlet) {
            $errorRecord = [System.Management.Automation.ErrorRecord]::new(
                [System.ArgumentException]::new($errorMessage),
                'AccountParameterRequired',
                [System.Management.Automation.ErrorCategory]::InvalidArgument,
                $null
            )
            $CallingCmdlet.ThrowTerminatingError($errorRecord)
        }
        else {
            Write-Error $errorMessage
        }
        return $null
    }

    # Account parameter specified - find matching account
    $matchingAccount = $accounts | Where-Object { $_.Name -eq $Account }

    if ($matchingAccount) {
        Write-Verbose "Using account: $($matchingAccount.Name)"
        return $matchingAccount.Id
    }

    # Account doesn't exist - create it with a warning
    Write-Warning "Account '$Account' not found. Creating new account..."
    $newAccount = New-Account -Name $Account -Bank 'Unknown' -Last4Digits '0000' -DataPath $DataPath
    if ($newAccount) {
        return $newAccount.Id
    }

    return $null
}