<?php
require_once __DIR__ . '/includes/layout.php';
require_login();

$devices = list_devices();
$stats = get_dashboard_stats();

render_header(
    'Dispositivos',
    'devices',
    'Aparelhos conectados ao painel — gerencie cada ecossistema por ID',
    [
        ['label' => 'Dashboard', 'url' => 'dashboard.php'],
        ['label' => 'Dispositivos', 'url' => null],
    ]
);
?>

<div class="devices-toolbar">
    <div class="toolbar-stats">
        <span class="toolbar-pill pill-green"><?= (int) $stats['online_now'] ?> online</span>
        <span class="toolbar-pill pill-blue"><?= (int) $stats['total_devices'] ?> registrados</span>
        <span class="toolbar-pill pill-red"><?= (int) $stats['blocked_count'] ?> bloqueados</span>
    </div>
    <a href="dns.php" class="btn secondary btn-sm">DNS global</a>
</div>

<div class="card card-flush">
    <?php if (empty($devices)): ?>
        <div class="empty-state">
            <div class="empty-icon">📡</div>
            <h3>Nenhum dispositivo conectado</h3>
            <p>Instale o launcher no Android TV e abra o app. O registro é automático.</p>
            <code class="code-block">POST <?= htmlspecialchars(PANEL_BASE_URL) ?>/api/v1/register.php</code>
        </div>
    <?php else: ?>
        <div class="table-wrap">
            <table class="data-table">
                <thead>
                    <tr>
                        <th>Dispositivo</th>
                        <th>Presença</th>
                        <th>Branding</th>
                        <th>Apps</th>
                        <th>Segurança</th>
                        <th>Último acesso</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                <?php foreach ($devices as $dev): ?>
                    <?php
                    $blocked = (int) $dev['launcher_blocked'] === 1;
                    $locked = (int) $dev['settings_lock_enabled'] === 1 && !empty($dev['settings_lock_hash']);
                    $presence = device_presence_status($dev['last_seen_at']);
                    $hasLogo = !empty($dev['logo_file']);
                    $hasWall = !empty($dev['wallpaper_file']);
                    ?>
                    <tr>
                        <td>
                            <div class="cell-device">
                                <div class="device-avatar <?= $blocked ? 'blocked' : '' ?>"><?= $blocked ? '⛔' : '📺' ?></div>
                                <div>
                                    <strong><?= htmlspecialchars($dev['label'] ?: 'Sem nome') ?></strong>
                                    <div class="table-sub"><?= htmlspecialchars($dev['app_name'] ?: 'NEXUS TV') ?></div>
                                    <code class="id-chip-sm" title="<?= htmlspecialchars($dev['id']) ?>"><?= htmlspecialchars(substr($dev['id'], 0, 13)) ?>…</code>
                                </div>
                            </div>
                        </td>
                        <td>
                            <span class="presence-pill <?= htmlspecialchars($presence['class']) ?>">
                                <?= htmlspecialchars($presence['label']) ?>
                            </span>
                            <div class="table-sub"><?= htmlspecialchars($dev['device_model'] ?: '—') ?></div>
                        </td>
                        <td>
                            <div class="mini-tags">
                                <span class="mini-tag <?= $hasLogo ? 'on' : 'off' ?>">Logo</span>
                                <span class="mini-tag <?= $hasWall ? 'on' : 'off' ?>">Wall</span>
                            </div>
                        </td>
                        <td><strong><?= (int) $dev['apps_count'] ?></strong><span class="table-sub">/<?= MAX_APPS ?></span></td>
                        <td>
                            <?php if ($blocked): ?>
                                <span class="status-pill off">Bloqueado</span>
                            <?php elseif ($locked): ?>
                                <span class="status-pill warn">Config 🔒</span>
                            <?php else: ?>
                                <span class="status-pill on">Normal</span>
                            <?php endif; ?>
                        </td>
                        <td>
                            <?= htmlspecialchars(format_device_seen($dev['last_seen_at'])) ?>
                            <?php if ($dev['last_ip']): ?>
                                <div class="table-sub"><?= htmlspecialchars($dev['last_ip']) ?></div>
                            <?php endif; ?>
                        </td>
                        <td class="actions-cell">
                            <a href="device.php?id=<?= urlencode($dev['id']) ?>" class="btn primary btn-sm">Gerenciar</a>
                        </td>
                    </tr>
                <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    <?php endif; ?>
</div>

<?php render_footer(); ?>
