"""
Context processors for Themis.

Makes user preferences, app configuration, and notification counts
available in all templates.
"""
from django.conf import settings


def themis_settings(request):
    """
    Provide Themis app configuration to all templates.

    Reads THEMIS_* settings from Django settings and provides
    defaults for any that are not configured.

    Context variables:
        themis_app_name: Application display name
        themis_notification_poll_interval: Polling interval in seconds (0=disabled)
        CASDOOR_ENABLED: Whether SSO is active
        ALLOW_LOCAL_LOGIN: Whether the local login form is shown to non-superusers
    """
    return {
        "themis_app_name": getattr(settings, "THEMIS_APP_NAME", "Application"),
        "themis_notification_poll_interval": getattr(
            settings, "THEMIS_NOTIFICATION_POLL_INTERVAL", 60
        ),
        "CASDOOR_ENABLED": getattr(settings, "CASDOOR_ENABLED", False),
        "ALLOW_LOCAL_LOGIN": getattr(settings, "ALLOW_LOCAL_LOGIN", False),
    }


def user_preferences(request):
    """
    Provide user preferences to all templates.

    Context variables:
        user_timezone: User's effective timezone name
        user_date_format: User's date format preference
        user_time_format: User's time format preference
        user_is_traveling: Boolean if user has a current timezone set
        user_theme_mode: Theme mode (light/dark/auto)
        user_theme_name: DaisyUI light theme name
        user_dark_theme_name: DaisyUI dark theme name
        user_profile: Full UserProfile object (or None)
    """
    if request.user.is_authenticated and hasattr(request.user, "profile"):
        profile = request.user.profile
        return {
            "user_timezone": profile.effective_timezone,
            "user_date_format": profile.date_format,
            "user_time_format": profile.time_format,
            "user_is_traveling": profile.is_traveling,
            "user_theme_mode": getattr(request, "themis_theme_mode", profile.theme_mode),
            "user_theme_name": getattr(request, "themis_theme_name", profile.theme_name),
            "user_dark_theme_name": getattr(
                request, "themis_dark_theme_name", profile.dark_theme_name
            ),
            "user_profile": profile,
        }

    return {
        "user_timezone": "UTC",
        "user_date_format": "YYYY-MM-DD",
        "user_time_format": "24-hour",
        "user_is_traveling": False,
        "user_theme_mode": "auto",
        "user_theme_name": "corporate",
        "user_dark_theme_name": "business",
        "user_profile": None,
    }


def notifications(request):
    """
    Provide notification count to all templates.

    Context variables:
        themis_unread_notification_count: Number of unread notifications
        themis_notifications_enabled: Whether notifications are enabled for this user
        themis_browser_notifications_enabled: Whether browser notifications are enabled
    """
    if request.user.is_authenticated and hasattr(request.user, "profile"):
        from themis.notifications import get_unread_count

        profile = request.user.profile
        if profile.notifications_enabled:
            return {
                "themis_unread_notification_count": get_unread_count(request.user),
                "themis_notifications_enabled": True,
                "themis_browser_notifications_enabled": profile.browser_notifications_enabled,
            }
        return {
            "themis_unread_notification_count": 0,
            "themis_notifications_enabled": False,
            "themis_browser_notifications_enabled": False,
        }

    return {
        "themis_unread_notification_count": 0,
        "themis_notifications_enabled": False,
        "themis_browser_notifications_enabled": False,
    }
