# Rename 20 test user S22+2 sang naming role-based de UAT de phan biet
#
# Email format: {dept-lower}.{level}@solutions.com.vn
#   - act.nv / act.pp / act.tp
#   - bod.1 / bod.2 (no hierarchy)
#   - equ.nv / equ.pp / equ.tp
#   - fin.nv / fin.pp / fin.tp
#   - hra.nv / hra.pp / hra.tp
#   - pm.nv / pm.pp / pm.tp
#   - qs.nv / qs.pp / qs.tp
#
# FullName format: "{DEPT} {LEVEL} - {Roles} [Flags]"
#   - Flag [Bypass] = CanBypassReview=true (2-stage skip)
#   - Flag [SkipFinal] = AllowDrafterSkipToFinal=true (F2 Mig 29)
#
# Identity rename pattern per gotcha #38 — must update 4 fields atomically:
#   Email + NormalizedEmail + UserName + NormalizedUserName + FullName.
#
# Run qua SSH vietreport-vps + sqlcmd 1 transaction.
#
# Usage: .\scripts\rename-test-users-to-roles.ps1

$ErrorActionPreference = 'Stop'

# Mapping: old email -> new email + new FullName
$renames = @(
    @{ Old='hoa.nguyen@solutions.com.vn';  New='act.nv@solutions.com.vn'; FullName='ACT NV - Drafter+Accounting' },
    @{ Old='lan.pham@solutions.com.vn';    New='act.pp@solutions.com.vn'; FullName='ACT PP - Drafter+Accounting' },
    @{ Old='minh.le@solutions.com.vn';     New='act.tp@solutions.com.vn'; FullName='ACT TP - DeptMgr+Accounting [Bypass]' },
    @{ Old='tuan.tran@solutions.com.vn';   New='bod.1@solutions.com.vn';  FullName='BOD 1 - Director' },
    @{ Old='hung.do@solutions.com.vn';     New='bod.2@solutions.com.vn';  FullName='BOD 2 - Director+AuthSigner' },
    @{ Old='dung.bui@solutions.com.vn';    New='equ.nv@solutions.com.vn'; FullName='EQU NV - Drafter+Equipment' },
    @{ Old='hai.vu@solutions.com.vn';      New='equ.pp@solutions.com.vn'; FullName='EQU PP - Drafter+Equipment' },
    @{ Old='tho.do@solutions.com.vn';      New='equ.tp@solutions.com.vn'; FullName='EQU TP - DeptMgr+Equipment' },
    @{ Old='linh.dao@solutions.com.vn';    New='fin.nv@solutions.com.vn'; FullName='FIN NV - Drafter+Finance' },
    @{ Old='nga.bui@solutions.com.vn';     New='fin.pp@solutions.com.vn'; FullName='FIN PP - Drafter+Finance [SkipFinal]' },
    @{ Old='thu.pham@solutions.com.vn';    New='fin.tp@solutions.com.vn'; FullName='FIN TP - DeptMgr+Finance' },
    @{ Old='mai.tran@solutions.com.vn';    New='hra.nv@solutions.com.vn'; FullName='HRA NV - Drafter+HrAdmin' },
    @{ Old='hong.le@solutions.com.vn';     New='hra.pp@solutions.com.vn'; FullName='HRA PP - Drafter+HrAdmin' },
    @{ Old='tam.nguyen@solutions.com.vn';  New='hra.tp@solutions.com.vn'; FullName='HRA TP - DeptMgr+HrAdmin [Bypass]' },
    @{ Old='khoi.do@solutions.com.vn';     New='pm.nv@solutions.com.vn';  FullName='PM NV - Drafter+PM [SkipFinal]' },
    @{ Old='phong.vu@solutions.com.vn';    New='pm.pp@solutions.com.vn';  FullName='PM PP - Drafter+PM' },
    @{ Old='quan.bui@solutions.com.vn';    New='pm.tp@solutions.com.vn';  FullName='PM TP - DeptMgr+PM' },
    @{ Old='hieu.nguyen@solutions.com.vn'; New='qs.nv@solutions.com.vn';  FullName='QS NV - Drafter' },
    @{ Old='thanh.pham@solutions.com.vn';  New='qs.pp@solutions.com.vn';  FullName='QS PP - Drafter' },
    @{ Old='duc.le@solutions.com.vn';      New='qs.tp@solutions.com.vn';  FullName='QS TP - DeptMgr' }
)

# Build SQL transaction (SET QUOTED_IDENTIFIER ON required for filtered indexes on Users)
$sqlLines = @('SET QUOTED_IDENTIFIER ON;', 'BEGIN TRAN;')
foreach ($r in $renames) {
    $newNormalized = $r.New.ToUpper()
    $sqlLines += "UPDATE Users SET Email='$($r.New)', NormalizedEmail='$newNormalized', UserName='$($r.New)', NormalizedUserName='$newNormalized', FullName='$($r.FullName)' WHERE Email='$($r.Old)';"
}
$sqlLines += 'COMMIT;'
$sqlLines += "SELECT Email, FullName FROM Users WHERE Email LIKE '%.nv@%' OR Email LIKE '%.pp@%' OR Email LIKE '%.tp@%' OR Email LIKE 'bod.%' ORDER BY Email;"
$sqlScript = $sqlLines -join "`n"

Write-Host "==> Running rename transaction via SSH vietreport-vps..." -ForegroundColor Cyan
Write-Host "    Total renames: $($renames.Count)" -ForegroundColor Gray

# Escape sqlcmd args via tempfile to avoid shell quoting issues
$tmpPath = [System.IO.Path]::GetTempFileName() + '.sql'
$sqlScript | Out-File -FilePath $tmpPath -Encoding ASCII

# SCP file to VPS then execute
$remotePath = 'C:\Windows\Temp\rename-users.sql'
& scp $tmpPath "vietreport-vps:$remotePath"
& ssh vietreport-vps "sqlcmd -S .\SQLEXPRESS -d SolutionErp -E -i $remotePath"

Remove-Item $tmpPath -Force
Write-Host "==> Rename complete. Login test:" -ForegroundColor Green
Write-Host "    act.nv@solutions.com.vn / TestUser@2026" -ForegroundColor Magenta
Write-Host "    fin.pp@solutions.com.vn / TestUser@2026 (SkipFinal)" -ForegroundColor Magenta
Write-Host "    pm.nv@solutions.com.vn  / TestUser@2026 (SkipFinal)" -ForegroundColor Magenta
Write-Host "    act.tp@solutions.com.vn / TestUser@2026 (Bypass)" -ForegroundColor Magenta
Write-Host "    hra.tp@solutions.com.vn / TestUser@2026 (Bypass)" -ForegroundColor Magenta
