<?php
use Spatie\Activitylog\Models\Activity;
$activities = Activity::with('causer')->latest()->take(10)->get();

function activityBadgeClass($event) {
    return match ($event) {
        'created' => 'bg-success-50 text-success-600 dark:bg-success-500/15 dark:text-success-500',
        'updated' => 'bg-warning-50 text-warning-600 dark:bg-warning-500/15 dark:text-orange-400',
        'deleted' => 'bg-error-50 text-error-600 dark:bg-error-500/15 dark:text-error-500',
        default   => 'bg-blue-light-50 text-blue-light-500 dark:bg-blue-light-500/15 dark:text-blue-light-500',
    };
}
?>
<style>
.activity-log-widget h3 {
    text-align: center;
    margin-bottom: 16px;
    font-size: 17px;
    font-weight: 600;
    color: #1e293b;
}
.dark .activity-log-widget h3 { color: #e2e8f0; }
.activity-log-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}
.activity-log-table th,
.activity-log-table td {
    padding: 12px 16px;
    text-align: left;
    border-bottom: 1px solid #f1f5f9;
}
.dark .activity-log-table th,
.dark .activity-log-table td { border-bottom-color: #1f2937; }
.activity-log-table th {
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: #64748b;
    background: #f8fafc;
}
.dark .activity-log-table th { color: #94a3b8; background: #111827; }
.activity-log-table td {
    color: #334155;
    vertical-align: middle;
}
.dark .activity-log-table td { color: #cbd5e1; }
.activity-log-table tr:hover td { background: #f8fafc; }
.dark .activity-log-table tr:hover td { background: #1f2937; }
.activity-log-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    border-radius: 9999px;
    padding: 4px 10px;
    font-size: 12px;
    font-weight: 500;
    text-transform: capitalize;
}
.activity-log-empty {
    text-align: center;
    padding: 40px 20px;
    color: #64748b;
}
.dark .activity-log-empty { color: #94a3b8; }
@media (max-width: 640px) {
    .activity-log-table th,
    .activity-log-table td { padding: 10px 12px; font-size: 13px; }
}
</style>
<div class="activity-log-widget">
    <h3>Latest Activity Log</h3>
    <div class="max-w-full overflow-x-auto">
        <table class="activity-log-table">
            <thead>
                <tr>
                    <th>Date & Time</th>
                    <th>User Name</th>
                    <th>Action</th>
                    <th>Module Name</th>
                </tr>
            </thead>
            <tbody>
                <?php if ($activities->isNotEmpty()): ?>
                    <?php foreach ($activities as $activity): ?>
                        <?php
                        $causer = $activity->causer;
                        $userName = $causer
                            ? ($causer->username ?? trim(($causer->first_name ?? '') . ' ' . ($causer->last_name ?? '')) ?: 'User #' . $causer->id)
                            : 'System';
                        ?>
                        <tr>
                            <td>
                                <span class="block text-sm font-medium"><?= $activity->created_at?->format('d M Y') ?></span>
                                <span class="block text-xs text-gray-500 dark:text-gray-400"><?= $activity->created_at?->format('H:i') ?></span>
                            </td>
                            <td class="font-medium"><?= e($userName) ?></td>
                            <td>
                                <span class="activity-log-badge <?= activityBadgeClass($activity->event) ?>">
                                    <?= e(ucfirst($activity->event ?? 'N/A')) ?>
                                </span>
                            </td>
                            <td><?= e($activity->subject_type ?? '-') ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr>
                        <td colspan="4" class="activity-log-empty">
                            No recent activity logs found.
                        </td>
                    </tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>