'failure', 'message' => 'Unauthorized access. Admin role required.']); exit; } try { $limit = filterRequest('limit', 'int') ?? 50; $countryCode = filterRequest('country_code'); $sql = "SELECT l.*, p.first_name, p.last_name FROM marketing_campaigns_log l LEFT JOIN passengers p ON p.id = l.passenger_id"; $params = []; if ($countryCode) { $sql .= " WHERE l.country_code = :country"; $params[':country'] = strtoupper($countryCode); } $sql .= " ORDER BY l.sent_at DESC LIMIT :limit"; $stmt = $con->prepare($sql); $stmt->bindValue(':limit', $limit, PDO::PARAM_INT); foreach ($params as $key => $val) { $stmt->bindValue($key, $val); } $stmt->execute(); $logs = $stmt->fetchAll(PDO::FETCH_ASSOC); // Decrypt names or just return them // (Names are not encrypted in this schema, only phones are, so we can return directly) // Aggregate statistics for Dashboard charts $sqlStats = "SELECT message_type, COUNT(*) as count FROM marketing_campaigns_log"; if ($countryCode) { $sqlStats .= " WHERE country_code = :country"; $stmtStats = $con->prepare($sqlStats); $stmtStats->execute([':country' => strtoupper($countryCode)]); } else { $stmtStats = $con->prepare($sqlStats); $stmtStats->execute(); } $stats = $stmtStats->fetchAll(PDO::FETCH_ASSOC); jsonSuccess([ 'logs' => $logs, 'stats' => $stats ]); } catch (Exception $e) { error_log("[get_campaigns_log.php] Error: " . $e->getMessage()); jsonError("Failed to fetch campaigns log: " . $e->getMessage()); }