Private/Resolve-DocId.ps1

# Copyright (c) 2026 Jeffrey Snover. All rights reserved.
# Licensed under the MIT License. See LICENSE file in the project root.

<#
.SYNOPSIS
    Generates a unique document ID by appending year and optional numeric suffix.
.DESCRIPTION
    Takes a base slug (from New-Slug) and appends the publication year to form a
    candidate doc-id. If a source directory with that name already exists, appends
    incrementing numeric suffixes (-1, -2, etc.) until a unique ID is found.

    Used by Import-AITriadDocument to ensure every ingested document gets a
    collision-free identifier.
.PARAMETER BaseSlug
    The URL-safe slug generated by New-Slug (e.g., 'ai-governance-framework').
.PARAMETER Year
    Four-digit year to append. Defaults to the current year.
.EXAMPLE
    Resolve-DocId -BaseSlug 'ai-governance-framework'
    # Returns: 'ai-governance-framework-2026'

.EXAMPLE
    Resolve-DocId -BaseSlug 'ai-safety-report' -Year '2025'
    # Returns: 'ai-safety-report-2025' (or 'ai-safety-report-2025-1' if taken)
#>

function Resolve-DocId {
    param(
        [string]$BaseSlug,
        [string]$Year = (Get-Date -Format 'yyyy')
    )

    $SourcesDir = Get-SourcesDir
    if (-not (Test-Path $SourcesDir)) {
        Write-Warning "Sources directory not found: $SourcesDir — uniqueness check skipped"
    }
    $Candidate  = "$BaseSlug-$Year"
    $Counter    = 1
    while (Test-Path (Join-Path $SourcesDir $Candidate)) {
        $Candidate = "$BaseSlug-$Year-$Counter"
        $Counter++
    }
    return $Candidate
}