<?php
/**
 * Vérification de la configuration Flutterwave
 * Lance: php tools/verify_flutterwave_config.php
 */

require __DIR__ . '/../vendor/autoload.php';
$dotenv = \Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

echo "╔════════════════════════════════════════════════════════════════╗\n";
echo "║         VÉRIFICATION CONFIGURATION FLUTTERWAVE                 ║\n";
echo "╚════════════════════════════════════════════════════════════════╝\n\n";

echo "1️⃣  VARIABLES D'ENVIRONNEMENT (.env)\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";

$publicKeyEnv = getenv('FLUTTERWAVE_PUBLIC_KEY') ?: getenv('FLW_PUBLIC_KEY');
$secretKeyEnv = getenv('FLUTTERWAVE_SECRET_KEY') ?: getenv('FLW_SECRET_KEY');
$encryptKeyEnv = getenv('FLUTTERWAVE_ENCRYPTION_KEY') ?: getenv('FLW_ENCRYPTION_KEY');

echo "✅ FLUTTERWAVE_PUBLIC_KEY  : " . (strlen($publicKeyEnv) > 0 ? substr($publicKeyEnv, 0, 30) . "..." : "❌ NON DÉFINIE") . "\n";
echo "✅ FLUTTERWAVE_SECRET_KEY  : " . (strlen($secretKeyEnv) > 0 ? substr($secretKeyEnv, 0, 30) . "..." : "❌ NON DÉFINIE") . "\n";
echo "✅ FLUTTERWAVE_ENCRYPTION_KEY : " . (strlen($encryptKeyEnv) > 0 ? substr($encryptKeyEnv, 0, 30) . "..." : "❌ NON DÉFINIE") . "\n";

echo "\n2️⃣  FICHIER config/payment.php\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";

$configFile = __DIR__ . '/../config/payment.php';
if (file_exists($configFile)) {
    $config = require $configFile;
    if (isset($config['flutterwave'])) {
        echo "✅ Configuration 'flutterwave' trouvée\n";
        
        $pk = $config['flutterwave']['public_key'] ?? null;
        $sk = $config['flutterwave']['secret_key'] ?? null;
        $ek = $config['flutterwave']['encryption_key'] ?? null;
        
        echo "   - public_key:     " . (strlen($pk) > 0 ? substr($pk, 0, 30) . "..." : "❌ VIDE") . "\n";
        echo "   - secret_key:     " . (strlen($sk) > 0 ? substr($sk, 0, 30) . "..." : "❌ VIDE") . "\n";
        echo "   - encryption_key: " . (strlen($ek) > 0 ? substr($ek, 0, 30) . "..." : "❌ VIDE") . "\n";
    } else {
        echo "❌ Configuration 'flutterwave' manquante\n";
    }
} else {
    echo "❌ Fichier config/payment.php introuvable\n";
}

echo "\n3️⃣  VALIDATION KEYS\n";
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n";

$allValid = true;
if (strlen($publicKeyEnv) === 0) {
    echo "❌ FLUTTERWAVE_PUBLIC_KEY est vide ou non définie\n";
    $allValid = false;
} else {
    echo "✅ FLUTTERWAVE_PUBLIC_KEY ok\n";
}

if (strlen($secretKeyEnv) === 0) {
    echo "❌ FLUTTERWAVE_SECRET_KEY est vide ou non définie\n";
    $allValid = false;
} else {
    echo "✅ FLUTTERWAVE_SECRET_KEY ok\n";
}

if (strlen($encryptKeyEnv) === 0) {
    echo "⚠️  FLUTTERWAVE_ENCRYPTION_KEY est vide (optionnel pour hosted payment)\n";
} else {
    echo "✅ FLUTTERWAVE_ENCRYPTION_KEY ok\n";
}

echo "\n";
if ($allValid) {
    echo "╔════════════════════════════════════════════════════════════════╗\n";
    echo "║  ✅ CONFIGURATION VALIDE - PAIEMENT PRÊT À DÉMARRER             ║\n";
    echo "╚════════════════════════════════════════════════════════════════╝\n";
} else {
    echo "╔════════════════════════════════════════════════════════════════╗\n";
    echo "║  ❌ CONFIGURATION INCOMPLÈTE - VEUILLEZ VÉRIFIER .env           ║\n";
    echo "╚════════════════════════════════════════════════════════════════╝\n";
}
