[CmdletBinding()]
param()

Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
$markerDirectory = Join-Path $env:APPDATA 'AzureWorkshopz'
$markerPath = Join-Path $markerDirectory 'fslogix-profile-marker.json'
if (-not (Test-Path -LiteralPath $markerDirectory)) {
    New-Item -ItemType Directory -Path $markerDirectory -Force | Out-Null
}

$existing = $null
if (Test-Path -LiteralPath $markerPath) {
    try { $existing = Get-Content -LiteralPath $markerPath -Raw | ConvertFrom-Json } catch { $existing = $null }
}

$proof = [ordered]@{
    Marker = 'ProfileMarkerPresent'
    FirstHost = if ($existing -and $existing.FirstHost) { [string]$existing.FirstHost } else { [string]$env:COMPUTERNAME }
    CurrentHost = [string]$env:COMPUTERNAME
    FirstSeenUtc = if ($existing -and $existing.FirstSeenUtc) { [string]$existing.FirstSeenUtc } else { [DateTime]::UtcNow.ToString('o') }
    LastSeenUtc = [DateTime]::UtcNow.ToString('o')
}
[System.IO.File]::WriteAllText($markerPath, ($proof | ConvertTo-Json), [System.Text.UTF8Encoding]::new($false))

if (-not [System.Diagnostics.EventLog]::SourceExists('AVDWorkshop')) {
    New-EventLog -LogName Application -Source 'AVDWorkshop'
}
$message = "FSLogix profile marker observed. FirstHost=$($proof.FirstHost); CurrentHost=$($proof.CurrentHost); Marker=$($proof.Marker)"
Write-EventLog -LogName Application -Source 'AVDWorkshop' -EventId 41080 -EntryType Information -Message $message

$proof
