@echo off
setlocal enabledelayedexpansion

:: ------------------------------------------------------------------------------
:: Unity Build Script (misc/build.bat)
:: Step 1: Use FASTBuild to generate unity .cpp files directly in Intermediate/
:: Step 2: Invoke compiler directly on target unity files
:: ------------------------------------------------------------------------------

set SCRIPT_DIR=%~dp0
cd /d "%SCRIPT_DIR%.."

set FBUILD=misc\fbuild.exe
set HELPER=powershell -NoProfile -ExecutionPolicy Bypass -File "%SCRIPT_DIR%build_helper.ps1"

set TARGET_JULIET=0
set TARGET_ROMEO=0
set TARGET_SHADER=0
set TARGET_GAME=0
set TARGET_SPECIFIED=0

set CONFIG_DEBUG=0
set CONFIG_RELEASE=0
set CONFIG_PROFILE=0
set CONFIG_SPECIFIED=0

:parse_args
if "%~1"=="" goto check_defaults
if /i "%~1"=="debug" set CONFIG_DEBUG=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="release" set CONFIG_RELEASE=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="profile" set CONFIG_PROFILE=1& set CONFIG_SPECIFIED=1
if /i "%~1"=="juliet" set TARGET_JULIET=1& set TARGET_SPECIFIED=1
if /i "%~1"=="game" set TARGET_GAME=1& set TARGET_SPECIFIED=1
if /i "%~1"=="romeo" set TARGET_ROMEO=1& set TARGET_SPECIFIED=1
if /i "%~1"=="shadercompiler" set TARGET_SHADER=1& set TARGET_SPECIFIED=1
shift
goto parse_args

:check_defaults
if !CONFIG_SPECIFIED! equ 0 (
    if !TARGET_SPECIFIED! equ 0 (
        set CONFIG_DEBUG=1& set CONFIG_RELEASE=1& set CONFIG_PROFILE=1
        set TARGET_JULIET=1& set TARGET_GAME=1& set TARGET_ROMEO=1& set TARGET_SHADER=1
        goto start_build
    )
    set CONFIG_DEBUG=1
)
if !TARGET_SPECIFIED! equ 0 (
    set TARGET_JULIET=1& set TARGET_GAME=1& set TARGET_ROMEO=1& set TARGET_SHADER=1
)

:start_build
%HELPER% -Action StartBuild >nul 2>&1
if exist "Intermediate\built_outputs.tmp" del /f /q "Intermediate\built_outputs.tmp" >nul 2>&1

if not exist "%FBUILD%" (
    echo [BUILD FAILED] Could not find %FBUILD%.
    exit /b 1
)

set COMPILER=clang-cl.exe
where %COMPILER% >nul 2>nul
if !ERRORLEVEL! neq 0 (
    set COMPILER=cl.exe
    where %COMPILER% >nul 2>nul
    if !ERRORLEVEL! neq 0 (
        echo [BUILD FAILED] Neither clang-cl.exe nor cl.exe was found in PATH.
        exit /b 1
    )
)

if !CONFIG_DEBUG! equ 1 call :build_config Debug
if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!

if !CONFIG_PROFILE! equ 1 call :build_config Profile
if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!

if !CONFIG_RELEASE! equ 1 call :build_config Release
if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!

echo.
echo ==============================================================================
echo [BUILD SUMMARY]
echo ==============================================================================
echo Compiler Used : %COMPILER%
%HELPER% -Action FinishBuild
echo ==============================================================================
echo [ALL BUILDS SUCCEEDED] All requested targets processed successfully.
echo ==============================================================================
exit /b 0

:: ------------------------------------------------------------------------------
:: Build Configuration Subroutine
:: ------------------------------------------------------------------------------
:build_config
set "CFG=%~1"

echo.
echo ==============================================================================
echo Generating Unity Files into Intermediate/ via FASTBuild [%CFG%]...
echo ==============================================================================

set FASTBUILD_TARGETS=Juliet-Unity
if /i not "!CFG!"=="Release" set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! ImGui-Unity
if !TARGET_JULIET! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! JulietApp-Unity Game-Unity
if !TARGET_GAME! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! Game-Unity
if !TARGET_ROMEO! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! Romeo-Unity
if !TARGET_SHADER! equ 1 set FASTBUILD_TARGETS=!FASTBUILD_TARGETS! JulietShaderCompiler-Unity

%HELPER% -Action StartUnity >nul 2>&1

"%FBUILD%" !FASTBUILD_TARGETS!
if !ERRORLEVEL! neq 0 (
    echo [BUILD FAILED] FASTBuild failed to generate unity files for %CFG%.
    exit /b !ERRORLEVEL!
)

:: Evaluate all target needs in a single fast PowerShell pass
%HELPER% -Action EvaluateConfig -CFG "!CFG!" -TargetJuliet "!TARGET_JULIET!" -TargetGame "!TARGET_GAME!" -TargetRomeo "!TARGET_ROMEO!" -TargetShader "!TARGET_SHADER!" >nul 2>&1

set FASTBUILD_UNITY_CHANGED=0
set JULIET_NEED_COMPILE=0
set IMGUI_NEED_COMPILE=0
set GAME_NEED_COMPILE=0
set JULIETAPP_NEED_COMPILE=0
set ROMEO_NEED_COMPILE=0
set SHADER_NEED_COMPILE=0

if exist "Intermediate\config_status.tmp" (
    for /f "usebackq tokens=1,2 delims==" %%A in ("Intermediate\config_status.tmp") do set %%A=%%B
    del /f /q "Intermediate\config_status.tmp" >nul 2>&1
)

if !FASTBUILD_UNITY_CHANGED! equ 1 (
    echo [FASTBuild] Unity structure or source file list changed.
) else (
    echo [FASTBuild] No Unity file structure changes detected [up-to-date].
)

set ANY_NEED_BUILD=0
if !JULIET_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1
if !IMGUI_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1
if !GAME_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1
if !JULIETAPP_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1
if !ROMEO_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1
if !SHADER_NEED_COMPILE! equ 1 set ANY_NEED_BUILD=1

if !ANY_NEED_BUILD! equ 0 (
    echo [SKIP / UP-TO-DATE] All requested targets in [%CFG%] are up-to-date.
    exit /b 0
)

echo.
echo ==============================================================================
echo Compiling Unity Files directly via %COMPILER% [%CFG%]...
echo ==============================================================================

if not exist "bin\x64-!CFG!" mkdir "bin\x64-!CFG!"
if not exist "Intermediate\x64-!CFG!" mkdir "Intermediate\x64-!CFG!"

set COMMON_FLAGS=/nologo /std:c++20 /W4 /EHa- /utf-8 /DUNICODE /D_UNICODE /DWIN32_LEAN_AND_MEAN /D_CRT_SECURE_NO_WARNINGS /I"Juliet/include" /I"Juliet/src" /I"Game" /I"External/imgui" /I"External/imgui/backends" /DJULIET_WIN32 /wd5267 /wd4061 /wd4505 /wd4514 /wd4577 /wd4625 /wd4710 /wd4711 /wd4746 /wd4820 /wd5045 /wd5220 /wd5245 /wd4626 /wd5026 /wd5027 /wd4530 /Fd"Intermediate\x64-!CFG!\vc140.pdb"

if /i "!CFG!"=="Debug" (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MDd /Od /Zi /Gw /Gy /Zc:inline /DDEBUG /DPROFILING_ENABLED /DJULIET_ENABLE_IMGUI /DJULIET_DEBUG
    set IMGUI_UNITY_SRCS=Intermediate\External\imgui\*.cpp
) else if /i "!CFG!"=="Profile" (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MD /O2 /Zi /Gw /Gy /Zc:inline /DRELEASE /DPROFILING_ENABLED /DJULIET_ENABLE_IMGUI
    set IMGUI_UNITY_SRCS=Intermediate\External\imgui\*.cpp
) else (
    set COMPILER_FLAGS=!COMMON_FLAGS! /MD /O2 /Gw /Gy /Zc:inline /DRELEASE
    set IMGUI_UNITY_SRCS=
)

set COMMON_LIBS=kernel32.lib user32.lib gdi32.lib dxguid.lib Ws2_32.lib dxgi.lib imm32.lib dwmapi.lib d3dcompiler.lib shell32.lib winhttp.lib delayimp.lib

:: --- Juliet Shared Objs ---
echo.
echo --- Compiling Shared Object Files (Juliet / ImGui) [%CFG%] ---
if !JULIET_NEED_COMPILE! equ 0 (
    echo [SKIP / UP-TO-DATE] Intermediate\x64-!CFG!\Juliet_Unity*.obj [Unity files unchanged]
) else (
    echo [REBUILDING] Compiling Intermediate\x64-!CFG!\Juliet_Unity*.obj...
    %COMPILER% %COMPILER_FLAGS% /DJULIET_EXPORT /c Intermediate\Juliet\*.cpp /Fo"Intermediate\x64-!CFG!\\"
    if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
)

:: --- ImGui Shared Objs ---
if defined IMGUI_UNITY_SRCS (
    if !IMGUI_NEED_COMPILE! equ 0 (
        echo [SKIP / UP-TO-DATE] Intermediate\x64-!CFG!\ImGui_Unity*.obj [Unity files unchanged]
    ) else (
        echo [REBUILDING] Compiling Intermediate\x64-!CFG!\ImGui_Unity*.obj...
        %COMPILER% %COMPILER_FLAGS% /c !IMGUI_UNITY_SRCS! /Fo"Intermediate\x64-!CFG!\\"
        if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
    )
)

set JULIET_OBJS=Intermediate\x64-!CFG!\Juliet_Unity*.obj
set IMGUI_OBJS=
if defined IMGUI_UNITY_SRCS set IMGUI_OBJS=Intermediate\x64-!CFG!\ImGui_Unity*.obj

set BUILD_GAME_DLL=0
if !TARGET_JULIET! equ 1 set BUILD_GAME_DLL=1
if !TARGET_GAME! equ 1 set BUILD_GAME_DLL=1

:: --- Game.dll ---
if !BUILD_GAME_DLL! equ 1 (
    echo.
    echo --- Building Game.dll [%CFG%] ---
    if !GAME_NEED_COMPILE! equ 0 (
        echo [SKIP / UP-TO-DATE] bin\x64-!CFG!\Game.dll [Binary up to date, skipping compilation]
    ) else (
        echo [REBUILDING] Compiling bin\x64-!CFG!\Game.dll...
        %COMPILER% %COMPILER_FLAGS% /DJULIET_EXPORT /LD /Fe"bin\x64-!CFG!\Game.dll" ^
            %JULIET_OBJS% Intermediate\Game\*.cpp %IMGUI_OBJS% ^
            /link %COMMON_LIBS% /INCREMENTAL /ILK:"Intermediate\x64-!CFG!\Game.ilk" /PDB:"Intermediate\x64-!CFG!\Game.pdb"
        if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
        echo bin\x64-!CFG!\Game.dll>>Intermediate\built_outputs.tmp
    )
)

:: --- JulietApp.exe ---
if !TARGET_JULIET! equ 1 (
    echo.
    echo --- Building JulietApp.exe [%CFG%] ---
    if !JULIETAPP_NEED_COMPILE! equ 0 (
        echo [SKIP / UP-TO-DATE] bin\x64-!CFG!\JulietApp.exe [Binary up to date, skipping compilation]
    ) else (
        echo [REBUILDING] Compiling bin\x64-!CFG!\JulietApp.exe...
        %COMPILER% %COMPILER_FLAGS% /Fe"bin\x64-!CFG!\JulietApp.exe" ^
            Intermediate\JulietApp\*.cpp %IMGUI_OBJS% ^
            /link %COMMON_LIBS% bin\x64-!CFG!\Game.lib /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:"Intermediate\x64-!CFG!\JulietApp.ilk" /PDB:"Intermediate\x64-!CFG!\JulietApp.pdb"
        if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
        echo bin\x64-!CFG!\JulietApp.exe>>Intermediate\built_outputs.tmp
    )
)

:: --- Romeo.exe ---
if !TARGET_ROMEO! equ 1 (
    echo.
    echo --- Building Romeo.exe [%CFG%] ---
    if !ROMEO_NEED_COMPILE! equ 0 (
        echo [SKIP / UP-TO-DATE] bin\x64-!CFG!\Romeo.exe [Binary up to date, skipping compilation]
    ) else (
        echo [REBUILDING] Compiling bin\x64-!CFG!\Romeo.exe...
        %COMPILER% %COMPILER_FLAGS% /Fe"bin\x64-!CFG!\Romeo.exe" ^
            %JULIET_OBJS% Intermediate\Romeo\*.cpp %IMGUI_OBJS% ^
            /link %COMMON_LIBS% /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:"Intermediate\x64-!CFG!\Romeo.ilk" /PDB:"Intermediate\x64-!CFG!\Romeo.pdb"
        if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
        echo bin\x64-!CFG!\Romeo.exe>>Intermediate\built_outputs.tmp
    )
)

:: --- JulietShaderCompiler.exe ---
if !TARGET_SHADER! equ 1 (
    echo.
    echo --- Building JulietShaderCompiler.exe [%CFG%] ---
    if exist "Intermediate\JulietShaderCompiler\*.cpp" (
        if !SHADER_NEED_COMPILE! equ 0 (
            echo [SKIP / UP-TO-DATE] bin\x64-!CFG!\JulietShaderCompiler.exe [Binary up to date, skipping compilation]
        ) else (
            echo [REBUILDING] Compiling bin\x64-!CFG!\JulietShaderCompiler.exe...
            %COMPILER% %COMPILER_FLAGS% /I"JulietShaderCompiler" /Fe"bin\x64-!CFG!\JulietShaderCompiler.exe" ^
                %JULIET_OBJS% Intermediate\JulietShaderCompiler\*.cpp %IMGUI_OBJS% ^
                /link %COMMON_LIBS% dxcompiler.lib /DELAYLOAD:dxcompiler.dll /SUBSYSTEM:CONSOLE /INCREMENTAL /ILK:"Intermediate\x64-!CFG!\JulietShaderCompiler.ilk" /PDB:"Intermediate\x64-!CFG!\JulietShaderCompiler.pdb"
            if !ERRORLEVEL! neq 0 exit /b !ERRORLEVEL!
            echo bin\x64-!CFG!\JulietShaderCompiler.exe>>Intermediate\built_outputs.tmp
        )
    )
)

exit /b 0
