param(
  [switch]$SkipStop
)

$Root = Split-Path $PSScriptRoot -Parent
Set-Location $Root

Write-Host '================================'
Write-Host ' EENE Dashboard - Data Rebuild'
Write-Host ' Source: data\seed\hr-data.json'
Write-Host '================================'
Write-Host ''
Write-Host ' [WARN] 모든 업무·팀원·첨부 DB 레코드를 지우고'
Write-Host '        hr-data.json 기준으로 다시 만듭니다.'
Write-Host '        (평소 UI 수정은 자동 저장 — 이 파일은 초기화용)'
Write-Host '        uploads\ 파일은 유지됩니다.'
Write-Host ''

$confirm = Read-Host '계속하려면 Y 입력'
if ($confirm -notmatch '^[Yy]') {
  Write-Host '취소됨.'
  exit 0
}

if (-not $SkipStop) {
  Write-Host '[1/3] Stopping API/WEB ...'
  & "$PSScriptRoot\stop-server.ps1" | Out-Null
  Write-Host '        Done.'
} else {
  Write-Host '[1/3] Skip stop (-SkipStop)'
}

Write-Host ''
Write-Host '[2/3] DB sync + rebuild seed ...'
Set-Location (Join-Path $Root 'backend')
cmd /c 'npm run db:sync'
if ($LASTEXITCODE -ne 0) { exit 1 }
cmd /c 'npm run db:seed'
if ($LASTEXITCODE -ne 0) { exit 1 }
Set-Location $Root
Write-Host '        Done.'

Write-Host ''
Write-Host '[3/3] Next steps'
Write-Host '  1) 서버시작.bat 실행'
Write-Host '  2) 브라우저 Ctrl+F5'
Write-Host '  3) (선택) DevTools > Application > Local Storage 에서'
Write-Host '     eene-quarter-hub-config-v1, eene-board-slot-headers-v1 삭제'
Write-Host ''
Write-Host ' Done.'
