@echo off
setlocal EnableDelayedExpansion

rem Change to project root
cd /d "%~dp0\..\.." 2>nul
if errorlevel 1 (
    echo ERROR: Cannot change to project root directory
    echo Script location: %~dp0
    pause
    exit /b 1
)

:GetPageName
rem Get page name and strip spaces/tabs
set /p PAGE_NAME="Enter Page Name: "
if "!PAGE_NAME!"=="" (
    echo Page name cannot be empty!
    echo.
    goto GetPageName
)
set "PAGE_NAME=!PAGE_NAME: =!"
set "PAGE_NAME=!PAGE_NAME:	=!"
if "!PAGE_NAME!"=="" (
    echo Page name cannot be empty!
    echo.
    goto GetPageName
)

rem Select creation mode
echo ========================================
echo   Create New Page
echo ========================================
echo   1. Create Page With Folder
echo   2. Create Page
echo ========================================
set /p MODE="Select mode (1 or 2) [Default: 1]: "
if "%MODE%"=="" set "MODE=1"
if not "%MODE%"=="1" if not "%MODE%"=="2" (
    echo Invalid selection!
    pause
    goto EndScript
)

rem Set file paths based on mode
if "%MODE%"=="1" (
    set "PAGE_FOLDER=src\pages\!PAGE_NAME!"
    set "JSX_FILE=src\pages\!PAGE_NAME!\!PAGE_NAME!.jsx"
    set "JS_FILE=src\pages\!PAGE_NAME!\!PAGE_NAME!.js"
    set "CSS_FILE=src\pages\!PAGE_NAME!\!PAGE_NAME!.css"
) else (
    set "PAGE_FOLDER="
    set "JSX_FILE=src\pages\!PAGE_NAME!.jsx"
    set "JS_FILE=src\pages\!PAGE_NAME!.js"
    set "CSS_FILE=src\pages\!PAGE_NAME!.css"
)

rem Check if the page already exists
if "%MODE%"=="1" (
    if exist "!PAGE_FOLDER!" (
        echo Page !PAGE_NAME! already exists!
        pause
        goto EndScript
    )
) else (
    if exist "!JSX_FILE!" (
        echo Page !PAGE_NAME! already exists!
        pause
        goto EndScript
    )
)

rem Create folder if needed
if "%MODE%"=="1" if not exist "!PAGE_FOLDER!" mkdir "!PAGE_FOLDER!"

rem Create JSX file
(
echo import './!PAGE_NAME!.css';
echo import { !PAGE_NAME!Logic } from './!PAGE_NAME!.js';
echo.
echo function !PAGE_NAME!^(^) {
echo   const logic = !PAGE_NAME!Logic^(^);
echo.
echo   return ^(
echo     ^<div className="!PAGE_NAME!-container"^>
echo       ^<h1^>!PAGE_NAME!^</h1^>
echo     ^</div^>
echo   ^);
echo }
echo.
echo export default !PAGE_NAME!;
) > "!JSX_FILE!"

rem Create JS file
(
echo export function !PAGE_NAME!Logic^(^) {
echo   // business logic placeholder
echo.
echo   return {
echo     // expose data or methods here
echo   };
echo }
) > "!JS_FILE!"

rem Create CSS file
(
echo .!PAGE_NAME!-container {
echo   padding: 20px;
echo }
) > "!CSS_FILE!"

echo Created: !JSX_FILE!
echo Created: !JS_FILE!
echo Created: !CSS_FILE!

rem Add the new page into App.jsx
set "APP_FILE=src\App.jsx"
if exist "!APP_FILE!" (
    call :AddToApp "%MODE%" "!PAGE_NAME!" "!APP_FILE!"
    echo Added !PAGE_NAME! to App.jsx
) else (
    echo Warning: App.jsx not found, skipping...
)

rem Show summary
echo.
echo ========================================
echo Page !PAGE_NAME! created successfully!
echo ========================================
echo.
echo Summary:
if "%MODE%"=="1" (
    echo   1. Created folder: !PAGE_FOLDER!
    echo   2. Created files:
    echo      - !JSX_FILE!
    echo      - !JS_FILE!
    echo      - !CSS_FILE!
    echo   3. Modified file: src\App.jsx
    echo      - Added import: import !PAGE_NAME! from './pages/!PAGE_NAME!/!PAGE_NAME!.jsx';
    echo      - Added component: ^<!PAGE_NAME! /^>
) else (
    echo   1. Created files in src\pages directory:
    echo      - !JSX_FILE!
    echo      - !JS_FILE!
    echo      - !CSS_FILE!
    echo   2. Modified file: src\App.jsx
    echo      - Added import: import !PAGE_NAME! from './pages/!PAGE_NAME!.jsx';
    echo      - Added component: ^<!PAGE_NAME! /^>
)

pause
goto EndScript

rem Add new page into App.jsx (subroutine)
:AddToApp
setlocal EnableDelayedExpansion
set "MODE_VAL=%~1"
set "PAGE_VAL=%~2"
set "APP_PATH=%~3"

if not exist "%APP_PATH%" (
    echo ERROR: File not found: %APP_PATH%
    exit /b 1
)

rem Generate import line based on mode
if "%MODE_VAL%"=="1" (
    set "IMPORT_LINE=import %PAGE_VAL% from './pages/%PAGE_VAL%/%PAGE_VAL%.jsx';"
) else (
    set "IMPORT_LINE=import %PAGE_VAL% from './pages/%PAGE_VAL%.jsx';"
)

rem Find last import line number
set "LAST_IMPORT_LINE="
for /f "tokens=1* delims=:" %%n in ('findstr /n /b /c:"import " "%APP_PATH%"') do set "LAST_IMPORT_LINE=%%n"

rem Find end of return statement (best effort)
set "RETURN_LINE="
for /f "tokens=1 delims=:" %%n in ('findstr /n /c:"  );" "%APP_PATH%"') do set "RETURN_LINE=%%n"
if not defined RETURN_LINE (
    for /f "tokens=1 delims=:" %%n in ('findstr /n /c:");" "%APP_PATH%"') do set "RETURN_LINE=%%n"
)

rem Find last </div> before return ends
set "LAST_DIV_LINE="
for /f "tokens=1* delims=:" %%n in ('findstr /n /c:"</div>" "%APP_PATH%"') do (
    if defined RETURN_LINE (
        if %%n lss !RETURN_LINE! set "LAST_DIV_LINE=%%n"
    ) else (
        set "LAST_DIV_LINE=%%n"
    )
)

rem Check if import/component already exist
set "NEED_IMPORT=1"
set "NEED_COMPONENT=1"
findstr /c:"import %PAGE_VAL% from" "%APP_PATH%" >nul 2>&1
if not errorlevel 1 set "NEED_IMPORT=0"
findstr /c:"<%PAGE_VAL% />" "%APP_PATH%" >nul 2>&1
if not errorlevel 1 set "NEED_COMPONENT=0"

rem Rewrite App.jsx with additions
set "TEMP_FILE=%TEMP%\app_temp_%RANDOM%.jsx"
if exist "%TEMP_FILE%" del "%TEMP_FILE%"

set /a "LINE_NO=0"
for /f "usebackq delims=" %%a in ("%APP_PATH%") do (
    set "line=%%a"
    set /a "LINE_NO+=1"
    set "isImport=0"
    if /i "!line:~0,7!"=="import " set "isImport=1"

    rem Insert component before closing div inside return
    if "!LINE_NO!"=="!LAST_DIV_LINE!" if !NEED_COMPONENT! equ 1 (
        set "indent="
        set "line_copy=!line!"
        for /f "tokens=1 delims=<" %%b in ("!line_copy!") do set "indent=%%b"
        if "!indent!"=="" set "indent=    "
        if "!indent!"=="!line_copy!" set "indent=    "
        rem add 2 spaces for component indent under div
        set "indent=!indent!  "
        >>"%TEMP_FILE%" echo !indent!^<%PAGE_VAL% /^>
        set "NEED_COMPONENT=0"
    )

    >>"%TEMP_FILE%" echo(!line!

    rem Insert new import after the last import line
    if !isImport! equ 1 if !NEED_IMPORT! equ 1 if "!LINE_NO!"=="!LAST_IMPORT_LINE!" (
        >>"%TEMP_FILE%" echo !IMPORT_LINE!
        set "NEED_IMPORT=0"
    )
)

rem Append missing import/component at end if needed
if !NEED_IMPORT! equ 1 >>"%TEMP_FILE%" echo !IMPORT_LINE!
if !NEED_COMPONENT! equ 1 >>"%TEMP_FILE%" echo       ^<%PAGE_VAL% /^>

move /y "%TEMP_FILE%" "%APP_PATH%" >nul
endlocal
exit /b

:EndScript
endlocal
echo Press any key to exit...
pause >nul
