CadencePlan.psm1

Set-StrictMode -Version Latest

function Get-NextBusinessDay {
    [CmdletBinding()]
    param([Parameter(Mandatory)][datetime]$Date)
    $d = $Date.Date
    while ($d.DayOfWeek -in 'Saturday', 'Sunday') { $d = $d.AddDays(1) }
    $d
}

function New-CadencePlan {
<#
.SYNOPSIS
Dates for each touch of an outbound sequence, counted in business days, never on a weekend.
.DESCRIPTION
-Offsets are business days after the start (0 = start day). -Channels names each touch and must match the
number of offsets. A start that falls on a weekend moves to Monday. Dates in -Skip (holidays you list) are
skipped as if they were weekends.
.EXAMPLE
New-CadencePlan -Start 2026-12-01 -Offsets 0,2,5,9 -Channels email,linkedin,email,call
#>

    [CmdletBinding()]
    param(
        [Parameter(Mandatory)][datetime]$Start,
        [Parameter(Mandatory)][int[]]$Offsets,
        [string[]]$Channels,
        [datetime[]]$Skip = @()
    )
    if ($Channels -and $Channels.Count -ne $Offsets.Count) { throw "Channels ($($Channels.Count)) must match Offsets ($($Offsets.Count))" }
    for ($i = 1; $i -lt $Offsets.Count; $i++) { if ($Offsets[$i] -lt $Offsets[$i - 1]) { throw 'Offsets must not go backwards' } }
    if ($Offsets | Where-Object { $_ -lt 0 }) { throw 'Offsets cannot be negative' }
    $skipDays = @($Skip | ForEach-Object { $_.Date })
    $isOff = { param($d) $d.DayOfWeek -in 'Saturday', 'Sunday' -or $skipDays -contains $d.Date }
    $day = $Start.Date
    while (& $isOff $day) { $day = $day.AddDays(1) }
    $cursor = $day
    $done = 0
    $step = 1
    foreach ($o in $Offsets) {
        while ($done -lt $o) {
            $cursor = $cursor.AddDays(1)
            if (-not (& $isOff $cursor)) { $done++ }
        }
        [pscustomobject]@{
            Step    = $step
            Offset  = $o
            Date    = $cursor.ToString('yyyy-MM-dd')
            Weekday = $cursor.DayOfWeek.ToString()
            Channel = if ($Channels) { $Channels[$step - 1] } else { '' }
        }
        $step++
    }
}

Export-ModuleMember -Function New-CadencePlan, Get-NextBusinessDay