Private/New-SqlCertResult.ps1

# =============================================================================
# Script : Private/New-SqlCertResult.ps1
# Author : Keith Ramsey
# Created : 2026-05-15
# =============================================================================
# Change Log
# -----------------------------------------------------------------------------
# 2026-05-15 Keith Ramsey Initial: SqlCert.Result factory + provenance mixin
# (DR-005). Migrated from the worker's
# New-StageResult; adds the provenance stamp.
# =============================================================================
# Decision Contract (see Docs/DECISION_REGISTER.md)
# -----------------------------------------------------------------------------
# Must : closed Status vocab Success|Skipped|Pending|Failed (DR-005);
# stamp ModuleVersion/SchemaVersion/CommitSha/RunId/Timestamp from
# the single source captured in SqlCertForge.psm1; pure factory,
# no state change, never throws.
# =============================================================================

function New-SqlCertResult {
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSUseShouldProcessForStateChangingFunctions', '',
        Justification = 'Pure in-memory result factory; performs no state change.')]
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)] [string] $Stage,
        [Parameter(Mandatory)] [ValidateSet('Success', 'Skipped', 'Pending', 'Failed')] [string] $Status,
        [string] $Detail,
        $Data
    )

    # Provenance: single source of truth is SqlCertForge.psm1. These script
    # vars always exist when the module is loaded; the fallbacks keep the
    # factory honest (never fabricate) if a helper is dot-sourced standalone
    # for a unit test.
    $mv = if (Test-Path variable:script:moduleVersion) { $script:moduleVersion } else { 'unknown' }
    $sv = if (Test-Path variable:script:schemaVersion) { $script:schemaVersion } else { 'unknown' }
    $sha = if (Test-Path variable:script:commitSha)    { $script:commitSha }     else { $null }
    $rid = if (Test-Path variable:script:runId)        { $script:runId }         else { $null }

    [pscustomobject]@{
        PSTypeName    = 'SqlCert.Result'
        Stage         = $Stage
        Status        = $Status
        Detail        = $Detail
        Data          = $Data
        ModuleVersion = $mv
        SchemaVersion = $sv
        CommitSha     = $sha
        RunId         = $rid
        Timestamp     = [datetime]::UtcNow.ToString('o')
    }
}