<?php
// Variables: $onlineAdmins, $allAdmins, $activityLog, $summary, $unreadTotal
$currentAdminId = (int)$_SESSION['admin_id'];
$csrfToken = \Core\System\Csrf::token();

if (!function_exists('human_diff')) {
    function human_diff(?string $datetime): string {
        if (empty($datetime)) return '—';
        $diff = time() - strtotime($datetime);
        if ($diff < 60) return __('team_activity.just_now','just now');
        if ($diff < 3600) return floor($diff/60).' '.__('team_activity.min_ago','min ago');
        if ($diff < 86400) return floor($diff/3600).' '.__('team_activity.hr_ago','hr ago');
        return floor($diff/86400).' '.__('team_activity.days_ago','days ago');
    }
}

include __DIR__ . '/../header.php';
?>
<div class="content-wrapper">
<section class="content pt-3">
<div class="container-fluid pb-5">

<div class="d-flex flex-wrap justify-content-between align-items-start gap-3 mb-4">
    <div>
        <h3 class="mb-1 fw-bold"><i class="bi bi-activity me-2 text-success"></i><?= __('team_activity.title','Activity & Presence') ?></h3>
        <p class="text-muted mb-0 small"><?= __('team_activity.subtitle','Monitor team online status and login history') ?></p>
    </div>
    <div class="d-flex gap-2">
        <a href="/admin/team/communication" class="btn btn-outline-secondary btn-sm">
            <i class="bi bi-chat-dots me-1"></i><?= __('sidebar.team_communication','Messages') ?>
        </a>
        <a href="/admin/team/meetings" class="btn btn-outline-info btn-sm">
            <i class="bi bi-calendar-event me-1"></i><?= __('sidebar.team_meetings','Meetings') ?>
        </a>
    </div>
</div>

<div class="row g-3">

        <!-- Online Now -->
        <div class="col-lg-4">
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header bg-white border-bottom py-2 d-flex align-items-center gap-2">
                    <span class="badge bg-success rounded-circle p-1">&nbsp;</span>
                    <span class="fw-semibold small"><?= __('team_activity.online_now','Online Now') ?></span>
                    <span class="badge bg-success ms-auto"><?= count($onlineAdmins) ?></span>
                </div>
                <div class="list-group list-group-flush">
                    <?php foreach ($onlineAdmins as $a): ?>
                    <div class="list-group-item d-flex align-items-center gap-2 py-2">
                        <div class="position-relative flex-shrink-0">
                            <img src="<?= htmlspecialchars(!empty($a['avatar']) ? $a['avatar'] : '/admin/assets/adminlte-assets/img/avatar.png', ENT_QUOTES) ?>"
                                 class="rounded-circle" width="34" height="34" style="object-fit:cover" alt="">
                            <span class="position-absolute bottom-0 end-0 rounded-circle border border-white"
                                  style="width:9px;height:9px;background:#28a745"></span>
                        </div>
                        <div class="flex-grow-1">
                            <div class="fw-semibold small"><?= htmlspecialchars($a['username']) ?></div>
                            <div class="text-success" style="font-size:0.7rem">
                                <?= __('team_activity.active_ago','Active') ?> <?= human_diff($a['last_seen_at'] ?? '') ?>
                            </div>
                        </div>
                    </div>
                    <?php endforeach; ?>
                    <?php if (empty($onlineAdmins)): ?>
                    <div class="list-group-item text-center text-muted py-3 small">
                        <?= __('team_activity.no_one_online','No one else is online') ?>
                    </div>
                    <?php endif; ?>
                </div>
            </div>

            <!-- All members presence -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom py-2">
                    <span class="fw-semibold small"><?= __('team_activity.all_members','All Members') ?></span>
                </div>
                <div class="list-group list-group-flush">
                    <?php foreach ($allAdmins as $a): ?>
                    <div class="list-group-item d-flex align-items-center gap-2 py-2">
                        <div class="position-relative flex-shrink-0">
                            <img src="<?= htmlspecialchars(!empty($a['avatar']) ? $a['avatar'] : '/admin/assets/adminlte-assets/img/avatar.png', ENT_QUOTES) ?>"
                                 class="rounded-circle" width="32" height="32" style="object-fit:cover" alt="">
                            <span class="position-absolute bottom-0 end-0 rounded-circle border border-white"
                                  style="width:9px;height:9px;background:<?= $a['is_online'] ? '#28a745' : '#adb5bd' ?>"></span>
                        </div>
                        <div class="flex-grow-1 min-width-0">
                            <div class="fw-semibold small text-truncate"><?= htmlspecialchars($a['username']) ?></div>
                            <div class="text-muted" style="font-size:0.7rem">
                                <?= __('team.role_'.$a['role'], $a['role']) ?>
                                <?php if (!empty($a['last_seen_at'])): ?>
                                &bull; <?= human_diff($a['last_seen_at']) ?>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php if ($a['is_online']): ?>
                        <span class="badge bg-success-subtle text-success" style="font-size:.65rem"><?= __('team_activity.online','Online') ?></span>
                        <?php endif; ?>
                    </div>
                    <?php endforeach; ?>
                </div>
            </div>
        </div>

        <!-- Right: Stats + Activity Log -->
        <div class="col-lg-8">

            <!-- 30-day summary per admin -->
            <div class="card border-0 shadow-sm mb-3">
                <div class="card-header bg-white border-bottom py-2">
                    <span class="fw-semibold small"><?= __('team_activity.login_summary','Login Summary — Last 30 Days') ?></span>
                </div>
                <div class="table-responsive">
                    <table class="table table-sm table-hover mb-0">
                        <thead class="table-light">
                            <tr>
                                <th><?= __('team_activity.member','Member') ?></th>
                                <th><?= __('team_activity.role','Role') ?></th>
                                <th><?= __('team_activity.logins','Logins') ?></th>
                                <th><?= __('team_activity.last_login','Last Login') ?></th>
                                <th><?= __('team_activity.last_logout','Last Logout') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($summary as $s): ?>
                            <tr>
                                <td class="small fw-semibold"><?= htmlspecialchars($s['username']) ?></td>
                                <td><span class="badge bg-secondary-subtle text-secondary" style="font-size:.65rem"><?= __('team.role_'.$s['role'], $s['role']) ?></span></td>
                                <td><span class="badge bg-primary-subtle text-primary"><?= (int)$s['login_count'] ?></span></td>
                                <td class="small text-muted"><?= !empty($s['last_login']) ? date('d M y H:i', strtotime($s['last_login'])) : '—' ?></td>
                                <td class="small text-muted"><?= !empty($s['last_logout']) ? date('d M y H:i', strtotime($s['last_logout'])) : '—' ?></td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>

            <!-- Full activity log -->
            <div class="card border-0 shadow-sm">
                <div class="card-header bg-white border-bottom py-2 d-flex align-items-center justify-content-between">
                    <span class="fw-semibold small"><?= __('team_activity.activity_log','Activity Log') ?></span>
                    <span class="badge bg-secondary"><?= count($activityLog) ?></span>
                </div>
                <div class="table-responsive" style="max-height:420px;overflow-y:auto">
                    <table class="table table-sm table-hover mb-0">
                        <thead class="table-light sticky-top">
                            <tr>
                                <th><?= __('team_activity.time','Time') ?></th>
                                <th><?= __('team_activity.member','Member') ?></th>
                                <th><?= __('team_activity.action','Action') ?></th>
                                <th><?= __('team_activity.ip','IP Address') ?></th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($activityLog as $log): ?>
                            <tr>
                                <td class="small text-muted text-nowrap"><?= date('d M y H:i', strtotime($log['created_at'])) ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <img src="<?= htmlspecialchars(!empty($log['avatar']) ? $log['avatar'] : '/admin/assets/adminlte-assets/img/avatar.png', ENT_QUOTES) ?>"
                                             class="rounded-circle" width="22" height="22" style="object-fit:cover" alt="">
                                        <span class="small"><?= htmlspecialchars($log['username'] ?? '') ?></span>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge bg-<?= $log['action']==='login' ? 'success' : 'secondary' ?>">
                                        <i class="bi bi-box-arrow-<?= $log['action']==='login' ? 'in-right' : 'right' ?> me-1"></i>
                                        <?= __('team_activity.'.$log['action'], $log['action']) ?>
                                    </span>
                                </td>
                                <td class="small text-muted"><?= htmlspecialchars($log['ip_address'] ?? '—') ?></td>
                            </tr>
                            <?php endforeach; ?>
                            <?php if (empty($activityLog)): ?>
                            <tr><td colspan="4" class="text-center text-muted py-3 small"><?= __('team_activity.no_log','No activity recorded yet') ?></td></tr>
                            <?php endif; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>

</div>
</section>
<?php include __DIR__ . '/../footer.php'; ?>
