from django.utils.translation import ugettext as _
from django.utils.translation import ugettext_lazy

from memoized import memoized

from corehq.apps.reports.datatables import DataTablesColumn, DataTablesHeader
from corehq.apps.reports.dispatcher import UserManagementReportDispatcher
from corehq.apps.reports.generic import GenericTabularReport, GetParamsMixin
from corehq.apps.reports.standard import DatespanMixin, ProjectReport
from corehq.apps.reports.util import datespan_from_beginning
from corehq.apps.users.models import UserHistory


class UserHistoryReport(GetParamsMixin, DatespanMixin, GenericTabularReport, ProjectReport):
    slug = 'user_history'
    name = ugettext_lazy("User History")
    section_name = ugettext_lazy("User Management")

    dispatcher = UserManagementReportDispatcher

    # ToDo: Add pending filters
    fields = [
        'corehq.apps.reports.filters.users.ExpandedMobileWorkerFilter',
        'corehq.apps.reports.filters.dates.DatespanFilter',
    ]

    description = ugettext_lazy("History of user updates")
    ajax_pagination = True

    sortable = False

    @property
    def default_datespan(self):
        return datespan_from_beginning(self.domain_object, self.timezone)

    @property
    def headers(self):
        # ToDo: Add headers
        h = [
            DataTablesColumn(_("User")),
        ]

        return DataTablesHeader(*h)

    @property
    def total_records(self):
        return self._get_queryset().count()

    @memoized
    def _get_queryset(self):
        # ToDo: add query based on params
        return UserHistory.objects.none()

    @property
    def rows(self):
        records = self._get_queryset().order_by('-changed_at')[
            self.pagination.start:self.pagination.start + self.pagination.count
        ]
        for record in records:
            yield _user_history_row(record)


def _user_history_row(record):
    # ToDo: add render for each row
    return []

@@ -1,28 +1,41 @@
+import json%0A%0A
 from django.utils.translatio
@@ -532,24 +532,35 @@
 odels import
+ CouchUser,
  UserHistory
@@ -560,16 +560,119 @@
 History%0A
+from corehq.const import USER_DATETIME_FORMAT%0Afrom corehq.util.timezones.conversions import ServerTime%0A
 %0A%0Aclass 
@@ -1384,36 +1384,8 @@
 f):%0A
-        # ToDo: Add headers%0A
     
@@ -1435,16 +1435,284 @@
 ser%22)),%0A
+            DataTablesColumn(_(%22By User%22)),%0A            DataTablesColumn(_(%22Action%22)),%0A            DataTablesColumn(_(%22Via%22)),%0A            DataTablesColumn(_(%22Change Message%22)),%0A            DataTablesColumn(_(%22Changes%22)),%0A            DataTablesColumn(_(%22Timestamp%22)),%0A
         
@@ -1963,13 +1963,33 @@
 cts.
-none(
+filter(domain=self.domain
 )%0A%0A 
@@ -2291,39 +2291,649 @@
     
-# ToDo: add render for each row
+return %5B%0A        CouchUser.get_by_user_id(record.user_id).username,  # ToDo: re-think this fetch%0A        CouchUser.get_by_user_id(record.changed_by).username,%0A        _get_action_display(record.action),%0A        record.details%5B'changed_via'%5D,%0A        record.message,%0A        json.dumps(record.details%5B'changes'%5D),%0A        ServerTime(record.changed_at).ui_string(USER_DATETIME_FORMAT),%0A    %5D%0A%0A%0Adef _get_action_display(logged_action):%0A    action = ugettext_lazy(%22Updated%22)%0A    if logged_action == UserHistory.CREATE:%0A        action = ugettext_lazy(%22Added%22)%0A    elif logged_action == UserHistory.DELETE:%0A        action = ugettext_lazy(%22Deleted%22)
 %0A   
@@ -2944,7 +2944,11 @@
 urn 
-%5B%5D
+action
 %0A

add rows display