/**
 * Contextual Help System for LocalLendables
 * Guides users from informal chat inquiries to formal borrow requests
 */

// Enhanced borrowing keywords for better detection
const BORROWING_KEYWORDS = [
    // Direct borrowing words
    'borrow', 'rent', 'use', 'need', 'lend', 'loan',
    // Permission asking
    'can i', 'may i', 'could i', 'would it be possible',
    // Availability inquiries
    'available', 'free', 'open',
    // Time-related requests
    'dates', 'when', 'tomorrow', 'today', 'weekend', 'week', 'month', 'next',
    // Action words
    'pick up', 'return', 'bring back', 'get', 'take'
];

// Check if message contains borrowing-related content
function checkForBorrowingKeywords(message) {
    const lowerMessage = message.toLowerCase();
    return BORROWING_KEYWORDS.some(keyword => lowerMessage.includes(keyword));
}

// Show the borrowing help banner
function showBorrowingHelp() {
    const banner = document.getElementById('borrowingHelpBanner');
    if (banner && banner.classList.contains('d-none')) {
        banner.classList.remove('d-none');
        
        // Auto-hide after 12 seconds if not manually dismissed
        setTimeout(() => {
            if (banner && !banner.classList.contains('d-none')) {
                dismissBorrowingHelp();
            }
        }, 12000);
    }
}

// Dismiss the borrowing help banner
function dismissBorrowingHelp() {
    const banner = document.getElementById('borrowingHelpBanner');
    if (banner) {
        banner.classList.add('d-none');
        // Store dismissal to prevent showing again in this session
        sessionStorage.setItem('borrowingHelpDismissed', 'true');
    }
}

// Analytics: Track help banner interactions
function trackBorrowingHelpInteraction(action) {
    console.log(`Contextual Help: ${action} - Banner interaction logged`);
    // Could be extended to send analytics to backend if needed
}

// Initialize contextual help system
document.addEventListener('DOMContentLoaded', function() {
    console.log('Contextual help system initialized');
    
    // Reset help dismissal on new page load
    if (performance.navigation.type === 1) { // Page refresh
        sessionStorage.removeItem('borrowingHelpDismissed');
    }
});