<?php
require_once __DIR__ . '/includes/db.php';
require_once __DIR__ . '/includes/session_check.php';
checkSession();
require_once __DIR__ . '/includes/message_handler.php';

$pageTitle = 'Mensajes Enviados';

$userId = getCurrentUserId();
$messages = getSentMessages(isAdmin() ? null : $userId, 100);

require_once __DIR__ . '/templates/header.php';
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-send"></i> Mensajes Enviados</h2>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-body">
        <?php if (empty($messages)): ?>
            <p class="text-muted text-center py-4">No hay mensajes enviados</p>
        <?php else: ?>
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>Destinatario</th>
                            <th>Plataforma</th>
                            <th>Fecha de Envío</th>
                            <th>Mensajes Enviados</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($messages as $msg): ?>
                        <tr>
                            <td>#<?= $msg['id'] ?></td>
                            <td><?= htmlspecialchars($msg['recipient_name']) ?></td>
                            <td>
                                <?php if ($msg['platform'] === 'discord'): ?>
                                    <i class="bi bi-discord platform-discord"></i> Discord
                                <?php else: ?>
                                    <i class="bi bi-telegram platform-telegram"></i> Telegram
                                <?php endif; ?>
                            </td>
                            <td><?= date('d/m/Y H:i:s', strtotime($msg['sent_at'])) ?></td>
                            <td><?= $msg['message_count'] ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        <?php endif; ?>
    </div>
</div>

<?php require_once __DIR__ . '/templates/footer.php'; ?>
