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

$userId = getCurrentUserId();
$scheduledMessages = getScheduledMessages($userId);
$sentMessages = getSentMessages($userId, 5);

$pendingCount = count(array_filter($scheduledMessages, fn($m) => $m['status'] === 'pending'));
$sentToday = count(array_filter($sentMessages, fn($m) => date('Y-m-d', strtotime($m['sent_at'])) === date('Y-m-d')));
?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2><i class="bi bi-house-door"></i> Dashboard</h2>
    <span class="text-muted">Bienvenido, <?= htmlspecialchars(getCurrentUsername()) ?></span>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="bg-primary bg-opacity-10 p-3 rounded me-3">
                        <i class="bi bi-clock text-primary fs-4"></i>
                    </div>
                    <div>
                        <h3 class="mb-0"><?= $pendingCount ?></h3>
                        <small class="text-muted">Mensajes Pendientes</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="bg-success bg-opacity-10 p-3 rounded me-3">
                        <i class="bi bi-check-circle text-success fs-4"></i>
                    </div>
                    <div>
                        <h3 class="mb-0"><?= $sentToday ?></h3>
                        <small class="text-muted">Enviados Hoy</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="bg-discord bg-opacity-10 p-3 rounded me-3" style="background-color: rgba(88,101,242,0.1)">
                        <i class="bi bi-discord fs-4" style="color: #5865F2"></i>
                    </div>
                    <div>
                        <h3 class="mb-0">
                            <?= count(array_filter($scheduledMessages, fn($m) => $m['platform'] === 'discord')) ?>
                        </h3>
                        <small class="text-muted">Discord</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="bg-info bg-opacity-10 p-3 rounded me-3">
                        <i class="bi bi-telegram text-info fs-4"></i>
                    </div>
                    <div>
                        <h3 class="mb-0">
                            <?= count(array_filter($scheduledMessages, fn($m) => $m['platform'] === 'telegram')) ?>
                        </h3>
                        <small class="text-muted">Telegram</small>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<div class="row">
    <div class="col-md-8">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0">
                <h5 class="mb-0"><i class="bi bi-clock"></i> Próximos Mensajes</h5>
            </div>
            <div class="card-body">
                <?php if (empty($scheduledMessages)): ?>
                    <p class="text-muted text-center py-4">No hay mensajes programados</p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th>Destinatario</th>
                                    <th>Plataforma</th>
                                    <th>Fecha</th>
                                    <th>Estado</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach (array_slice($scheduledMessages, 0, 10) as $msg): ?>
                                <tr>
                                    <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', strtotime($msg['send_time'])) ?></td>
                                    <td>
                                        <span class="badge bg-<?= match($msg['status']) {
                                            'pending' => 'warning',
                                            'processing' => 'info',
                                            'sent' => 'success',
                                            'failed' => 'danger',
                                            'disabled' => 'secondary',
                                            default => 'light'
                                        } ?>">
                                            <?= $msg['status'] ?>
                                        </span>
                                    </td>
                                </tr>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>
    
    <div class="col-md-4">
        <div class="card border-0 shadow-sm">
            <div class="card-header bg-white border-0">
                <h5 class="mb-0"><i class="bi bi-lightning"></i> Acciones Rápidas</h5>
            </div>
            <div class="card-body">
                <div class="d-grid gap-2">
                    <a href="create_message.php" class="btn btn-primary">
                        <i class="bi bi-plus-circle"></i> Nuevo Mensaje
                    </a>
                    <a href="recurrentes.php" class="btn btn-outline-secondary">
                        <i class="bi bi-repeat"></i> Ver Plantillas
                    </a>
                    <a href="scheduled_messages.php" class="btn btn-outline-secondary">
                        <i class="bi bi-list"></i> Ver Todos los Programados
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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