
from .models import Theme, Shop

def theme_context(request):
    """Add theme context to all templates"""
    theme = None
    try:
        theme = Theme.objects.first()
        if not theme:
            # Create default theme if none exists
            theme = Theme.objects.create(mode='light', primary_color='#28a745')
    except Exception as e:
        print(f"Error in theme_context: {e}")
        # Create a basic theme object for template rendering
        theme = Theme(mode='light', primary_color='#28a745')
    
    return {
        'theme': theme,
        'theme_mode': theme.mode if theme else 'light',
        'primary_color': theme.primary_color if theme else '#28a745',
        'secondary_color': theme.get_secondary_color() if theme else '#218838',
    }

def shop_details(request):
    """Add shop details to all templates"""
    shop = None
    try:
        shop = Shop.objects.first()
        if not shop:
            # Create default shop if none exists
            shop = Shop.objects.create(
                name="POS 254",
                address="Nairobi, Kenya",
                phone="+254700000000",
                email="info@pos254.com",
                website="https://pos254.com"
            )
    except Exception as e:
        print(f"Error in shop_details context: {e}")
        # Create a basic shop object for template rendering
        class FallbackShop:
            name = "POS 254"
            address = "Nairobi, Kenya"
            phone = "+254700000000"
            email = "info@pos254.com"
            website = "https://pos254.com"
            logo = None
            
            def __str__(self):
                return self.name
        
        shop = FallbackShop()
    
    return {
        'shop': shop,
        'shop_name': shop.name if shop else "POS 254",
        'shop_phone': shop.phone if shop else "+254700000000",
        'shop_email': shop.email if shop else "info@pos254.com",
        'shop_address': shop.address if shop else "Nairobi, Kenya",
        'shop_website': getattr(shop, 'website', '') if shop else "",
        'shop_logo': shop.logo if shop and hasattr(shop, 'logo') else None,
    }
