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.UI.ps1','scripts/updater/Vendoo.Updater.Host.ps1','scripts/updater/Vendoo.Updater.Worker.ps1','docs/PRODUCTION_UPDATER_2_1.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 ui=read(files[2]),host=read(files[3]),worker=read(files[4]),docs=read(files[5]);
const must=(text,needle,label)=>{if(!text.includes(needle))failures.push(`${label} fehlt: ${needle}`)};
for(const t of ["$UpdaterVersion = '2.1.0'",'Vendoo.Updater.Host.ps1','Write-InitialStatus','RedirectStandardOutput=$true','RedirectStandardError=$true','Get-WorkerExitDiagnostics','worker.stderr.log','Diagnoseordner','Protokoll öffnen','Erneut versuchen'])must(ui,t,'UI');
for(const t of ["$UpdaterVersion = '2.1.0'",'Write-HostStatus','Append-HostLog','CoreScript','Worker konnte nicht initialisiert werden','terminalen Status','Exception.ToString()'])must(host,t,'Worker-Host');
for(const t of ["$UpdaterVersion='2.1.0'",'[Parameter(Mandatory=$true)][string]$LogFile','verify:all','Save-Session','Load-Session'])must(worker,t,'Worker-Core');
for(const forbidden of [';exit 0',';exit 1',';exit 2'])if(worker.includes(forbidden))failures.push(`Worker-Core beendet den Host eigenmächtig: ${forbidden}`);
for(const f of files.slice(1,5)){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 ['Worker-Host','status.json','worker.stderr.log','Protokollpfad kann nicht mehr leer sein'])must(docs,t,'Dokumentation');
if(failures.length){console.error(`Production-Updater-2.1-Gate fehlgeschlagen (${failures.length})`);for(const f of failures)console.error(`- ${f}`);process.exit(1)}
console.log('OK: Vendoo Production Updater 2.1 – überwachter Worker-Host, garantierte Bootstrap-Diagnose und vollständiger Fehlerkanal.');
