Private/Helpers.ps1

# Helper Functions for Data Persistence

function Get-DataFilePath {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('Biller', 'Earning', 'Account')]
        [string]$EntityType,

        [Parameter()]
        [string]$DataPath = $script:DefaultDataPath
    )

    $fileName = "$EntityType`s.json"
    return Join-Path -Path $DataPath -ChildPath $fileName
}

function Read-EntityData {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('Biller', 'Earning', 'Account')]
        [string]$EntityType,

        [Parameter()]
        [string]$DataPath = $script:DefaultDataPath
    )

    $filePath = Get-DataFilePath -EntityType $EntityType -DataPath $DataPath

    if (-not (Test-Path $filePath)) {
        return @()
    }

    try {
        $jsonContent = Get-Content -Path $filePath -Raw -ErrorAction Stop
        $data = $jsonContent | ConvertFrom-Json -ErrorAction Stop
        
        # Convert StartDate strings to datetime objects for Biller and Earning types
        if ($EntityType -in @('Biller', 'Earning')) {
            foreach ($item in $data) {
                if ($item.StartDate -is [string]) {
                    $item.StartDate = [datetime]::Parse($item.StartDate)
                }
            }
        }
        
        return $data
    }
    catch {
        Write-Warning "Failed to read $EntityType data: $_"
        return @()
    }
}

function Write-EntityData {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('Biller', 'Earning', 'Account')]
        [string]$EntityType,

        [Parameter(Mandatory)]
        [AllowEmptyCollection()]
        [array]$Data,

        [Parameter()]
        [string]$DataPath = $script:DefaultDataPath
    )

    $filePath = Get-DataFilePath -EntityType $EntityType -DataPath $DataPath

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

    try {
        if ($Data.Count -eq 0) {
            '[]' | Set-Content -Path $filePath -ErrorAction Stop
        }
        else {
            $Data | ConvertTo-Json -Depth 10 -AsArray | Set-Content -Path $filePath -ErrorAction Stop
        }
        return $true
    }
    catch {
        Write-Error "Failed to write $EntityType data: $_"
        return $false
    }
}

function Get-NextOccurrence {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [datetime]$StartDate,

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

        [Parameter(Mandatory)]
        [datetime]$FromDate
    )

    $currentDate = $StartDate

    while ($currentDate -lt $FromDate) {
        switch ($Frequency) {
            'Daily' { $currentDate = $currentDate.AddDays(1) }
            'Weekly' { $currentDate = $currentDate.AddDays(7) }
            'BiWeekly' { $currentDate = $currentDate.AddDays(14) }
            'Monthly' { $currentDate = $currentDate.AddMonths(1) }
            'Bimonthly' { $currentDate = $currentDate.AddMonths(2) }
            'Quarterly' { $currentDate = $currentDate.AddMonths(3) }
            'Yearly' { $currentDate = $currentDate.AddYears(1) }
            default { throw "Invalid frequency: $Frequency" }
        }
    }

    return $currentDate
}