"""Unread notification count and 'My Profile' link for the site header."""
from accounts.notification_service import unread_notification_count


def notifications_context(request):
    user = getattr(request, 'user', None)
    authed = bool(user and user.is_authenticated)
    count = unread_notification_count(user) if authed else 0
    saved = 0
    my_profile_url = ''
    if authed:
        from jobs.models import SavedJob

        saved = SavedJob.objects.filter(user=user).count()

        if getattr(user, 'is_seeker', False):
            from seekers.models import SeekerProfile

            profile = SeekerProfile.objects.filter(user=user).only('slug').first()
            if profile:
                my_profile_url = profile.get_absolute_url()
        elif getattr(user, 'is_company', False):
            from companies.models import Company

            company = Company.objects.filter(owner=user).only('slug').first()
            if company:
                my_profile_url = company.get_absolute_url()
    return {
        'unread_notifications_count': count,
        'saved_jobs_count': saved,
        'my_profile_url': my_profile_url,
    }
