@echo off
cd /d "%~dp0"

echo.
echo  =========================================
echo    DBX CORM - Gitea Push
echo  =========================================
echo.

echo  [1/4] Checking for sensitive files...

set FOUND=0
call :check_file .env
call :check_file cafe24_tokens.json
call :check_file manual-ordering.json

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.

echo  [2/4] Git status:
echo  -----------------------------------------
git status -s
echo  -----------------------------------------
echo.

echo  [3/4] Staging all changes...
git add -A
echo   OK.
echo.

echo  Enter commit message (or press Enter to skip commit and just push):
set "MSG="
set /p MSG=^>^>

if not "%MSG%"=="" (
    git diff --cached --quiet
    if errorlevel 1 (
        git commit -m "%MSG%"
        if errorlevel 1 (
            echo  [ERROR] Commit failed.
            pause
            exit /b 1
        )
    ) else (
        echo  No staged changes. Skipping commit.
    )
)
echo.

echo  [4/4] Pushing to Gitea...
git push -u origin master

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
exit /b 0

:check_file
git ls-files --error-unmatch "%~1" >nul 2>&1
if not errorlevel 1 (
    echo   [ERROR] Sensitive file is tracked by git: %~1
    set FOUND=1
)
exit /b 0
