@echo off
setlocal EnableExtensions

set "ROOT=%~dp0"
if "%ROOT:~-1%"=="\" set "ROOT=%ROOT:~0,-1%"

set "DIST_DIR=%ROOT%\dist"
set "DIST_PUBLIC=%DIST_DIR%\public"
set "DIST_APP=%DIST_PUBLIC%"
set "FRONTEND_DIR=%ROOT%\frontend"
set "BACKEND_DIR=%ROOT%\backend"

echo [1/8] Cleaning dist...
if exist "%DIST_DIR%" (
	rmdir /S /Q "%DIST_DIR%"
	if exist "%DIST_DIR%" (
		echo ERROR: Failed to remove "%DIST_DIR%".
		exit /b 1
	)
)

mkdir "%DIST_DIR%" >nul 2>&1 || (
	echo ERROR: Failed to create "%DIST_DIR%".
	exit /b 1
)

echo [2/8] Installing backend dependencies... SKIPED
@REM pushd "%BACKEND_DIR%" >nul 2>&1 || (
@REM 	echo ERROR: Backend directory not found: "%BACKEND_DIR%".
@REM 	exit /b 1
@REM )

@REM call composer install --no-dev --optimize-autoloader
@REM set "COMPOSER_EXIT=%ERRORLEVEL%"
@REM popd >nul
@REM if not "%COMPOSER_EXIT%"=="0" (
@REM 	echo ERROR: composer install failed.
@REM 	exit /b 1
@REM )

echo [3/8] Regenerating frontend API client... SKIPED
@REM call php "%BACKEND_DIR%\scripts\buildTypeScript.php"
@REM if not "%ERRORLEVEL%"=="0" (
@REM 	echo ERROR: TypeScript API client generation failed.
@REM 	exit /b 1
@REM )

echo [4/8] Building frontend...
pushd "%FRONTEND_DIR%" >nul 2>&1 || (
	echo ERROR: Frontend directory not found: "%FRONTEND_DIR%".
	exit /b 1
)

if exist "%FRONTEND_DIR%\node_modules" (
	echo - npm install
	call npm.cmd install
) else (
	echo - npm ci
	call npm.cmd ci
)
set "NPM_INSTALL_EXIT=%ERRORLEVEL%"
if not "%NPM_INSTALL_EXIT%"=="0" (
	popd >nul
	echo ERROR: npm dependency install failed.
	exit /b 1
)

echo - npm run build
call npm.cmd run build
set "NPM_BUILD_EXIT=%ERRORLEVEL%"
if not "%NPM_BUILD_EXIT%"=="0" (
	popd >nul
	echo ERROR: npm run build failed.
	exit /b 1
)

if not exist "%FRONTEND_DIR%\dist" (
	popd >nul
	echo ERROR: Frontend build output not found at "%FRONTEND_DIR%\dist".
	exit /b 1
)

popd >nul

echo [5/8] Copy backend root to dist/... SKIPED
@REM call :RunRobocopy "%BACKEND_DIR%" "%DIST_DIR%" /E /R:2 /W:1 /NFL /NDL /NJH /NJS /NP ^
@REM 	/XD "%BACKEND_DIR%\.git" "%BACKEND_DIR%\.vscode" "%BACKEND_DIR%\tests" "%BACKEND_DIR%\node_modules" "%BACKEND_DIR%\frontend" "%BACKEND_DIR%\dist" ^
@REM 	/XF ".env" ".env.*"
@REM if errorlevel 1 exit /b 1

echo [6/8] Copy frontend/dist to dist/public...
call :RunRobocopy "%FRONTEND_DIR%\dist" "%DIST_APP%" /E /R:2 /W:1 /NFL /NDL /NJH /NJS /NP
if errorlevel 1 exit /b 1

echo [7/8] Applying .env.production (if present)...
set "ENV_NOTE=No backend/.env.production found"
if exist "%BACKEND_DIR%\.env.production" (
	copy /Y "%BACKEND_DIR%\.env.production" "%DIST_DIR%\.env" >nul
	if errorlevel 1 (
		echo ERROR: Failed to copy backend/.env.production to dist/.env.
		exit /b 1
	)
	set "ENV_NOTE=Copied backend/.env.production to dist/.env"
)

echo [8/8] Summary
echo - Frontend build: OK
echo - Backend root copied to: "%DIST_DIR%" (excluding excluded dirs)
echo - Frontend assets copied to: "%DIST_APP%"
echo - Env: %ENV_NOTE%
echo - DocumentRoot should be: "%DIST_PUBLIC%"
echo - Frontend app is served from: "%DIST_APP%"
echo DONE "%DIST_DIR%"
exit /b 0

:RunRobocopy
robocopy %*
if errorlevel 8 (
	echo ERROR: robocopy failed with exit code %ERRORLEVEL%.
	exit /b 1
)
exit /b 0
