# Copies service account + bootstrap from api/ to deploy/firebase-server-files/ for upload.
$ErrorActionPreference = 'Stop'
$root = Split-Path $PSScriptRoot -Parent

$pairs = @(
    @{
        SrcJson = Join-Path $root 'api\firebase-service-account.local.json'
        SrcBoot = Join-Path $root 'api\firebase-admin-bootstrap.local.php'
        DstDir = Join-Path $root 'deploy\firebase-server-files\api'
    },
    @{
        SrcJson = Join-Path $root 'super-admin-panel\api\firebase-service-account.local.json'
        SrcBoot = Join-Path $root 'super-admin-panel\api\firebase-admin-bootstrap.local.php'
        DstDir = Join-Path $root 'deploy\firebase-server-files\super-admin-panel\api'
    }
)

foreach ($p in $pairs) {
    if (-not (Test-Path $p.SrcJson)) { throw "Missing: $($p.SrcJson)" }
    if (-not (Test-Path $p.SrcBoot)) { throw "Missing: $($p.SrcBoot)" }
    New-Item -ItemType Directory -Force -Path $p.DstDir | Out-Null
    Copy-Item -Force $p.SrcJson (Join-Path $p.DstDir 'firebase-service-account.local.json')
    Copy-Item -Force $p.SrcBoot (Join-Path $p.DstDir 'firebase-admin-bootstrap.local.php')
    Write-Host "Synced -> $($p.DstDir)"
}

$smokeSrc = Join-Path $root 'api\firebase-smoke-test.php'
if (Test-Path $smokeSrc) {
    Copy-Item -Force $smokeSrc (Join-Path $root 'deploy\firebase-server-files\api\firebase-smoke-test.php')
    Write-Host "Synced -> deploy\firebase-server-files\api\firebase-smoke-test.php"
}

Write-Host "Deploy bundle updated. Upload deploy/firebase-server-files/ to production (see README-DEPLOY.txt)."
