@echo off
chcp 65001 >nul
title Cai dat tu dong DNB Scraper - Node.js + openzca
color 0A
setlocal EnableExtensions EnableDelayedExpansion

:: ============================================================
::  DNB Scraper - One Click Installer v2
::  Tu dong cai Node.js LTS + npm + openzca
::  Luon giu cua so, khong tu tat khi loi
:: ============================================================

set "LOG=%TEMP%\dnb_bo_tro_install.log"
set "SCRIPT=%~f0"

:: Neu chua co quyen admin thi mo lai bang cmd /k de KHONG bi tat cua so
net session >nul 2>&1
if %errorlevel% neq 0 (
    echo ============================================================
    echo   DNB SCRAPER - CAI DAT BO TRO
    echo ============================================================
    echo.
    echo File dang xin quyen Administrator...
    echo Neu Windows hien hop thoai User Account Control, anh bam YES.
    echo.
    powershell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process -FilePath 'cmd.exe' -ArgumentList '/k','""%SCRIPT%"" --admin' -Verb RunAs"
    if %errorlevel% neq 0 (
        echo.
        echo [LOI] Khong mo duoc cua so Administrator.
        echo Anh thu bam chuot phai vao file .bat ^> Run as administrator.
        echo.
        pause
    )
    exit /b
)

cls
echo ============================================================
echo   CAI DAT TU DONG BO TRO DNB SCRAPER v2
echo   Node.js LTS + npm + openzca CLI
echo ============================================================
echo.
echo Cua so nay se KHONG tu tat. Neu co loi, anh chup man hinh gui em.
echo Log cai dat: %LOG%
echo.
echo ==== DNB installer started %DATE% %TIME% ==== > "%LOG%"

:: PATH pho bien cua Node/npm global
set "PATH=%ProgramFiles%\nodejs;%ProgramFiles(x86)%\nodejs;%APPDATA%\npm;%PATH%"

echo [1/6] Kiem tra Node.js...
node --version >nul 2>&1
if %errorlevel% equ 0 (
    for /f "tokens=*" %%i in ('node --version') do set "NODE_VER=%%i"
    echo   Node.js da co: !NODE_VER!
    echo Node existing !NODE_VER! >> "%LOG%"
    goto CHECK_NPM
)

echo   Chua co Node.js. Bat dau cai tu dong...
echo.

echo [2/6] Thu cai Node.js LTS bang winget...
winget --version >nul 2>&1
if %errorlevel% equ 0 (
    echo   Dang chay winget install OpenJS.NodeJS.LTS...
    winget install --id OpenJS.NodeJS.LTS -e --accept-source-agreements --accept-package-agreements >> "%LOG%" 2>&1
    if %errorlevel% equ 0 (
        echo   Cai Node.js bang winget OK.
    ) else (
        echo   Winget cai that bai. Se thu tai MSI tu nodejs.org...
        echo Winget install failed >> "%LOG%"
    )
) else (
    echo   May chua co winget. Se tai MSI tu nodejs.org...
    echo winget not found >> "%LOG%"
)

set "PATH=%ProgramFiles%\nodejs;%ProgramFiles(x86)%\nodejs;%APPDATA%\npm;%PATH%"
node --version >nul 2>&1
if %errorlevel% equ 0 goto CHECK_NPM

echo.
echo [3/6] Tai va cai Node.js LTS MSI truc tiep...
set "MSI=%TEMP%\node-lts-x64.msi"
powershell -NoProfile -ExecutionPolicy Bypass -Command "$ErrorActionPreference='Stop'; $index=Invoke-RestMethod 'https://nodejs.org/dist/index.json'; $v=($index | Where-Object { $_.lts -ne $false -and $_.files -contains 'win-x64-msi' } | Select-Object -First 1).version; $url='https://nodejs.org/dist/'+$v+'/node-'+$v+'-x64.msi'; Write-Host 'Downloading' $url; Invoke-WebRequest $url -OutFile '%MSI%'" >> "%LOG%" 2>&1
if %errorlevel% neq 0 (
    echo.
    echo [LOI] Khong tai duoc Node.js MSI.
    echo Kiem tra internet roi chay lai file.
    echo Xem log: %LOG%
    goto END_ERROR
)

echo   Dang cai Node.js MSI, vui long doi...
msiexec /i "%MSI%" /qn /norestart >> "%LOG%" 2>&1
if %errorlevel% neq 0 (
    echo.
    echo [LOI] Cai Node.js MSI that bai.
    echo Xem log: %LOG%
    goto END_ERROR
)

echo   Cai Node.js MSI OK.
set "PATH=%ProgramFiles%\nodejs;%ProgramFiles(x86)%\nodejs;%APPDATA%\npm;%PATH%"

:CHECK_NPM
echo.
echo [4/6] Kiem tra Node.js/npm...
node --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [LOI] Chua goi duoc node sau khi cai.
    echo Anh dong cua so nay, mo lai file .bat mot lan nua.
    goto END_ERROR
)
npm --version >nul 2>&1
if %errorlevel% neq 0 (
    echo [LOI] Chua goi duoc npm sau khi cai.
    echo Anh dong cua so nay, mo lai file .bat mot lan nua.
    goto END_ERROR
)
for /f "tokens=*" %%i in ('node --version') do set "NODE_VER=%%i"
for /f "tokens=*" %%i in ('npm --version') do set "NPM_VER=%%i"
echo   Node.js OK: !NODE_VER!
echo   npm OK: !NPM_VER!
echo Node !NODE_VER! npm !NPM_VER! >> "%LOG%"

echo.
echo [5/6] Cai dat/cap nhat openzca@latest...
call npm install -g openzca@latest >> "%LOG%" 2>&1
if %errorlevel% neq 0 (
    echo.
    echo [LOI] Cai openzca that bai.
    echo Xem log: %LOG%
    goto END_ERROR
)

set "PATH=%APPDATA%\npm;%ProgramFiles%\nodejs;%PATH%"

echo.
echo [6/6] Kiem tra openzca...
where openzca
if %errorlevel% neq 0 (
    echo.
    echo [LOI] Da cai nhung chua thay openzca trong PATH.
    echo Anh dong cua so nay, mo lai file .bat mot lan nua.
    goto END_ERROR
)

openzca --version
if %errorlevel% neq 0 (
    echo.
    echo [LOI] Tim thay openzca nhung chay version chua OK.
    echo Xem log: %LOG%
    goto END_ERROR
)

echo.
echo ============================================================
echo   HOAN TAT ✅
echo ============================================================
echo Node.js/npm/openzca da san sang.
echo.
echo Buoc tiep theo:
echo 1. Mo DNB Scraper
echo 2. Vao tab Zalo ^> Cau hinh Zalo
echo 3. Bam Kiem tra cai dat
echo 4. Bam Quet QR Login
echo.
echo Cua so nay co the dong duoc roi.
echo.
pause
exit /b 0

:END_ERROR
echo.
echo ============================================================
echo   CHUA CAI XONG ❌
echo ============================================================
echo Anh chup man hinh nay gui em, hoac gui file log:
echo %LOG%
echo.
pause
exit /b 1
