# Import-Workflows.ps1
# Script pour importer tous les workflows N8N automatiquement

param(
    [string]$n8nUrl = "http://localhost:5678",
    [string]$apiKey = ""
)

Write-Host "🚀 Importation des Workflows N8N pour Keep Notes MCP" -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""

# Vérifier si N8N est accessible
try {
    $response = Invoke-WebRequest -Uri "$n8nUrl/rest/workflows" -UseBasicParsing
    Write-Host "✅ N8N est accessible à $n8nUrl" -ForegroundColor Green
} catch {
    Write-Host "❌ Erreur: Impossible de connecter à N8N à $n8nUrl" -ForegroundColor Red
    Write-Host "   Vérifiez que N8N est en cours d'exécution" -ForegroundColor Yellow
    exit 1
}

Write-Host ""

# Liste des workflows à importer
$workflows = @(
    @{
        file = "n8n-workflow-create-note.json"
        name = "Create Note with Classification"
    },
    @{
        file = "n8n-workflow-search-summary.json"
        name = "Search & Summary"
    },
    @{
        file = "n8n-workflow-notebook-management.json"
        name = "Notebook Manager"
    },
    @{
        file = "n8n-workflow-reminder-notifications.json"
        name = "Reminder Notifications"
    },
    @{
        file = "n8n-workflow-label-management.json"
        name = "Label Manager"
    },
    @{
        file = "n8n-workflow-email-integration.json"
        name = "Email to Note"
    }
)

$imported = 0
$failed = 0

foreach ($workflow in $workflows) {
    $filePath = $workflow.file
    $name = $workflow.name

    Write-Host "📥 Importation: $name" -ForegroundColor Yellow

    if (-not (Test-Path $filePath)) {
        Write-Host "   ❌ Fichier non trouvé: $filePath" -ForegroundColor Red
        $failed++
        continue
    }

    try {
        # Lire le fichier JSON
        $workflowJson = Get-Content $filePath -Raw

        # Préparer les en-têtes
        $headers = @{
            "Content-Type" = "application/json"
        }
        if ($apiKey) {
            $headers["Authorization"] = "Bearer $apiKey"
        }

        # Envoyer à N8N
        $response = Invoke-RestMethod `
            -Uri "$n8nUrl/rest/workflows/import" `
            -Method POST `
            -Body $workflowJson `
            -Headers $headers `
            -ErrorAction Stop

        Write-Host "   ✅ Importé avec succès (ID: $($response.id))" -ForegroundColor Green
        $imported++
    } catch {
        Write-Host "   ❌ Erreur lors de l'import: $_" -ForegroundColor Red
        $failed++
    }

    Write-Host ""
}

# Résumé
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host "📊 Résumé de l'importation" -ForegroundColor Cyan
Write-Host "=================================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "✅ Importés avec succès: $imported" -ForegroundColor Green
Write-Host "❌ Échecs: $failed" -ForegroundColor Red
Write-Host "📝 Total: $($workflows.Count)" -ForegroundColor Yellow
Write-Host ""

if ($imported -gt 0) {
    Write-Host "🎉 Importation terminée!" -ForegroundColor Green
    Write-Host ""
    Write-Host "📌 Prochaines étapes:" -ForegroundColor Yellow
    Write-Host "   1. Ouvrez N8N dans votre navigateur" -ForegroundColor White
    Write-Host "   2. Configurez les variables d'environnement (Settings → Variables)" -ForegroundColor White
    Write-Host "   3. Configurez les connexions (Slack, Email, OpenAI)" -ForegroundColor White
    Write-Host "   4. Activez les workflows (bouton play)" -ForegroundColor White
    Write-Host ""
    Write-Host "📚 Documentation: N8N-SETUP.md" -ForegroundColor Cyan
} else {
    Write-Host "⚠️ Aucun workflow n'a pu être importé" -ForegroundColor Yellow
    Write-Host "Consultez la documentation N8N-SETUP.md pour plus d'informations" -ForegroundColor Cyan
}

Write-Host ""
