<?php
session_start();
if (!isset($_SESSION['admin'])) { header("Location: admin.php"); exit; }

// --- POSTGRES CONNECTION ---
$host = "localhost";
$dbname = "smarteye";
$user = "lucas";
$pass = "smarteye_db_pass";

try {
    $pdo = new PDO("pgsql:host=$host;dbname=$dbname", $user, $pass);
} catch (PDOException $e) {
    die("Erreur DB: " . $e->getMessage());
}

// Stats
$stats_sql = "SELECT client_name, COUNT(*) as total FROM sms_logs GROUP BY client_name ORDER BY total DESC";
$stats = $pdo->query($stats_sql)->fetchAll(PDO::FETCH_ASSOC);

// Logs
$logs_sql = "SELECT * FROM sms_logs ORDER BY sent_at DESC LIMIT 100";
$logs = $pdo->query($logs_sql)->fetchAll(PDO::FETCH_ASSOC);
?>
<!DOCTYPE html>
<html lang="fr">
<head>
    <meta charset="UTF-8">
    <title>SMS Manager - Lucas</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>body { background-color: #0f172a; color: #e2e8f0; }</style>
</head>
<body class="min-h-screen flex flex-col p-8 max-w-7xl mx-auto w-full">

    <!-- Header -->
    <div class="flex justify-between items-center mb-8">
        <div>
            <h1 class="text-2xl font-bold text-white tracking-wider flex items-center gap-2">
                <a href="admin.php" class="text-slate-500 hover:text-white transition">←</a> 
                SMS MANAGER
            </h1>
            <p class="text-slate-500 text-sm">Base de données PostgreSQL</p>
        </div>
        <div class="bg-slate-800 px-4 py-2 rounded border border-slate-700">
            <span class="text-emerald-400 font-bold"><?php echo count($logs); ?></span> <span class="text-xs text-slate-400">derniers logs affichés</span>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-8">
        
        <!-- Stats Panel -->
        <div class="bg-slate-800 rounded-xl p-6 border border-slate-700 shadow-lg h-fit">
            <h2 class="text-purple-400 text-xs font-bold uppercase tracking-widest mb-4 border-b border-slate-700 pb-2">📊 Volume par Client</h2>
            <div class="space-y-3">
                <?php foreach($stats as $s): 
                    $percent = ($s['total'] / max(1, array_sum(array_column($stats, 'total')))) * 100;
                ?>
                <div>
                    <div class="flex justify-between text-sm mb-1">
                        <span class="font-bold text-white"><?php echo $s['client_name']; ?></span>
                        <span class="text-slate-400"><?php echo $s['total']; ?> SMS</span>
                    </div>
                    <div class="w-full bg-slate-900 rounded-full h-2">
                        <div class="bg-purple-600 h-2 rounded-full" style="width: <?php echo $percent; ?>%"></div>
                    </div>
                </div>
                <?php endforeach; ?>
            </div>
        </div>

        <!-- Logs Table -->
        <div class="lg:col-span-2 bg-slate-800 rounded-xl border border-slate-700 shadow-lg overflow-hidden">
             <div class="p-6 border-b border-slate-700">
                <h2 class="text-slate-200 font-bold text-lg">📝 Historique des envois</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full text-left text-sm">
                    <thead class="bg-slate-900 text-slate-400 text-xs uppercase">
                        <tr>
                            <th class="p-4">Date</th>
                            <th class="p-4">Client</th>
                            <th class="p-4">Message</th>
                            <th class="p-4 text-right">Statut</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-700">
                        <?php foreach($logs as $log): ?>
                        <tr class="hover:bg-slate-700/30 transition">
                            <td class="p-4 text-slate-400 whitespace-nowrap">
                                <?php echo date("d/m H:i", strtotime($log['sent_at'])); ?>
                            </td>
                            <td class="p-4 font-bold text-white">
                                <?php echo htmlspecialchars($log['client_name']); ?>
                                <div class="text-xs text-slate-500 font-normal"><?php echo $log['to_number']; ?></div>
                            </td>
                            <td class="p-4 text-slate-300 max-w-xs truncate" title="<?php echo htmlspecialchars($log['message']); ?>">
                                <?php echo htmlspecialchars($log['message']); ?>
                            </td>
                            <td class="p-4 text-right">
                                <?php if($log['status'] === 'SENT'): ?>
                                    <span class="bg-emerald-900/40 text-emerald-400 px-2 py-1 rounded text-xs font-bold border border-emerald-900">OK</span>
                                <?php else: ?>
                                    <span class="bg-red-900/40 text-red-400 px-2 py-1 rounded text-xs font-bold border border-red-900">ERR</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>

</body>
</html>
