from datetime import datetime
import re

def slugify(text):
    """Convert text to a slug format (lowercase, hyphens instead of spaces, alphanumeric only)"""
    if not text:
        return ''
    
    # Convert to lowercase
    text = text.lower()
    
    # Replace spaces with hyphens
    text = text.replace(' ', '-')
    
    # Remove special characters
    text = re.sub(r'[^a-z0-9\-]', '', text)
    
    # Remove multiple hyphens
    text = re.sub(r'-+', '-', text)
    
    # Remove leading/trailing hyphens
    text = text.strip('-')
    
    return text

def timeago(date):
    """Convert a datetime to a 'time ago' string."""
    if not date:
        return ''
        
    now = datetime.utcnow()
    diff = now - date
    
    seconds = diff.total_seconds()
    minutes = int(seconds / 60)
    hours = int(minutes / 60)
    days = int(hours / 24)
    
    if seconds < 60:
        return 'just now'
    elif minutes < 60:
        return f'{minutes} minute{"s" if minutes != 1 else ""} ago'
    elif hours < 24:
        return f'{hours} hour{"s" if hours != 1 else ""} ago'
    elif days < 7:
        return f'{days} day{"s" if days != 1 else ""} ago'
    else:
        return date.strftime('%B %d, %Y')
