# update-nextnote-assets.ps1

[CmdletBinding()]
param(
    [Parameter(Mandatory)][string]$ReleaseDirectory
)

$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
Import-Module (Join-Path $PSScriptRoot 'BuildCommon.psm1') -Force

function Assert-Sha256 {
    param([string]$Path, [string]$Expected, [string]$Description)
    $actual = Get-FileSha256 $Path
    if ($actual -ne $Expected.ToLowerInvariant()) {
        throw "$Description SHA-256 mismatch: expected $Expected, got $actual."
    }
    $actual
}

$repoRoot = Get-RepoRoot
$releaseDirectory = (Resolve-Path $ReleaseDirectory).Path
$releaseManifestPath = Join-Path $releaseDirectory 'release-manifest.json'
$ffmpegPath = Join-Path $releaseDirectory 'ffmpeg.exe'
$archivePath = Join-Path $releaseDirectory 'ffmpeg.7z'
$ffmpegManifestPath = Join-Path $releaseDirectory 'ffmpeg.manifest.json'

foreach ($requiredFile in $releaseManifestPath, $ffmpegPath, $archivePath, $ffmpegManifestPath) {
    if (-not (Test-Path $requiredFile)) { throw "Release package file is missing: $requiredFile" }
}

$releaseManifest = Get-Content $releaseManifestPath -Raw | ConvertFrom-Json
if ($releaseManifest.status -ne 'release-ready') { throw 'Release package is not marked release-ready.' }
if ($releaseManifest.license -ne 'LGPL-2.1-or-later') { throw 'Release package is not the approved LGPL FFmpeg build.' }
Assert-Sha256 -Path $ffmpegPath -Expected $releaseManifest.signedSha256 -Description 'Signed ffmpeg.exe' | Out-Null
Assert-Sha256 -Path $archivePath -Expected $releaseManifest.archiveSha256 -Description 'ffmpeg.7z' | Out-Null

$ffmpegManifest = Get-Content $ffmpegManifestPath -Raw | ConvertFrom-Json
if ($ffmpegManifest.license -ne 'LGPL-2.1-or-later') {
    throw 'ffmpeg.manifest.json is not marked with the approved LGPL licence.'
}
if ($ffmpegManifest.executableSha256 -ne $releaseManifest.signedSha256 -or $ffmpegManifest.archiveSha256 -ne $releaseManifest.archiveSha256) {
    throw 'ffmpeg.manifest.json does not match release-manifest.json.'
}

$windowsRoot = Split-Path -Parent $repoRoot
$targets = @(
    (Join-Path $windowsRoot 'NextNote\NextNoteDisplay\Assets'),
    (Join-Path $windowsRoot 'NextNote\NextNoteHelper\Assets'),
    (Join-Path $windowsRoot 'NextNote Google Slides\NextNoteGSHelper\Assets')
)

foreach ($target in $targets) {
    if (-not (Test-Path $target)) { throw "Application asset target is missing: $target" }
}

foreach ($target in $targets) {
    Copy-FileAtomically -Source $archivePath -Destination (Join-Path $target 'ffmpeg.7z')
    Copy-FileAtomically -Source $ffmpegManifestPath -Destination (Join-Path $target 'ffmpeg.manifest.json')
}

$archiveHashes = @()
foreach ($target in $targets) {
    $targetArchive = Join-Path $target 'ffmpeg.7z'
    $targetManifest = Join-Path $target 'ffmpeg.manifest.json'
    $archiveHashes += Assert-Sha256 -Path $targetArchive -Expected $releaseManifest.archiveSha256 -Description $targetArchive
    Assert-Sha256 -Path $targetManifest -Expected (Get-FileSha256 $ffmpegManifestPath) -Description $targetManifest | Out-Null
}
if (@($archiveHashes | Select-Object -Unique).Count -ne 1) { throw 'The three embedded FFmpeg archives are not byte-identical.' }

Write-Host 'Updated all three application FFmpeg assets with the verified signed release archive.' -ForegroundColor Green
