document.addEventListener('DOMContentLoaded', function() {
    function fetchUserLogs() {

            fetch('/fetch_user_logs.php')
            .then(res => res.json())
            .then(data => {
                const container = document.getElementById('activityLogContainer');
                container.innerHTML = '';
        
                data.forEach(log => {
                const icon = getLogIcon(log.action_type);
                const card = document.createElement('div');
                card.className = 'card bg-dark border-secondary text-light shadow-sm';
        
                card.innerHTML = `
                    <div class="card-body d-flex align-items-center">
                    <div class="me-3 fs-4">${icon}</div>
                    <div>
                        <div class="fw-semibold">${log.description}</div>
                        <small class="text-light">${log.created_at}</small>
                    </div>
                    </div>
                `;
        
                container.appendChild(card);
                });
        });
        
        function getLogIcon(type) {
        switch (type) {
            case 'deposit': return '💰';
            case 'withdrawal': return '📤';
            case 'investment': return '📈';
            case 'withdrawal_approved': return '✅';
            default: return '📌';
        }
        }
        
        function formatTimeAgo(timestamp) {
        const time = new Date(timestamp);
        const now = new Date();
        const diff = Math.floor((now - time) / 60000);
        if (diff < 1) return 'Just now';
        if (diff < 60) return `${diff} min ago`;
        const hours = Math.floor(diff / 60);
        if (hours < 24) return `${hours}h ago`;
        return time.toLocaleString();
        }
    }

    fetchUserLogs();

    setInterval(fetchUserLogs, 2000);

})