<?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-crosshair"></i> <?= t('Dashboard') ?></h2>
    <span class="text-muted" style="font-family: 'Share Tech Mono', monospace; font-size: 0.8rem;">
        // <?= t('BIENVENIDO') ?>, <?= strtoupper(htmlspecialchars(getCurrentUsername())) ?>
    </span>
</div>

<div class="row mb-4">
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="stat-card-icon me-3" style="border-color: var(--accent-orange); color: var(--accent-orange);">
                        <i class="bi bi-stopwatch fs-5"></i>
                    </div>
                    <div>
                        <div style="font-family: 'Share Tech Mono', monospace; font-size: 2rem; color: var(--accent-orange);"><?= $pendingCount ?></div>
                        <small class="text-muted text-uppercase" style="letter-spacing: 1px; font-size: 0.7rem;"><?= t('Pendientes') ?></small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="stat-card-icon me-3" style="border-color: var(--hud-cyan); color: var(--hud-cyan);">
                        <i class="bi bi-check2-all fs-5"></i>
                    </div>
                    <div>
                        <div style="font-family: 'Share Tech Mono', monospace; font-size: 2rem; color: var(--hud-cyan);"><?= $sentToday ?></div>
                        <small class="text-muted text-uppercase" style="letter-spacing: 1px; font-size: 0.7rem;"><?= t('Enviados Hoy') ?></small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="stat-card-icon me-3" style="border-color: var(--discord-color); color: var(--discord-color);">
                        <i class="bi bi-discord fs-5"></i>
                    </div>
                    <div>
                        <div style="font-family: 'Share Tech Mono', monospace; font-size: 2rem; color: var(--discord-color);">
                            <?= count(array_filter($scheduledMessages, fn($m) => $m['platform'] === 'discord')) ?>
                        </div>
                        <small class="text-muted text-uppercase" style="letter-spacing: 1px; font-size: 0.7rem;"><?= t('Discord') ?></small>
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <div class="col-md-3">
        <div class="card border-0 shadow-sm h-100">
            <div class="card-body">
                <div class="d-flex align-items-center">
                    <div class="stat-card-icon me-3" style="border-color: var(--telegram-color); color: var(--telegram-color);">
                        <i class="bi bi-telegram fs-5"></i>
                    </div>
                    <div>
                        <div style="font-family: 'Share Tech Mono', monospace; font-size: 2rem; color: var(--telegram-color);">
                            <?= count(array_filter($scheduledMessages, fn($m) => $m['platform'] === 'telegram')) ?>
                        </div>
                        <small class="text-muted text-uppercase" style="letter-spacing: 1px; font-size: 0.7rem;"><?= t('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 border-0">
                <h5 class="mb-0"><i class="bi bi-list-check"></i> <?= t('Próximos Mensajes') ?></h5>
            </div>
            <div class="card-body">
                <?php if (empty($scheduledMessages)): ?>
                    <p class="text-muted text-center py-4">// <?= t('NO HAY MENSAJES PROGRAMADOS') ?></p>
                <?php else: ?>
                    <div class="table-responsive">
                        <table class="table table-hover">
                            <thead>
                                <tr>
                                    <th><?= t('Destinatario') ?></th>
                                    <th><?= t('Plataforma') ?></th>
                                    <th><?= t('Fecha') ?></th>
                                    <th><?= t('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 style="font-family: 'Share Tech Mono', monospace;"><?= 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'
                                        } ?>">
                                            <?= t($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 border-0">
                <h5 class="mb-0"><i class="bi bi-lightning"></i> <?= t('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-pencil-square"></i> <?= t('Nuevo Mensaje') ?>
                    </a>
                    <a href="recurrentes.php" class="btn btn-outline-secondary">
                        <i class="bi bi-collection"></i> <?= t('Ver Plantillas') ?>
                    </a>
                    <a href="scheduled_messages.php" class="btn btn-outline-secondary">
                        <i class="bi bi-list"></i> <?= t('Ver Todos los Programados') ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>

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