# Script para configurar Push Notifications
# Execute este script após iniciar o Convex (npx convex dev)

Write-Host "🔔 Configurando Push Notifications..." -ForegroundColor Cyan
Write-Host ""

# Verificar se estamos no diretório correto
if (-not (Test-Path "packages/backend")) {
    Write-Host "❌ Erro: Execute este script da raiz do projeto" -ForegroundColor Red
    exit 1
}

Write-Host "📝 Configurando variáveis de ambiente no Convex..." -ForegroundColor Yellow
Write-Host ""

cd packages/backend

# VAPID Keys geradas
$VAPID_PUBLIC_KEY = "BDerX0lK_hBCLpC7EbuxoJb2EJ7bcCLaHWxkNumVbvrx9w0MmJduHxJOP3WBwBP-SpQGcueMOyHCv7LFK3KnQks"
$VAPID_PRIVATE_KEY = "KWkJLMxCuCPQQiRXIJEt06G4pTdW0FaUN_vMyY02sc4"
$FRONTEND_URL = "http://localhost:5173"

Write-Host "Configurando VAPID_PUBLIC_KEY..." -ForegroundColor Gray
npx convex env set VAPID_PUBLIC_KEY $VAPID_PUBLIC_KEY

Write-Host "Configurando VAPID_PRIVATE_KEY..." -ForegroundColor Gray
npx convex env set VAPID_PRIVATE_KEY $VAPID_PRIVATE_KEY

Write-Host "Configurando FRONTEND_URL..." -ForegroundColor Gray
npx convex env set FRONTEND_URL $FRONTEND_URL

Write-Host ""
Write-Host "✅ Variáveis de ambiente configuradas no Convex!" -ForegroundColor Green
Write-Host ""

cd ../..

# Configurar arquivo .env do frontend
Write-Host "📝 Criando arquivo .env no frontend..." -ForegroundColor Yellow

$envContent = @"
# VAPID Public Key para Push Notifications
VITE_VAPID_PUBLIC_KEY=$VAPID_PUBLIC_KEY
"@

$envPath = "apps/web/.env"

if (Test-Path $envPath) {
    Write-Host "⚠️  Arquivo .env já existe. Adicionando VAPID_PUBLIC_KEY..." -ForegroundColor Yellow
    # Verificar se já existe a variável
    $currentContent = Get-Content $envPath -Raw
    if ($currentContent -notmatch "VITE_VAPID_PUBLIC_KEY") {
        Add-Content $envPath "`n$envContent"
        Write-Host "✅ VAPID_PUBLIC_KEY adicionada ao .env" -ForegroundColor Green
    } else {
        Write-Host "ℹ️  VITE_VAPID_PUBLIC_KEY já existe no .env" -ForegroundColor Cyan
    }
} else {
    Set-Content $envPath $envContent
    Write-Host "✅ Arquivo .env criado em apps/web/.env" -ForegroundColor Green
}

Write-Host ""
Write-Host "✨ Configuração concluída!" -ForegroundColor Green
Write-Host ""
Write-Host "📋 Próximos passos:" -ForegroundColor Cyan
Write-Host "1. Reinicie o servidor Convex (se estiver rodando)" -ForegroundColor White
Write-Host "2. Reinicie o servidor frontend (se estiver rodando)" -ForegroundColor White
Write-Host "3. Teste as push notifications conforme o guia de testes" -ForegroundColor White
Write-Host ""

