import { existsSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
const root=resolve(import.meta.dirname,'..');
const files=['UPDATE_VENDOO.bat','scripts/update-vendoo-gui.ps1','scripts/updater/Vendoo.Updater.Preflight.ps1','scripts/updater/Vendoo.Updater.UI.ps1','scripts/updater/Vendoo.Updater.Host.ps1','scripts/updater/Vendoo.Updater.Worker.ps1','tools/verify-powershell-parser.ps1','tools/verify-powershell-static.mjs','docs/PRODUCTION_UPDATER_2_3.md'];
const failures=[];
for(const f of files) if(!existsSync(resolve(root,f))) failures.push(`Pflichtdatei fehlt: ${f}`);
const read=f=>readFileSync(resolve(root,f),'utf8');
const bat=read(files[0]),preflight=read(files[2]),ui=read(files[3]),host=read(files[4]),worker=read(files[5]),parser=read(files[6]),docs=read(files[8]);
const must=(text,needle,label)=>{if(!text.includes(needle))failures.push(`${label} fehlt: ${needle}`)};
for(const t of ['pushd "%~dp0"','Vendoo.Updater.Preflight.ps1','PREFLIGHT_EXIT','GUI_EXIT','pause']) must(bat,t,'BAT-Startvertrag');
for(const forbidden of ['-PackageRoot "%~dp0"','start "Vendoo Production Updater"']) if(bat.includes(forbidden)) failures.push(`BAT enthält verbotene Pfad-/Startregression: ${forbidden}`);
for(const t of ["$UpdaterVersion = '2.3.0'",'param()','Get-CanonicalPackageRoot','$PSScriptRoot','Resolve-Path','GetFullPath','GetInvalidPathChars','updater-preflight.log']) must(preflight,t,'Preflight');
for(const t of ["$UpdaterVersion = '2.3.0'",'Resolve-Path -LiteralPath','Vendoo.Updater.Host.ps1','RedirectStandardOutput=$true','RedirectStandardError=$true']) must(ui,t,'UI');
for(const t of ["$UpdaterVersion = '2.3.0'",'PowerShell-Parserprüfung','System.Management.Automation.Language.Parser','ParseFile']) must(host,t,'Worker-Host');
for(const t of ["$UpdaterVersion='2.3.0'",'Resolve-Path -LiteralPath','"${Label}: {0:N1}','"${Title}: sichere Eingabe']) must(worker,t,'Worker-Core');
for(const forbidden of ['"$Label:','"$Title:']) if(worker.includes(forbidden)) failures.push(`Worker enthält verbotene Parser-Regression: ${forbidden}`);
for(const t of ['System.Management.Automation.Language.Parser','ParseFile','Windows-PowerShell-Parser-Gate']) must(parser,t,'Windows-Parser-Gate');
for(const f of files.slice(1,7)){const b=readFileSync(resolve(root,f));if(!(b[0]===0xef&&b[1]===0xbb&&b[2]===0xbf))failures.push(`${f} besitzt keine UTF-8-BOM.`)}
for(const t of ['%~dp0','Resolve-Path','kanonisiert','Regression']) must(docs,t,'Dokumentation');
if(failures.length){console.error(`Production-Updater-2.3-Gate fehlgeschlagen (${failures.length})`);for(const f of failures)console.error(`- ${f}`);process.exit(1)}
console.log('OK: Vendoo Production Updater 2.3 – kanonischer Root-Pfad, kein trailing-backslash-Argument und synchron sichtbare Exit-Codes.');
