# Encode Certificate for CI/CD
# Converts the PFX certificate to base64 for use as GitHub/Gitea action secret

param(
    [string]$CertPath = "certificates\rmtPocketWatcher.pfx",
    [string]$OutputFile = "certificate_base64.txt"
)

$ErrorActionPreference = "Stop"

Write-Host "Encoding Certificate for CI/CD" -ForegroundColor Green
Write-Host "==============================" -ForegroundColor Green

# Check if certificate exists
if (-not (Test-Path $CertPath)) {
    Write-Error "Certificate not found at: $CertPath"
    Write-Host "Create a certificate first using .\create_certificate.ps1" -ForegroundColor Yellow
    exit 1
}

# Read certificate and encode as base64
Write-Host "Reading certificate: $CertPath" -ForegroundColor Yellow
$certBytes = [System.IO.File]::ReadAllBytes((Resolve-Path $CertPath))
$certBase64 = [System.Convert]::ToBase64String($certBytes)

# Save to file
$certBase64 | Out-File -FilePath $OutputFile -Encoding ASCII -NoNewline

Write-Host "✅ Certificate encoded successfully!" -ForegroundColor Green
Write-Host "Base64 encoded certificate saved to: $OutputFile" -ForegroundColor Cyan
Write-Host ""
Write-Host "Next steps:" -ForegroundColor Yellow
Write-Host "1. Copy the contents of $OutputFile" -ForegroundColor White
Write-Host "2. Add as action secret named 'CERT_BASE64'" -ForegroundColor White
Write-Host "3. Add certificate password as secret named 'CERT_PASSWORD'" -ForegroundColor White
Write-Host ""
Write-Host "⚠️  IMPORTANT: Delete $OutputFile after copying to keep certificate secure!" -ForegroundColor Red