Public/Export-ProjectedTransactions.ps1

function Export-ProjectedTransactions {
    <#
    .SYNOPSIS
        Exports projected transactions to a file.
 
    .DESCRIPTION
        Generates projected transactions and exports them to CSV or JSON format.
 
    .PARAMETER StartDate
        The start date for the projection period.
 
    .PARAMETER EndDate
        The end date for the projection period.
 
    .PARAMETER OutputPath
        The file path for the export. Extension determines format (.csv or .json).
 
    .PARAMETER AccountId
        Optional account ID to associate with transactions.
 
    .PARAMETER InitialBalance
        The starting balance for the projection. Defaults to 0.
 
    .PARAMETER Format
        Output format: CSV or JSON. If not specified, determined by file extension.
 
    .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
        Export-ProjectedTransactions -StartDate "2025-01-01" -EndDate "2025-12-31" -OutputPath "C:\budget\2025.csv"
 
    .EXAMPLE
        Export-ProjectedTransactions -StartDate (Get-Date) -EndDate (Get-Date).AddMonths(3) -OutputPath "projection.json" -Format JSON
 
    .EXAMPLE
        Export-ProjectedTransactions -StartDate "2025-01-01" -EndDate "2025-12-31" -OutputPath "japan-trip.csv" -Budget "japan-holiday-2026"
 
    .OUTPUTS
        File path of the exported data
    #>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [datetime]$StartDate,

        [Parameter(Mandatory)]
        [datetime]$EndDate,

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

        [Parameter()]
        [string]$AccountId,

        [Parameter()]
        [decimal]$InitialBalance = 0,

        [Parameter()]
        [ValidateSet('CSV', 'JSON')]
        [string]$Format,

        [Parameter()]
        [string]$Budget,

        [Parameter()]
        [string]$DataPath
    )

    # Get projected transactions
    $transactions = Get-ProjectedTransactions -StartDate $StartDate -EndDate $EndDate -AccountId $AccountId -InitialBalance $InitialBalance -Budget $Budget -DataPath $DataPath

    # Determine format from file extension if not specified
    if (-not $Format) {
        $extension = [System.IO.Path]::GetExtension($OutputPath).ToLower()
        $Format = switch ($extension) {
            '.json' { 'JSON' }
            '.csv' { 'CSV' }
            default { 'CSV' }
        }
    }

    # Ensure directory exists
    $directory = Split-Path -Path $OutputPath -Parent
    if ($directory -and -not (Test-Path $directory)) {
        New-Item -Path $directory -ItemType Directory -Force | Out-Null
    }

    # Export based on format
    try {
        switch ($Format) {
            'CSV' {
                $transactions | Select-Object Date, Name, Amount, Balance, Type, AccountId |
                    Export-Csv -Path $OutputPath -NoTypeInformation -ErrorAction Stop
                Write-Verbose "Exported to CSV: $OutputPath"
            }
            'JSON' {
                $transactions | ConvertTo-Json -Depth 10 |
                    Set-Content -Path $OutputPath -ErrorAction Stop
                Write-Verbose "Exported to JSON: $OutputPath"
            }
        }

        return (Resolve-Path $OutputPath).Path
    }
    catch {
        Write-Error "Failed to export transactions: $_"
    }
}