# authentic2 - versatile identity manager
# Copyright (C) 2010-2020 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import functools
import uuid

from django import forms
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied, ValidationError
from django.core.validators import EmailValidator
from django.db.models import Q
from django.utils.translation import ugettext_lazy as _

from authentic2.apps.journal.forms import JournalForm
from authentic2.apps.journal.models import EventType, n_2_pairing
from authentic2.apps.journal.search_engine import JournalSearchEngine
from authentic2.apps.journal.views import JournalView
from authentic2.custom_user.models import DeletedUser

from . import views

User = get_user_model()


class JournalSearchEngine(JournalSearchEngine):
    def search_by_uuid(self, lexem):
        # by user uuid
        try:
            user_uuid = uuid.UUID(lexem)
        except ValueError:
            yield self.q_false
        else:
            yield Q(user__uuid=user_uuid.hex)

    search_by_uuid.documentation = _(
        '''\
You can use <tt>uuid:1234</tt> to find all events related \
to user whose UUID is <tt>1234</tt>.'''
    )
    unmatched = None

    def lexem_queries(self, lexem):
        queries = list(super().lexem_queries(lexem))
        if queries:
            yield from queries
        elif '@' in lexem:
            # fallback for raw email
            try:
                EmailValidator(lexem)
            except ValidationError:
                pass
            else:
                yield from super().lexem_queries('email:' + lexem)
                yield from super().lexem_queries('username:' + lexem)

    def unmatched_lexems_query(self, lexems):
        fullname = ' '.join(lexem.strip() for lexem in lexems if lexem.strip())
        if fullname:
            users = User.objects.find_duplicates(fullname=fullname)
            return self.query_for_users(users)

    def search_by_email(self, email):
        yield from super().search_by_email(email)
        pks = list(DeletedUser.objects.filter(old_email=email).values_list('old_user_id', flat=True))
        yield Q(user_id__in=pks)
        user_ct = ContentType.objects.get_for_model(User)
        yield Q(reference_ids__contains=[n_2_pairing(user_ct.id, pk) for pk in pks])


EVENT_TYPE_CHOICES = (
    ('', _('All')),
    (
        _('Users'),
        (
            ('login,user.creation,user.registration,sso,password', _('Connection & SSO')),
            ('password', _('Password')),
            (
                'password,manager.user.((de)?activation|password|profile|email),^user.deletion,^user.profile',
                _('Profile changes'),
            ),
        ),
    ),
    (
        _('Backoffice'),
        (
            ('manager', _('All')),
            ('manager.user', _('User management')),
            ('manager.role', _('Role management')),
        ),
    ),
)


class JournalForm(JournalForm):
    search_engine_class = JournalSearchEngine

    event_type = forms.ChoiceField(required=False, choices=EVENT_TYPE_CHOICES)

    def clean_event_type(self):
        patterns = self.cleaned_data['event_type'].split(',')
        qs_filter = functools.reduce(Q.__or__, (Q(name__regex=pattern) for pattern in patterns))
        self.cleaned_data['_event_type'] = EventType.objects.filter(qs_filter)
        return self.cleaned_data['event_type']

    def get_queryset(self, **kwargs):
        qs = super().get_queryset(**kwargs)

        event_type = self.cleaned_data.get('_event_type')
        if event_type:
            qs = qs.filter(type__in=event_type)

        return qs

    def prefetcher(self, model, pks):
        if not issubclass(model, User):
            return
        for deleted_user in DeletedUser.objects.filter(old_user_id__in=pks):
            yield deleted_user.old_user_id, deleted_user


class BaseJournalView(views.TitleMixin, views.MediaMixin, views.MultipleOUMixin, JournalView):
    template_name = 'authentic2/manager/journal.html'
    title = _('Journal')
    form_class = JournalForm

    def get_context_data(self, **kwargs):
        ctx = super().get_context_data(**kwargs)
        date_hierarchy = ctx['date_hierarchy']
        if date_hierarchy.title:
            ctx['title'] = _('Journal of %s') % date_hierarchy.title
        return ctx


class GlobalJournalView(views.PermissionMixin, BaseJournalView):
    template_name = 'authentic2/manager/journal.html'
    permissions_global = True
    permissions = ['custom_user.view_user', 'a2_rbac.view_role']


journal = GlobalJournalView.as_view()
