Public/Build-PCSite.ps1

function Build-PCSite {
    <#
    .SYNOPSIS
    Builds a markdown content tree into a themed, file://-portable static website.

    .DESCRIPTION
    Thin shell over PowerCraft.Sites.Core (D-002: no business logic here — binding,
    shaping, and safety only). The kernel builds to a temp directory and swaps it into
    place, writes build-report.json on every run, and collects per-page errors.

    Per D-005, a failed page fails the run: this function throws (non-zero exit for
    unattended callers) when the build result contains errors; the report on disk
    names each failed page.

    .PARAMETER ContentPath
    Root of the markdown content tree (flat or multi-portal — detection per PY-02).

    .PARAMETER OutputPath
    Output directory. Replaced wholesale on every build.

    .PARAMETER ConfigPath
    Optional site.config.yaml. Missing file means engine defaults.

    .PARAMETER Theme
    Theme directory path, or the name of a theme bundled with this module.
    Default: the bundled neutral 'default' theme.

    .PARAMETER Clean
    Remove an existing output directory before building (honors -WhatIf).

    .PARAMETER Report
    Emit the full parsed build report to the pipeline instead of the summary object.

    .PARAMETER Year
    Explicit build year used for {year} substitution. Defaults to the current year at
    this shell boundary; the kernel itself never reads the wall clock (AC-3).

    .EXAMPLE
    Build-PCSite -ContentPath .\content -OutputPath .\dist -ConfigPath .\site.config.yaml
    #>

    [CmdletBinding(SupportsShouldProcess)]
    param(
        [Parameter(Mandatory, Position = 0)]
        [string]$ContentPath,

        [Parameter(Mandatory, Position = 1)]
        [string]$OutputPath,

        [string]$ConfigPath,

        [string]$Theme = 'default',

        [switch]$Clean,

        [switch]$Report,

        [int]$Year = (Get-Date).Year
    )

    $contentFull = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($ContentPath)
    if (-not (Test-Path -LiteralPath $contentFull -PathType Container)) {
        throw "Content path not found or not a directory: $contentFull"
    }

    $outputFull = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($OutputPath)

    $configFull = $null
    if ($ConfigPath) {
        $configFull = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($ConfigPath)
        if (-not (Test-Path -LiteralPath $configFull -PathType Leaf)) {
            throw "Config file not found: $configFull"
        }
    }

    $themeFull = Resolve-PCSiteThemePath -Theme $Theme

    if ($Clean -and (Test-Path -LiteralPath $outputFull) -and
        $PSCmdlet.ShouldProcess($outputFull, 'Remove existing output directory')) {
        Remove-Item -LiteralPath $outputFull -Recurse -Force
    }

    if (-not $PSCmdlet.ShouldProcess($outputFull, "Build site from '$contentFull' (theme '$Theme')")) {
        return
    }

    $request = [PowerCraft.Sites.Core.Building.SiteBuildRequest]::new()
    $request.ContentPath = $contentFull
    $request.OutputPath = $outputFull
    $request.ThemePath = $themeFull
    $request.ConfigPath = $configFull
    $request.Year = $Year

    $result = [PowerCraft.Sites.Core.Building.SiteBuilder]::Build($request)

    if (-not $result.Success) {
        $detail = ($result.Errors | ForEach-Object { "$($_.Page) [$($_.Phase)]: $($_.Message)" }) -join "`n"
        throw "Build failed with $($result.Errors.Count) error(s) — see $($result.ReportPath)`n$detail"
    }

    if ($Report) {
        Get-Content -LiteralPath $result.ReportPath -Raw | ConvertFrom-Json
    }
    else {
        [pscustomobject]@{
            Success    = $result.Success
            Mode       = $result.Report.Mode
            PageCount  = $result.PageCount
            OutputPath = $result.OutputPath
            ReportPath = $result.ReportPath
        }
    }
}