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='#2563eb')
    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='#2563eb')

    return {
        'theme': theme,
        'theme_mode': theme.mode if theme else 'light',
        'primary_color': theme.primary_color if theme else '#2563eb',
        'secondary_color': theme.get_secondary_color() if theme else '#1d4ed8',
    }

from settings.models import Shop

def shop_details(request):
    try:
        # Get shop based on domain
        domain = request.get_host().split(':')[0]  # Remove port if present

        # Map domains to shops (you can also store this in database)
        domain_shop_mapping = {
            'hyperkthika.posapp.co.ke': 1,    # Main POS system
            'shop.hyperkenyaltd.com': 1,      # Shop subdomain
            'hyperkenyaltd.com': 1,           # Main domain
            'localhost': 1,                   # For development
            '127.0.0.1': 1,                   # For development
        }

        shop_id = domain_shop_mapping.get(domain, 1)  # Default to first shop
        shop = Shop.objects.get(id=shop_id)

        if shop:
            return {
                'shop': shop,
                'shop_name': shop.name,
                'shop_logo_url': shop.get_logo_url(),
                'current_domain': domain,
            }
    except Shop.DoesNotExist:
        pass

    return {
        'shop': None,
        'shop_name': 'Your Shop',
        'shop_logo_url': '/static/images/default_logo.png',
        'current_domain': request.get_host().split(':')[0],
    }