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

echo.
echo  =========================================
echo    DBX CORM - Gitea Push
echo    https://gitea.no1king.freeddns.org/king/dbx-corm
echo  =========================================
echo.

REM 민감 파일 추적 확인
echo  [1/5] Checking for sensitive files...
set FOUND=0
for %%F in (
    .env
    cafe24_tokens.json
    manual-ordering.json
) do (
    git ls-files --error-unmatch "%%F" >nul 2>&1
    if not errorlevel 1 (
        echo  [ERROR] Sensitive file is tracked by git: %%F
        set FOUND=1
    )
)
if %FOUND%==1 (
    echo.
    echo  PUSH BLOCKED. Remove with:
    echo    git rm --cached ^<filename^>
    echo    git commit -m "remove sensitive file"
    echo.
    pause
    exit /b 1
)
echo   OK.
echo.

REM Git 상태
echo  [2/5] Git status:
echo  -----------------------------------------
git status -s
echo  -----------------------------------------
echo.

REM 변경사항 없으면 종료
git diff --quiet
set DIFF1=%errorlevel%
git diff --cached --quiet
set DIFF2=%errorlevel%
git ls-files --others --exclude-standard | findstr /r "." >nul
set DIFF3=%errorlevel%

if "%DIFF1%"=="0" if "%DIFF2%"=="0" if not "%DIFF3%"=="0" (
    echo  No changes to commit. Exiting.
    pause
    exit /b 0
)

REM 스테이징
echo  [3/5] Staging files...
git add -A
echo   OK.
echo.

REM 커밋 메시지 입력
echo  [4/5] Enter commit message:
set /p MSG=^>^>

if "%MSG%"=="" (
    echo  [ERROR] Empty commit message.
    pause
    exit /b 1
)

git commit -m "%MSG%"
if errorlevel 1 (
    echo  [ERROR] Commit failed.
    pause
    exit /b 1
)
echo.

REM Push
echo  [5/5] Pushing to Gitea...
git push -u origin master
if errorlevel 1 (
    git push -u origin main
)

if %errorlevel%==0 (
    echo.
    echo  =========================================
    echo    [OK] Push complete!
    echo    https://gitea.no1king.freeddns.org/king/dbx-corm
    echo  =========================================
) else (
    echo.
    echo  [ERROR] Push failed. Check credentials or network.
)
echo.
pause
