@echo off
REM =============================================================================
REM Local staging rehearsal helper (Windows)
REM Requires: MySQL client on PATH, production backup SQL file
REM Usage:
REM   scripts\run-local-staging-rehearsal.bat USER PASSWORD BACKUP_SQL_PATH
REM Example:
REM   scripts\run-local-staging-rehearsal.bat root mypass E:\wincasoft\backup-sql\rinocfzp_team_management-15-6-26-1.sql
REM =============================================================================

setlocal enabledelayedexpansion

if "%~3"=="" (
  echo Usage: %~nx0 USER PASSWORD BACKUP_SQL_PATH
  exit /b 1
)

set MYSQL_USER=%~1
set MYSQL_PASS=%~2
set BACKUP_SQL=%~3
set DB_NAME=team_management_staging

echo Creating database %DB_NAME%...
mysql -u %MYSQL_USER% -p%MYSQL_PASS% -e "DROP DATABASE IF EXISTS `%DB_NAME%`; CREATE DATABASE `%DB_NAME%` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;"
if errorlevel 1 exit /b 1

echo Importing backup...
mysql -u %MYSQL_USER% -p%MYSQL_PASS% %DB_NAME% < "%BACKUP_SQL%"
if errorlevel 1 exit /b 1

for %%F in (00-preflight-checks 01-baseline-migrations 02-access-control-schema 03-member-access-role) do (
  echo Running %%F.sql...
  mysql -u %MYSQL_USER% -p%MYSQL_PASS% %DB_NAME% < "%~dp0sql\%%F.sql"
  if errorlevel 1 exit /b 1
)

echo.
echo Schema steps 00-03 complete.
echo Next:
echo   1. set DATABASE_URL=mysql://%MYSQL_USER%:%MYSQL_PASS%@localhost:3306/%DB_NAME%
echo   2. npm run seed:access-roles
echo   3. mysql ... ^< scripts/sql/04-backfill-member-roles.sql
echo   4. mysql ... ^< scripts/sql/05-permission-scope-updates.sql
echo   5. mysql ... ^< scripts/sql/06-post-migration-verify.sql
echo.
echo See scripts/PRODUCTION_UPGRADE_RUNBOOK.md for full details.

endlocal
