@echo off
setlocal
chcp 65001 >nul
cd /d "%~dp0"

for /f %%I in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "RUNSTAMP=%%I"
set "RUN_DIR=%~dp0diagnostics\%RUNSTAMP%"
mkdir "%RUN_DIR%" 2>nul

echo 正在启动 YuukiLyrics Debug 版并监控崩溃...
echo 请正常操作，直到复现崩溃。请不要关闭此窗口。
echo.

systeminfo > "%RUN_DIR%\systeminfo.txt" 2>&1
wmic path win32_VideoController get Name,DriverVersion,AdapterRAM /format:list > "%RUN_DIR%\graphics.txt" 2>&1

"%~dp0tools\procdump64.exe" -accepteula -ma -e -t -l -x "%RUN_DIR%" "%~dp0yuukilyrics.exe" > "%RUN_DIR%\procdump.log" 2>&1
set "APP_EXIT=%ERRORLEVEL%"

echo %APP_EXIT%> "%RUN_DIR%\launcher_exit_code.txt"
wevtutil epl Application "%RUN_DIR%\application.evtx" /ow:true /q:"*[System[TimeCreated[timediff(@SystemTime) ^<= 1800000]]]" > "%RUN_DIR%\event_export.log" 2>&1
powershell -NoProfile -ExecutionPolicy Bypass -Command ^
  "$start=(Get-Date).AddMinutes(-30); Get-WinEvent -FilterHashtable @{LogName='Application'; StartTime=$start} -ErrorAction SilentlyContinue | Where-Object { $_.ProviderName -in @('Application Error','Windows Error Reporting','Application Hang') -or $_.Message -match 'yuukilyrics|flutter' } | Format-List TimeCreated,ProviderName,Id,LevelDisplayName,Message | Out-File -Width 500 -Encoding utf8 '%RUN_DIR%\events.txt'"

set "ZIP_PATH=%~dp0YuukiLyrics_Diagnostics_%RUNSTAMP%.zip"
powershell -NoProfile -ExecutionPolicy Bypass -Command "Compress-Archive -LiteralPath '%RUN_DIR%' -DestinationPath '%ZIP_PATH%' -Force"

echo.
echo 采集完成。
echo 请把以下文件发给开发者：
echo %ZIP_PATH%
echo.
pause
endlocal
