Files
whatsapp/api/get_conversation_stats.php
2026-01-21 08:28:54 -05:00

82 lines
2.5 KiB
PHP

<?php
/**
* API para obtener estadísticas de conversación de un usuario
* Versión: 1.0
*/
header('Content-Type: application/json');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
header('Access-Control-Allow-Headers: Content-Type, Authorization');
// Manejar preflight requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
exit(0);
}
try {
// Obtener parámetros
$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;
$debug = isset($_GET['debug']) && $_GET['debug'] === 'true';
if ($debug) {
error_log("=== GET CONVERSATION STATS DEBUG ===");
error_log("User ID: " . $user_id);
}
// Validar parámetros
if ($user_id <= 0) {
throw new Exception('ID de usuario inválido');
}
// Por ahora, simular estadísticas
// En el futuro, esto consultaría la base de datos real
$stats = [
'user_id' => $user_id,
'total_conversations' => rand(10, 100),
'conversations_today' => rand(0, 15),
'conversations_this_week' => rand(5, 50),
'conversations_this_month' => rand(20, 80),
'first_message_date' => date('Y-m-d H:i:s', strtotime('-' . rand(1, 30) . ' days')),
'last_message_date' => date('Y-m-d H:i:s', strtotime('-' . rand(1, 24) . ' hours')),
'avg_response_time' => rand(5, 120) . ' minutos',
'most_active_hour' => rand(9, 18) . ':00',
'conversation_status' => 'active',
'tags' => ['cliente', 'activo'],
'notes' => 'Usuario activo con buena interacción'
];
if ($debug) {
error_log("Estadísticas simuladas: " . json_encode($stats));
}
// Respuesta exitosa
$response = [
'success' => true,
'data' => $stats,
'message' => 'Estadísticas obtenidas correctamente',
'debug' => $debug ? [
'timestamp' => date('Y-m-d H:i:s'),
'user_id' => $user_id,
'simulated' => true
] : null
];
echo json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
} catch (Exception $e) {
$error_response = [
'success' => false,
'error' => $e->getMessage(),
'debug' => $debug ? [
'timestamp' => date('Y-m-d H:i:s'),
'file' => __FILE__,
'line' => $e->getLine(),
'trace' => $e->getTraceAsString()
] : null
];
http_response_code(500);
echo json_encode($error_response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
}
?>