@echo off
setlocal enabledelayedexpansion

rem Script generic de git add/commit/push cu meniu interactiv.
rem Functioneaza neschimbat in orice produs unde e copiat COMUN
rem (ROACONT, ROAGEST, ROACASA, ...), pentru ca detecteaza dinamic
rem folderul COMUN si folderul proiectului (parintele lui COMUN)
rem din locatia proprie (COMUN\scripts\git-menu.bat).

set "SCRIPT_DIR=%~dp0"
for %%I in ("%SCRIPT_DIR%..") do set "COMUN_DIR=%%~fI"
for %%I in ("%COMUN_DIR%\..") do set "PROIECT_DIR=%%~fI"

rem culori ANSI (suportate nativ de consola Windows 10/11)
for /F "delims=" %%E in ('echo prompt $E^|cmd') do set "ESC=%%E"
set "C_RESET=%ESC%[0m"
set "C_TITLE=%ESC%[1;96m"
set "C_SECTION=%ESC%[96m"
set "C_WARN=%ESC%[1;93m"
set "C_ERR=%ESC%[1;91m"
set "C_OK=%ESC%[1;92m"
set "C_PROMPT=%ESC%[93m"

:MENIU
cls
echo %C_SECTION%============================================%C_RESET%
echo %C_TITLE%  Git helper - add / commit / push%C_RESET%
echo %C_SECTION%============================================%C_RESET%
echo   1. COMUN    ( %COMUN_DIR% )
echo   2. Proiect  ( %PROIECT_DIR% )
echo   3. Iesire
echo %C_SECTION%============================================%C_RESET%
set "ALEGERE="
set /p ALEGERE="Alege optiune (1-3): "

if "%ALEGERE%"=="1" (
    set "TARGET=%COMUN_DIR%"
    goto VERIFICA
)
if "%ALEGERE%"=="2" (
    set "TARGET=%PROIECT_DIR%"
    goto VERIFICA
)
if "%ALEGERE%"=="3" goto SFARSIT

echo Optiune invalida.
pause
goto MENIU

:VERIFICA
pushd "%TARGET%"
git rev-parse --is-inside-work-tree >nul 2>&1
if errorlevel 1 (
    echo.
    echo %C_ERR%[EROARE] "%TARGET%" nu este un repository git.%C_RESET%
    echo.
    popd
    pause
    goto MENIU
)

rem verifica daca branch-ul local e in urma fata de upstream
set "BEHIND=0"
for /f %%N in ('git rev-list --count HEAD..@{u} 2^>nul') do set "BEHIND=%%N"
if not "%BEHIND%"=="0" (
    echo.
    echo %C_WARN%[ATENTIE] Branch-ul local e in urma cu %BEHIND% commit-uri fata de upstream.%C_RESET%
)
if not "%BEHIND%"=="0" (
    set "CONFIRM_PULL="
)
if not "%BEHIND%"=="0" set /p CONFIRM_PULL="Faci 'git pull' acum? (D/N): "
if not "%BEHIND%"=="0" if /I "%CONFIRM_PULL%"=="D" (
    echo.
    git pull
    if errorlevel 1 (
        echo %C_ERR%[EROARE] git pull a esuat - rezolva manual inainte sa continui.%C_RESET%
        popd
        pause
        goto MENIU
    )
    echo.
    echo %C_OK%git pull finalizat.%C_RESET%
    pause
)

goto STATUS

:STATUS
cls
echo %C_SECTION%============================================%C_RESET%
echo %C_TITLE%  Repository: %TARGET%%C_RESET%
echo %C_SECTION%============================================%C_RESET%
git status
echo %C_SECTION%============================================%C_RESET%
echo.

set "HASCHANGES=0"
for /f "delims=" %%S in ('git status --porcelain 2^>nul') do set "HASCHANGES=1"
if "%HASCHANGES%"=="0" (
    echo %C_OK%Nu sunt modificari - nimic de adaugat.%C_RESET%
    popd
    pause
    goto MENIU
)

set "CONFIRM_ADD="
set /p CONFIRM_ADD="%C_PROMPT%Faci 'git add -A' cu fisierele de mai sus? (D/N): %C_RESET%"
if /I not "%CONFIRM_ADD%"=="D" (
    echo Anulat - nu s-a facut git add.
    popd
    pause
    goto MENIU
)

git add -A
if errorlevel 1 (
    echo %C_ERR%[EROARE] git add a esuat.%C_RESET%
    popd
    pause
    goto MENIU
)
echo.
echo %C_OK%git add -A finalizat.%C_RESET%
pause

cls
echo %C_SECTION%============================================%C_RESET%
echo %C_TITLE%  Fisiere puse in staging ^(git add -A^)%C_RESET%
echo %C_SECTION%============================================%C_RESET%
git status
echo %C_SECTION%============================================%C_RESET%
echo.
set "CONFIRM_COMMIT="
set /p CONFIRM_COMMIT="%C_PROMPT%Faci commit cu fisierele de mai sus? (D/N): %C_RESET%"
if /I not "%CONFIRM_COMMIT%"=="D" (
    echo Anulat - fisierele raman in staging ^(git add^), nu s-a facut commit.
    popd
    pause
    goto MENIU
)

set "MESAJ="
set /p MESAJ="%C_PROMPT%Mesaj commit: %C_RESET%"
if "%MESAJ%"=="" (
    echo %C_ERR%Mesajul de commit nu poate fi gol. Commit anulat.%C_RESET%
    popd
    pause
    goto MENIU
)

git commit -m "%MESAJ%"
if errorlevel 1 (
    echo %C_ERR%[EROARE] Commit-ul a esuat.%C_RESET%
    popd
    pause
    goto MENIU
)

for /f "tokens=*" %%B in ('git branch --show-current') do set "BRANCH=%%B"

echo.
set "CONFIRM_PUSH="
set /p CONFIRM_PUSH="%C_PROMPT%Faci push pe branch-ul '%BRANCH%' catre origin? (D/N): %C_RESET%"
if /I not "%CONFIRM_PUSH%"=="D" (
    echo Commit facut local, push omis.
    popd
    pause
    goto MENIU
)

git push origin %BRANCH%
if errorlevel 1 (
    echo %C_ERR%[EROARE] Push-ul a esuat.%C_RESET%
    popd
    pause
    goto MENIU
)

echo.
echo %C_OK%Gata - add, commit si push finalizate.%C_RESET%
popd
pause
goto MENIU

:SFARSIT
endlocal
exit /b 0
