Public/New-Account.ps1

function New-Account {
    <#
    .SYNOPSIS
        Creates a new account entry.
 
    .DESCRIPTION
        Creates a new account with specified name, bank, and last 4 digits.
        The account is saved to the data store.
 
    .PARAMETER Name
        The name of the account.
 
    .PARAMETER Bank
        The bank name.
 
    .PARAMETER Last4Digits
        The last 4 digits of the account number.
 
    .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
        New-Account -Name "Checking" -Bank "Wells Fargo" -Last4Digits "1234"
 
    .OUTPUTS
        Account object
    #>

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

        [Parameter(Mandatory, ValueFromPipelineByPropertyName)]
        [string]$Bank,

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

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

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

        $account = [Account]::new($Name, $Bank, $Last4Digits)

        $existingAccounts = Read-EntityData -EntityType 'Account' -DataPath $resolvedPath
        $accountsList = [System.Collections.ArrayList]@($existingAccounts)
        $accountsList.Add($account.ToHashtable()) | Out-Null

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