@echo off
REM JCF deployment: build locally, upload over SSH, and deploy on Linux.
REM Usage: push.bat user@host [stage-only|with-env]

setlocal DisableDelayedExpansion
cd /d "%~dp0..\.."
if "%~1"=="" (
  echo Usage: push.bat user@host [stage-only^|with-env]
  echo Example: push.bat root@1.2.3.4
  exit /b 1
)
set "REMOTE=%~1"
set "REMOTE_ROOT=/opt/juyou_ai"
set "PACKAGE_ENV="
if "%~2"=="with-env" set "PACKAGE_ENV=.env"
if not "%~2"=="" if not "%~2"=="stage-only" if not "%~2"=="with-env" (
  echo The second argument can only be stage-only or with-env.
  exit /b 1
)
if "%~2"=="with-env" if not exist "API\.env" (
  echo Missing API\.env. Deployment environment cannot be uploaded.
  exit /b 1
)
if not "%~3"=="" (
  echo Too many arguments.
  exit /b 1
)
set "ARCHIVE=%TEMP%\juyou-deploy-%RANDOM%-%RANDOM%.tar.gz"
set "REMOTE_ARCHIVE=/tmp/juyou-deploy.tar.gz"
set "REMOTE_ENV=/tmp/juyou_ai.env"
set "PACKAGE_MODEL="

echo [0/5] Checking offline Whisper model...
ssh "%REMOTE%" "test -f %REMOTE_ROOT%/models/faster-whisper-small/config.json && test -f %REMOTE_ROOT%/models/faster-whisper-small/model.bin && test -f %REMOTE_ROOT%/models/faster-whisper-small/tokenizer.json && test -f %REMOTE_ROOT%/models/faster-whisper-small/vocabulary.txt" >nul 2>nul
if errorlevel 1 (
  python "API\deploy\download_whisper_model.py"
  if errorlevel 1 exit /b 1
  set "PACKAGE_MODEL=models"
) else (
  echo Offline Whisper model already exists on the server.
)

echo [1/5] Building WEB and ADMIN...
call npm --prefix WEB run build
if errorlevel 1 exit /b 1
call npm --prefix ADMIN run build
if errorlevel 1 exit /b 1

echo [2/5] Building Linux amd64 API...
pushd API
set "GOOS=linux"
set "GOARCH=amd64"
go build -trimpath -ldflags "-s -w" -o bin/jcf-api ./cmd/api
if errorlevel 1 (popd & exit /b 1)
popd

echo [3/5] Packaging deployment files...
tar --exclude=.git --exclude=tools/ffmpeg --exclude=deploy/.deploy.conf -C API -czf "%ARCHIVE%" bin static deploy migrations acme workers %PACKAGE_MODEL% %PACKAGE_ENV%
if errorlevel 1 exit /b 1

echo [4/5] Uploading and staging deployment files...
scp "%ARCHIVE%" "%REMOTE%:%REMOTE_ARCHIVE%"
if errorlevel 1 exit /b 1
ssh "%REMOTE%" "bash -lc 'set -e; WORK=/tmp/juyou-deploy-work; ENV_UPLOAD=%REMOTE_ENV%; cleanup(){ rm -rf $WORK %REMOTE_ARCHIVE%; }; trap cleanup EXIT; rm -rf $WORK; mkdir -p $WORK; tar -xzf %REMOTE_ARCHIVE% -C $WORK; if [ -f $WORK/.env ]; then install -m 600 $WORK/.env $ENV_UPLOAD; else rm -f $ENV_UPLOAD; fi; rm -f $WORK/.env; mkdir -p %REMOTE_ROOT%; if command -v rsync >/dev/null 2>&1; then rsync -a --delete --exclude=/.env --exclude=/media/ --exclude=/backups/ --exclude=/models/ $WORK/ %REMOTE_ROOT%/; if [ -d $WORK/models ]; then mkdir -p %REMOTE_ROOT%/models; rsync -a $WORK/models/ %REMOTE_ROOT%/models/; fi; else cp -a $WORK/. %REMOTE_ROOT%/; fi; echo upload complete; echo run manually: bash %REMOTE_ROOT%/deploy/one_click_deployment.sh'"
if errorlevel 1 exit /b 1

echo [5/5] Cleaning local temporary archive...
del "%ARCHIVE%" >nul 2>nul
echo Deployment push complete.
