<?php
// Endpoint pour retourner la liste des conversations en attente (aucune réponse admin)
header('Content-Type: application/json; charset=utf-8');
require_once 'config/config.php';
require_once 'config/db.php';

try {
    $db = getDbConnection();
    $stmt = $db->query("SELECT c.conversation_id, MIN(c.user_name) as user_name, MIN(c.user_email) as user_email, MIN(c.created_at) as created_at
        FROM support_chat c
        WHERE c.sender = 'user'
        AND c.conversation_id NOT IN (
            SELECT conversation_id FROM support_chat WHERE sender = 'admin'
        )
        AND c.conversation_id NOT IN (
            SELECT conversation_id FROM support_chat WHERE closed = 1
        )
        GROUP BY c.conversation_id
        ORDER BY created_at ASC");
    $pending = $stmt->fetchAll(PDO::FETCH_ASSOC);
    echo json_encode(['success'=>true,'pending'=>$pending]);
} catch(Exception $e) {
    echo json_encode(['success'=>false,'error'=>$e->getMessage()]);
}
?>
