param(
    [string]$Version
)

$ErrorActionPreference = "Stop"
$projectRoot = Split-Path -Parent (Split-Path -Parent $PSScriptRoot)
$applicationDir = Join-Path $projectRoot "application"

if (-not $Version) {
    $versionLine = Get-Content (Join-Path $applicationDir "gradle.properties") |
        Where-Object { $_ -match '^maptoolsVersion=' } |
        Select-Object -First 1
    if (-not $versionLine) { throw "maptoolsVersion fehlt in application/gradle.properties." }
    $Version = ($versionLine -split '=', 2)[1].Trim()
}

$releaseDirectory = Join-Path $projectRoot "release\$Version"
$requiredFiles = @(
    "windows\ArxSilex-MapTools-Converter-$Version-windows-x64.zip",
    "linux\ArxSilex-MapTools-Converter-$Version-linux-x64.tar.gz",
    "source\ArxSilex-MapTools-Converter-$Version-src.zip",
    "security\ArxSilex-MapTools-Converter-$Version-windows-x64.cdx.json",
    "security\ArxSilex-MapTools-Converter-$Version-linux-x64.cdx.json",
    "security\ArxSilex-MapTools-Converter-$Version-windows-x64-osv.json",
    "security\ArxSilex-MapTools-Converter-$Version-linux-x64-osv.json",
    "security\dependency-updates.json",
    "security\toolchain.json"
)

foreach ($relativePath in $requiredFiles) {
    $fullPath = Join-Path $releaseDirectory $relativePath
    if (-not (Test-Path -LiteralPath $fullPath)) {
        throw "Release unvollständig, Datei fehlt: $fullPath"
    }
}

Copy-Item -LiteralPath (Join-Path $projectRoot "CHANGELOG.md") -Destination (Join-Path $releaseDirectory "CHANGELOG.md") -Force
Copy-Item -LiteralPath (Join-Path $projectRoot "LICENSE") -Destination (Join-Path $releaseDirectory "LICENSE") -Force
Copy-Item -LiteralPath (Join-Path $projectRoot "NOTICE.md") -Destination (Join-Path $releaseDirectory "NOTICE.md") -Force

$filesToHash = Get-ChildItem -LiteralPath $releaseDirectory -File -Recurse |
    Where-Object { $_.Name -notin @("SHA256SUMS.txt", "release-manifest.json") } |
    Sort-Object FullName

$manifestArtifacts = @()
$checksumLines = @()
foreach ($file in $filesToHash) {
    $relativePath = [IO.Path]::GetRelativePath($releaseDirectory, $file.FullName).Replace('\', '/')
    $hash = (Get-FileHash -Algorithm SHA256 -LiteralPath $file.FullName).Hash.ToLowerInvariant()
    $checksumLines += "$hash  $relativePath"
    $manifestArtifacts += [ordered]@{
        path = $relativePath
        bytes = $file.Length
        sha256 = $hash
    }
}

$checksumPath = Join-Path $releaseDirectory "SHA256SUMS.txt"
$checksumLines | Set-Content -LiteralPath $checksumPath -Encoding utf8

$manifest = [ordered]@{
    schemaVersion = 1
    product = "ArxSilex MapTools Converter"
    version = $Version
    createdAt = (Get-Date).ToUniversalTime().ToString("o")
    platforms = @("windows-x64", "linux-x64", "source")
    securityGate = "passed"
    sbomFormat = "CycloneDX JSON"
    license = "GPL-3.0-only"
    artifacts = $manifestArtifacts
}
$manifest | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Join-Path $releaseDirectory "release-manifest.json") -Encoding utf8

Write-Host "Release abgeschlossen: $releaseDirectory"
