@echo off
setlocal

REM NT8 SDK Deployment Script
REM Copies release binaries and NT8 wrapper scripts into NinjaTrader 8 Custom folders.

set "SCRIPT_DIR=%~dp0"
set "PROJECT_ROOT=%SCRIPT_DIR%.."
set "NT8_CUSTOM=%USERPROFILE%\Documents\NinjaTrader 8\bin\Custom"
set "NT8_STRATEGIES=%NT8_CUSTOM%\Strategies"
set "CORE_BIN=%PROJECT_ROOT%\src\NT8.Core\bin\Release\net48"
set "ADAPTERS_BIN=%PROJECT_ROOT%\src\NT8.Adapters\bin\Release\net48"
set "WRAPPERS_SRC=%PROJECT_ROOT%\src\NT8.Adapters\Wrappers"
set "BACKUP_ROOT=%SCRIPT_DIR%backups"

echo ============================================================
echo NT8 SDK Deployment
echo Project Root: %PROJECT_ROOT%
echo NT8 Custom   : %NT8_CUSTOM%
echo ============================================================

if not exist "%NT8_CUSTOM%" (
    echo ERROR: NinjaTrader Custom folder not found.
    echo Expected path: %NT8_CUSTOM%
    echo Ensure NinjaTrader 8 is installed and started once.
    exit /b 1
)

if not exist "%CORE_BIN%\NT8.Core.dll" (
    echo ERROR: Core DLL not found: %CORE_BIN%\NT8.Core.dll
    echo Build release artifacts first:
    echo   dotnet build NT8-SDK.sln --configuration Release
    exit /b 1
)

if not exist "%ADAPTERS_BIN%\NT8.Adapters.dll" (
    echo ERROR: Adapters DLL not found: %ADAPTERS_BIN%\NT8.Adapters.dll
    echo Build release artifacts first:
    echo   dotnet build NT8-SDK.sln --configuration Release
    exit /b 1
)

if not exist "%NT8_STRATEGIES%" (
    mkdir "%NT8_STRATEGIES%"
)

for /f %%i in ('powershell -NoProfile -Command "Get-Date -Format yyyyMMdd_HHmmss"') do set "STAMP=%%i"
set "BACKUP_DIR=%BACKUP_ROOT%\%STAMP%"
set "MANIFEST_FILE=%BACKUP_ROOT%\%STAMP%\manifest.txt"
mkdir "%BACKUP_ROOT%\%STAMP%" >nul 2>&1

echo Backing up existing NT8 SDK files...
if exist "%NT8_CUSTOM%\NT8.Core.dll" copy /Y "%NT8_CUSTOM%\NT8.Core.dll" "%BACKUP_DIR%\NT8.Core.dll" >nul
if exist "%NT8_CUSTOM%\NT8.Adapters.dll" copy /Y "%NT8_CUSTOM%\NT8.Adapters.dll" "%BACKUP_DIR%\NT8.Adapters.dll" >nul
if exist "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" copy /Y "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" "%BACKUP_DIR%\BaseNT8StrategyWrapper.cs" >nul
if exist "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" copy /Y "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" "%BACKUP_DIR%\SimpleORBNT8Wrapper.cs" >nul

echo Deployment manifest > "%MANIFEST_FILE%"
echo Timestamp: %STAMP%>> "%MANIFEST_FILE%"
echo Source Core DLL: %CORE_BIN%\NT8.Core.dll>> "%MANIFEST_FILE%"
echo Source Adapters DLL: %ADAPTERS_BIN%\NT8.Adapters.dll>> "%MANIFEST_FILE%"
echo Destination Custom Folder: %NT8_CUSTOM%>> "%MANIFEST_FILE%"
echo Destination Strategies Folder: %NT8_STRATEGIES%>> "%MANIFEST_FILE%"

echo Deploying DLLs...
copy /Y "%CORE_BIN%\NT8.Core.dll" "%NT8_CUSTOM%\NT8.Core.dll" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy NT8.Core.dll
    exit /b 1
)

copy /Y "%ADAPTERS_BIN%\NT8.Adapters.dll" "%NT8_CUSTOM%\NT8.Adapters.dll" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy NT8.Adapters.dll
    exit /b 1
)

echo Deploying wrapper sources...
copy /Y "%WRAPPERS_SRC%\BaseNT8StrategyWrapper.cs" "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy BaseNT8StrategyWrapper.cs
    exit /b 1
)

copy /Y "%WRAPPERS_SRC%\SimpleORBNT8Wrapper.cs" "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy SimpleORBNT8Wrapper.cs
    exit /b 1
)

set "STRATEGIES_SRC=%PROJECT_ROOT%\src\NT8.Adapters\Strategies"
copy /Y "%STRATEGIES_SRC%\NT8StrategyBase.cs" "%NT8_STRATEGIES%\NT8StrategyBase.cs" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy NT8StrategyBase.cs
    exit /b 1
)

copy /Y "%STRATEGIES_SRC%\SimpleORBNT8.cs" "%NT8_STRATEGIES%\SimpleORBNT8.cs" >nul
if errorlevel 1 (
    echo ERROR: Failed to copy SimpleORBNT8.cs
    exit /b 1
)

echo Verifying deployment files...
if not exist "%NT8_CUSTOM%\NT8.Core.dll" (
    echo ERROR: Verification failed for NT8.Core.dll
    exit /b 1
)

if not exist "%NT8_CUSTOM%\NT8.Adapters.dll" (
    echo ERROR: Verification failed for NT8.Adapters.dll
    exit /b 1
)

if not exist "%NT8_STRATEGIES%\BaseNT8StrategyWrapper.cs" (
    echo ERROR: Verification failed for BaseNT8StrategyWrapper.cs
    exit /b 1
)

if not exist "%NT8_STRATEGIES%\SimpleORBNT8Wrapper.cs" (
    echo ERROR: Verification failed for SimpleORBNT8Wrapper.cs
    exit /b 1
)

echo.
echo Deployment complete.
echo Backup location: %BACKUP_DIR%
echo Manifest file : %MANIFEST_FILE%
echo.
echo Next steps:
echo  1. Open NinjaTrader 8.
echo  2. Open NinjaScript Editor and press F5 (Compile).
echo  3. Verify strategies appear in the Strategies list.

exit /b 0

