@echo off
setlocal

title Square Safety Word Button

echo ==========================================
echo        Square Safety Word Button
echo ==========================================
echo.

set "BASE_DIR=%~dp0"
set "ADDIN_DIR=%BASE_DIR%SquareSafety-WordAddin"
set "MANIFEST_PATH=%ADDIN_DIR%\manifest.xml"

if not exist "%MANIFEST_PATH%" (
  echo ERROR: Could not find add-in files.
  echo Please keep the SquareSafety-WordAddin folder next to this file.
  pause
  exit /b 1
)

echo Choose the Word document...

powershell -NoProfile -ExecutionPolicy Bypass -Command ^
 "Add-Type -AssemblyName System.Windows.Forms; $dlg = New-Object System.Windows.Forms.OpenFileDialog; $dlg.Filter = 'Word Documents (*.docx)|*.docx'; if ($dlg.ShowDialog() -eq 'OK') { Write-Output $dlg.FileName }" > "%TEMP%\ss_doc.txt"

set /p DOCX_PATH=<"%TEMP%\ss_doc.txt"

if "%DOCX_PATH%"=="" (
  echo No document selected.
  pause
  exit /b 1
)

echo Selected:
echo %DOCX_PATH%
echo.

cd /d "%ADDIN_DIR%"

echo Installing dev certificate...
call npx office-addin-dev-certs install

echo Starting local server...
start "" cmd /k "cd /d "%ADDIN_DIR%" && npx http-server -S -C "%USERPROFILE%\.office-addin-dev-certs\localhost.crt" -K "%USERPROFILE%\.office-addin-dev-certs\localhost.key" -p 3000"

timeout /t 5 >nul

echo Starting Word with add-in...

call npx office-addin-debugging start "%MANIFEST_PATH%" desktop --app word --document "%DOCX_PATH%"

echo.
echo If the panel does not appear:
echo In Word: Developer → Add-ins → Square Safety
echo.

pause
