#!/usr/bin/env pwsh
<#
.SYNOPSIS
    Generate Phase 1 shadow run identifiers (RunId, JobId, JobRunId, CorrelationId, Idempotency-Key).

.DESCRIPTION
    Produces a JSON-formatted versionset.json file with all identifiers needed to enqueue Phase 1.
    Uses CRYPTOGRAPHIC random UUIDs and correlation for full traceability.

.PARAMETER OutputPath
    Path to save versionset.json (default: ./versionset.json in current directory)

.EXAMPLE
    .\generate-shadow-run-identifiers.ps1 -OutputPath ./phase1-versionset.json

.OUTPUTS
    JSON file with structure:
    {
        "phase1_run": {
            "runId": "UUID",
            "jobId": "UUID",
            "jobRunId": "UUID",
            "correlationId": "UUID",
            "idempotencyKey": "UUID",
            "generatedAt": "ISO8601 timestamp",
            "usage": "Use these IDs to enqueue Job 893 in Hangfire..."
        }
    }
#>

[CmdletBinding()]
param(
    [string]$OutputPath = "./versionset.json"
)

$ErrorActionPreference = 'Stop'

Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
Write-Host "Phase 1: Generate Shadow Run Identifiers" -ForegroundColor Cyan
Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan

Write-Host "`n[1/3] Generating cryptographic UUIDs..."

$runId = [System.Guid]::NewGuid()
$jobId = [System.Guid]::NewGuid()
$jobRunId = [System.Guid]::NewGuid()
$correlationId = [System.Guid]::NewGuid()
$idempotencyKey = [System.Guid]::NewGuid()

Write-Host "  ✅ RunId:             $runId"
Write-Host "  ✅ JobId:             $jobId"
Write-Host "  ✅ JobRunId:          $jobRunId"
Write-Host "  ✅ CorrelationId:     $correlationId"
Write-Host "  ✅ IdempotencyKey:    $idempotencyKey"

Write-Host "`n[2/3] Creating JSON payload..."

$payload = @{
    phase1_run = @{
        runId          = $runId.ToString()
        jobId          = $jobId.ToString()
        jobRunId       = $jobRunId.ToString()
        correlationId  = $correlationId.ToString()
        idempotencyKey = $idempotencyKey.ToString()
        generatedAt    = [System.DateTime]::UtcNow.ToString("o")
        windowStart    = "2024-01-02"
        windowEnd      = "2024-09-10"
        usage          = "Use these IDs to enqueue Job 893 (Phase 1 shadow run) in Hangfire. Command: `n  Invoke-WebRequest -Uri 'http://127.0.0.1:5002/api/shadow-runs' -Method POST -Headers @{ 'X-KArtSell-User'='admin'; 'X-KArtSell-Role'='Admin'; 'Content-Type'='application/json' } -Body (ConvertTo-Json @{ modelId='<modelId>'; datasetId='<datasetId>'; windowStart='2024-01-02'; windowEnd='2024-09-10'; phaseFilter='All' })"
    }
}

Write-Host "  ✅ JSON payload generated"

Write-Host "`n[3/3] Writing to file: $OutputPath"

$json = $payload | ConvertTo-Json -Depth 10
$json | Out-File -FilePath $OutputPath -Encoding UTF8

Write-Host "  ✅ File saved: $(Resolve-Path $OutputPath)"

Write-Host "`n✅ IDENTIFIERS GENERATED`n"
Write-Host $json -ForegroundColor Green

Write-Host "`nNext Steps:`n"
Write-Host "  1. Copy the identifiers from above or read from $OutputPath"
Write-Host "  2. Call POST /api/shadow-runs with modelId/datasetId from frozen VersionSet"
Write-Host "  3. Hangfire will enqueue Job 893 with these correlation IDs"
Write-Host "  4. Monitor logs: grep 'CorrelationId: $correlationId' app.log"

Write-Host "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━" -ForegroundColor Cyan
