@echo off
cd /d "%~dp0"
title Kling Motion Bot RDP Auto Setup & Run

:: Tambahkan path default Node.js secara sementara jika belum terbaca di environment
if exist "C:\Program Files\nodejs" (
    set "PATH=%PATH%;C:\Program Files\nodejs"
)

echo ==========================================================
echo   AUTO-SETUP & RUN KLING MOTION AUTO-TOKEN BOT FOR RDP
echo ==========================================================
echo.

:: Check Node.js installation menggunakan node -v
node -v >nul 2>&1
if %errorlevel% neq 0 (
    echo [ERROR] Node.js belum terinstall atau belum terdeteksi!
    echo Silakan install Node.js terlebih dahulu (LTS Version) dari nodejs.org.
    echo.
    echo JIKA BARU SAJA MENGINSTALL: Silakan Sign Out dari RDP lalu masuk kembali
    echo agar Windows memuat ulang pengaturan environment PATH yang baru.
    echo.
    pause
    exit /b 1
)

echo [1/5] Menginstal dependensi Node.js (npm install)...
call npm install
if %errorlevel% neq 0 (
    echo [ERROR] Gagal menginstal dependensi!
    pause
    exit /b 1
)

echo.
echo [2/5] Mengunduh Chromium Browser untuk Playwright...
call npx playwright install chromium
if %errorlevel% neq 0 (
    echo [ERROR] Gagal mengunduh browser Chromium!
    pause
    exit /b 1
)

echo.
echo [3/5] Membuka port 3001 di Windows Firewall (memerlukan hak Administrator)...
powershell -Command "Start-Process powershell -ArgumentList 'New-NetFirewallRule -DisplayName \"Kling Bot Port 3001\" -Direction Inbound -LocalPort 3001 -Protocol TCP -Action Allow' -Verb RunAs"
echo Request pembukaan port 3001 telah dikirim ke Windows Firewall.

echo.
echo [4/5] Mengatur file konfigurasi (.env)...
if not exist .env (
    copy .env.example .env >nul
)

:: Update configurations in .env using PowerShell
powershell -Command "(Get-Content .env) -replace 'AUTO_TOKEN_ENABLED=.*', 'AUTO_TOKEN_ENABLED=true' -replace 'PLAYWRIGHT_HEADLESS=.*', 'PLAYWRIGHT_HEADLESS=true' -replace 'PORT=.*', 'PORT=3001' | Set-Content .env"

:: Check and append REMOTE_SERVER_URL and REMOTE_ADMIN_PASSWORD if not present
findstr /C:"REMOTE_SERVER_URL" .env >nul
if %errorlevel% neq 0 (
    echo. >> .env
    echo REMOTE_SERVER_URL=https://teknolabs.texaproject.com >> .env
    echo REMOTE_ADMIN_PASSWORD=4rg0v14N >> .env
)

echo File .env berhasil dikonfigurasi!
echo PENTING: Bot akan otomatis mengirim token hasil panen ke:
echo https://tekno-kling.texaproject.com

echo.
echo [5/5] Memasang PM2 & Menjalankan Bot 24 Jam...
call npm install -g pm2
if %errorlevel% neq 0 (
    echo [WARNING] Gagal memasang PM2 secara global. Menjalankan langsung dengan Node...
    echo Browser akan berjalan headless di background. Jangan tutup CMD ini!
    node server.js
) else (
    call pm2 stop kling-bot >nul 2>&1
    call pm2 delete kling-bot >nul 2>&1
    call pm2 start server.js --name "kling-bot"
    call pm2 save
    echo.
    echo ==========================================================
    echo   SUKSES! Server & Bot berjalan di latar belakang (PM2)
    echo ==========================================================
    echo Untuk melihat log bot, jalankan perintah: pm2 logs kling-bot
    echo.
)
pause
