public function previewAction(Request $request)
    {
        $rawCriteria = json_decode($request->get('filters'), true);

        $messageManager = $this->get('phlexible_message.message_manager');

        $criteria = new Criteria();
        $criteria->setMode(Criteria::MODE_OR);
        foreach ($rawCriteria as $group) {
            $criteriaGroup = new Criteria();
            $criteriaGroup->setMode(Criteria::MODE_AND);
            foreach ($group as $row) {
                if (!strlen($row['value'])) {
                    continue;
                }

                $criterium = new Criterium($row['key'], $row['value']);
                $criteriaGroup->add($criterium);
            }
            if ($criteriaGroup->count()) {
                $criteria->addCriteria($criteriaGroup);
            }
        }

        if (!$criteria->count()) {
            return new JsonResponse([
                'total' => 0,
                'messages' => [],
            ]);
        }

        $messages = $messageManager->findByCriteria($criteria, ['createdAt' => 'DESC'], 20);
        $count = $messageManager->countByCriteria($criteria);

        $priorityList = $messageManager->getPriorityNames();
        $typeList = $messageManager->getTypeNames();

        $data = [];
        foreach ($messages as $message) {
            $data[] = [
                'subject' => $message->getSubject(),
                'body' => nl2br($message->getBody()),
                'priority' => $priorityList[$message->getPriority()],
                'type' => $typeList[$message->getType()],
                'channel' => $message->getChannel(),
                'role' => $message->getRole(),
                'created_at' => $message->getCreatedAt()->format('Y-m-d H:i:s'),
                'user' => $message->getUser(),
            ];
        }

        return new JsonResponse([
            'total' => $count,
            'messages' => $data,
        ]);
    }