Payload/scripts/kaden/harness/init-harness-feature.ps1

#Requires -Version 5.1
<#
.SYNOPSIS
    Initialize docs/harness/features/<slug> from schema 1.2 templates.

.PARAMETER Force
    Overwrite an existing .review-loop.json (default: refuse when present).

.EXAMPLE
    .\scripts\init-harness-feature.ps1 -Feature saved-search-queries -Slice mo-save-list -ProfileName standard
#>

[CmdletBinding()]
param(
    [Parameter(Mandatory = $true)]
    [string]$Feature,

    [string]$Slice = 'mo-save-list',

    [ValidateSet('lean', 'standard', 'thorough', 'exhaustive')]
    [string]$ProfileName = 'standard',

    [switch]$Force,

    [string]$RepoRoot = '',

    [string]$FeatureRoot = '',

    [switch]$AllowArbitraryFeatureRoot
)

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'

Import-Module (Join-Path $PSScriptRoot 'lib\HarnessContract.psm1') -Force

if ([string]::IsNullOrWhiteSpace($RepoRoot)) {
    $RepoRoot = Get-HarnessRepoRoot -StartPath $PSScriptRoot
}

$featureRoot = Get-HarnessFeatureRoot -RepoRoot $RepoRoot -Feature $Feature `
    -FeatureRootOverride $FeatureRoot -AllowArbitraryFeatureRoot:$AllowArbitraryFeatureRoot
$templatePath = Join-Path $RepoRoot '.github\templates\review-loop.json'
$loopPath = Join-Path $featureRoot '.review-loop.json'

if (-not (Test-Path $templatePath)) {
    throw "Template not found: $templatePath"
}

if ((Test-Path $loopPath) -and -not $Force) {
    throw ".review-loop.json already exists at $loopPath. Use -Force to overwrite (destroys loop history)."
}

if (-not (Test-Path $featureRoot)) {
    New-Item -ItemType Directory -Path $featureRoot -Force | Out-Null
    New-Item -ItemType Directory -Path (Join-Path $featureRoot '.reviews') -Force | Out-Null
    New-Item -ItemType Directory -Path (Join-Path $featureRoot 'sensors') -Force | Out-Null
}

$updated = Get-HarnessIso8601UtcNow
$started = (Get-Date).ToString('yyyy-MM-dd')

$raw = Get-Content $templatePath -Raw
$raw = $raw.Replace('<slug>', $Feature)
$raw = $raw.Replace('<slice-id>', $Slice)
$raw = $raw -replace '"profile": "standard"', "`"profile`": `"$ProfileName`""
$raw = $raw -replace '"started": "[^"]+"', "`"started`": `"$started`""
$raw = $raw -replace '"updated": "[^"]+"', "`"updated`": `"$updated`""

Set-Content -Path $loopPath -Value $raw -Encoding UTF8 -NoNewline
Add-Content -Path $loopPath -Value '' -Encoding UTF8

$formatResult = Invoke-HarnessFormatReviewLoop -RepoRoot $RepoRoot -Paths @($loopPath)
if ($formatResult.ExitCode -ne 0) {
    throw "format-review-loop.mjs failed: $($formatResult.Stderr)"
}

Import-Module (Join-Path $PSScriptRoot 'lib\HarnessJournal.psm1') -Force
Initialize-HarnessJournal -FeatureRoot $featureRoot | Out-Null
Write-HarnessGateFileProvenance -FeatureRoot $featureRoot -GateFile 'review-loop' -Source 'init-harness-feature.ps1'
Write-HarnessGateFileProvenance -FeatureRoot $featureRoot -GateFile journal -Source 'init-harness-feature.ps1'

$profilePath = Join-Path $featureRoot '.profile'
"profile: $ProfileName" | Set-Content -Path $profilePath -Encoding UTF8

Write-Output "Initialized harness feature at $featureRoot"
Write-Output " .review-loop.json (schema 1.2, template formatting preserved)"
Write-Output " journal/events.jsonl (append-only ledger)"
Write-Output " .profile ($ProfileName)"
Write-Output "Next: add spec.md / user-stories.md, then @atdd-developer own $Feature slice $Slice"