@echo off
setlocal EnableExtensions

:: Search for .config directory in current working directory (including subdirectories) to find ProjectStructure location
set "STRUCT_DIR="
for /f "usebackq delims=" %%I in (`powershell -NoProfile -ExecutionPolicy Bypass -Command ^
    "$ErrorActionPreference = 'Continue'; $searchDir = '%CD%'; try { $subDirs = Get-ChildItem -LiteralPath $searchDir -Directory -Force -ErrorAction Stop; $configDirs = @(); foreach ($subDir in $subDirs) { $configPath = Join-Path $subDir.FullName '.config\config.json'; if (Test-Path -LiteralPath $configPath -PathType Leaf) { $configDirs += $configPath } }; if ($configDirs.Count -gt 0) { $configPath = $configDirs[0]; $config = Get-Content -LiteralPath $configPath -Raw | ConvertFrom-Json; if ($config.structDir) { $structDir = $config.structDir.ToString().Trim(); if ([System.IO.Path]::IsPathRooted($structDir)) { try { $resolved = Resolve-Path -LiteralPath $structDir -ErrorAction Stop; Write-Output $resolved.Path } catch { Write-Output $structDir } } else { $candidate = Join-Path (Split-Path -Parent $configPath) $structDir; try { $resolvedCandidate = Resolve-Path -LiteralPath $candidate -ErrorAction Stop; Write-Output $resolvedCandidate.Path } catch { Write-Output $candidate } } } } } catch { }"`) do set "STRUCT_DIR=%%I"

if not defined STRUCT_DIR (
    echo [ERROR] Unable to find .config directory or resolve ProjectStructure directory.
    echo Please ensure you are running NewProject.bat from a directory containing projects with .config folders.
    exit /b 1
)

:: Debug: Show discovered structDir
echo Found ProjectStructure directory: %STRUCT_DIR%

:: Locate CfgDeploy.ps1 in the ProjectStructure directory
set "CFG_DEPLOY=%STRUCT_DIR%\CfgDeploy.ps1"

if not exist "%CFG_DEPLOY%" (
    echo [ERROR] CfgDeploy.ps1 not found at: %CFG_DEPLOY%
    exit /b 1
)

:: Get current date in YYYY-MM-DD format
for /f "tokens=2-4 delims=/ " %%a in ('date /t') do (
    set mm=%%a
    set dd=%%b
    set yy=%%c
)
set yyyy=20%yy:~-2%
set yymmdd=%yy:~-2%%mm%%dd%

:: Ask for project name
set /p projectName="Enter project name (press Enter for default 'NewProject'): "
if "%projectName%"=="" set projectName=NewProject
set projectRoot=%yymmdd%_%projectName%

:: Create main project directory
mkdir "%projectRoot%"

:: Create Assets structure
mkdir "%projectRoot%\Assets\ElevenLabs"
if exist "%STRUCT_DIR%\NewDaily.bat" copy /Y "%STRUCT_DIR%\NewDaily.bat" "%projectRoot%\Assets\ElevenLabs\NewDaily.bat" >nul
mkdir "%projectRoot%\Assets\Blends"
mkdir "%projectRoot%\Assets\Mocap"
mkdir "%projectRoot%\Assets\VO"
if exist "%~dp0NewDaily.bat" copy /Y "%~dp0NewDaily.bat" "%projectRoot%\Assets\VO\NewDaily.bat" >nul

:: Create Blends structure
mkdir "%projectRoot%\Blends\animations\"
mkdir "%projectRoot%\Blends\stills\img-BG"

:: Create Deliverable structure
mkdir "%projectRoot%\Deliverable\"

:: Create Pr structure
mkdir "%projectRoot%\Pr\RnR\RIFE"

:: Add project root additions
if not exist "%projectRoot%\Renders" mkdir "%projectRoot%\Renders"

:: Place helper scripts into Renders (config lives in .config\, deployed by CfgDeploy below)
set "templateRoot=%STRUCT_DIR%"
for %%F in (UpdateSequences.bat ZipSeqArchv.bat UnzipSeqArchv.bat CompressPNGs.bat) do (
  if exist "%templateRoot%\%%F" copy /Y "%templateRoot%\%%F" "%projectRoot%\Renders\%%F" >nul
)

:: Use repo-provided templates for git config files
if exist "%~dp0components\gitignore" copy /Y "%~dp0components\gitignore" "%projectRoot%\.gitignore" >nul
if exist "%~dp0components\gitattributes" copy /Y "%~dp0components\gitattributes" "%projectRoot%\.gitattributes" >nul

:: Initialize git and install Git LFS
pushd "%projectRoot%" >nul
git init
git lfs install
git add . -v
git commit -m "init"
popd >nul

:: Deploy config workflow using CfgDeploy.ps1
echo.
echo Deploying config workflow...
for %%P in ("%projectRoot%") do set "PROJECT_ROOT_ABS=%%~fP"
powershell -NoProfile -ExecutionPolicy Bypass -Command "& '%CFG_DEPLOY%' -ProjectPath '%PROJECT_ROOT_ABS%'"
if errorlevel 1 (
    echo [WARNING] Config workflow deployment failed. You may need to run CfgDeploy.ps1 manually.
) else (
    echo Config workflow deployed successfully.
)

echo Project structure created successfully in folder: %projectRoot%
pause 
