tool/Automation/functions/Export-PipelineGuides.ps1

function Export-PipelineGuides {
    <#
    .SYNOPSIS
        Generate GUIDE.md files from templates with run-specific metadata.
    .PARAMETER OutputPath
        Root output folder for the pipeline run.
    .PARAMETER ScoringPath
        Path to scoring-results.json (for contact count).
    .PARAMETER ManifestPath
        Path to email-manifest.json (for email count).
    .PARAMETER Profile
        Profile name used for this run.
    .PARAMETER SourceFolder
        Original source email folder path.
    #>

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

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

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

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

        [Parameter(Mandatory)]
        [string]$SourceFolder
    )

    $repoRoot = $PSScriptRoot | Split-Path | Split-Path | Split-Path
    $templateDir = Join-Path $repoRoot 'tool' 'Output' 'templates'

    # Get module version
    $psd1Path = Join-Path $repoRoot 'LeadForge.psd1'
    $version = if (Test-Path $psd1Path) {
        (Import-PowerShellDataFile $psd1Path).ModuleVersion
    }
    else { 'unknown' }

    # Get counts
    $contactCount = 0
    if (Test-Path $ScoringPath) {
        $scoring = Get-Content $ScoringPath -Raw | ConvertFrom-Json
        $contactCount = $scoring.results.Count
    }

    $emailCount = 0
    if (Test-Path $ManifestPath) {
        $manifest = Get-Content $ManifestPath -Raw | ConvertFrom-Json
        $emailCount = $manifest.emails.Count
    }

    # Token values
    $tokens = @{
        '{{VERSION}}'       = $version
        '{{DATE}}'          = (Get-Date).ToString('yyyy-MM-dd HH:mm')
        '{{PROFILE}}'       = $Profile
        '{{SOURCE_FOLDER}}' = Split-Path $SourceFolder -Leaf
        '{{CONTACT_COUNT}}' = $contactCount.ToString()
        '{{EMAIL_COUNT}}'   = $emailCount.ToString()
        '{{OUTPUT_FOLDER}}' = Split-Path $OutputPath -Leaf
    }

    $filesCreated = @()

    # --- Root GUIDE.md ---
    $rootTemplate = Join-Path $templateDir 'GUIDE-root.md'
    if (Test-Path $rootTemplate) {
        $content = Get-Content $rootTemplate -Raw
        foreach ($key in $tokens.Keys) {
            $content = $content.Replace($key, $tokens[$key])
        }
        $rootGuide = Join-Path $OutputPath 'GUIDE.md'
        Set-Content -Path $rootGuide -Value $content -Encoding UTF8
        $filesCreated += $rootGuide
        Write-Host " Guide: $rootGuide" -ForegroundColor Gray
    }

    # --- Output GUIDE.md ---
    $outputTemplate = Join-Path $templateDir 'GUIDE-output.md'
    $outputDir = Join-Path $OutputPath 'output'
    if ((Test-Path $outputTemplate) -and (Test-Path $outputDir)) {
        $content = Get-Content $outputTemplate -Raw
        foreach ($key in $tokens.Keys) {
            $content = $content.Replace($key, $tokens[$key])
        }
        $outputGuide = Join-Path $outputDir 'GUIDE.md'
        Set-Content -Path $outputGuide -Value $content -Encoding UTF8
        $filesCreated += $outputGuide
        Write-Host " Guide: $outputGuide" -ForegroundColor Gray
    }

    # --- Copy profile configuration ---
    $profileDir = Join-Path $repoRoot 'profiles'
    $profileFile = Join-Path $profileDir "$Profile.json"
    if (Test-Path $profileFile) {
        $profileTarget = Join-Path $OutputPath 'profile.json'
        Copy-Item -Path $profileFile -Destination $profileTarget -Force
        $filesCreated += $profileTarget
        Write-Host " Profile: $profileTarget" -ForegroundColor Gray
    }

    $filesCreated
}