# Path: casepro/statistics/views.py
# class CasesClosedPerMonthChart(BasePerMonthChart):
#     """
#     Chart of cases closed per month for the current org
#     """
# 
#     def get_month_totals(self, request, since):
#         return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_CASE_CLOSED, since).month_totals()
# 
# class CasesOpenedPerMonthChart(BasePerMonthChart):
#     """
#     Chart of cases opened per month for the current org
#     """
# 
#     def get_month_totals(self, request, since):
#         return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_CASE_OPENED, since).month_totals()
# 
# class DailyCountExportCRUDL(SmartCRUDL):
#     model = DailyCountExport
#     actions = ("create", "read")
# 
#     class Create(NonAtomicMixin, OrgPermsMixin, SmartCreateView):
#         def post(self, request, *args, **kwargs):
#             of_type = request.json["type"]
# 
#             # parse dates and adjust max so it's exclusive
#             after = parse_iso8601(request.json["after"]).date()
#             before = parse_iso8601(request.json["before"]).date() + timedelta(days=1)
# 
#             export = DailyCountExport.create(self.request.org, self.request.user, of_type, after, before)
# 
#             daily_count_export.delay(export.pk)
# 
#             return JsonResponse({"export_id": export.pk})
# 
#     class Read(BaseDownloadView):
#         title = _("Download Export")
#         filename = "daily_count_export.xls"
# 
# class IncomingPerDayChart(BasePerDayChart):
#     """
#     Chart of incoming per day for either the current org or a given label
#     """
# 
#     def get_day_totals(self, request, since):
#         label_id = request.GET.get("label")
# 
#         if label_id:
#             label = Label.get_all(org=request.org).get(pk=label_id)
#             return DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING, since).day_totals()
#         else:
#             return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_INCOMING, since).day_totals()
# 
# class MostUsedLabelsChart(BaseChart):
#     """
#     Pie chart of top 10 labels used in last 30 days
#     """
# 
#     num_items = 10
#     num_days = 30
# 
#     def get_data(self, request):
#         since = timezone.now() - relativedelta(days=self.num_days)
#         labels = Label.get_all(request.org, request.user)
# 
#         counts_by_label = DailyCount.get_by_label(labels, DailyCount.TYPE_INCOMING, since).scope_totals()
# 
#         # sort by highest count DESC, label name ASC
#         by_usage = sorted(counts_by_label.items(), key=lambda c: (-c[1], c[0].name))
#         by_usage = [c for c in by_usage if c[1]]  # remove zero counts
# 
#         if len(by_usage) > self.num_items:
#             label_zones = by_usage[: self.num_items - 1]
#             others = by_usage[self.num_items - 1 :]
#         else:
#             label_zones = by_usage
#             others = []
# 
#         series = [{"name": l[0].name, "y": l[1]} for l in label_zones]
# 
#         # if there are remaining items, merge into single "Other" zone
#         if others:
#             series.append({"name": str(_("Other")), "y": sum([o[1] for o in others])})
# 
#         return {"series": series}
# 
# class RepliesPerMonthChart(BasePerMonthChart):
#     """
#     Chart of replies per month for either the current org, a given partner, or a given user
#     """
# 
#     def get_month_totals(self, request, since):
#         partner_id = request.GET.get("partner")
#         user_id = request.GET.get("user")
# 
#         if partner_id:
#             partner = Partner.objects.get(org=request.org, pk=partner_id)
#             return DailyCount.get_by_partner([partner], DailyCount.TYPE_REPLIES, since).month_totals()
#         elif user_id:
#             user = request.org.get_users().get(pk=user_id)
#             return DailyCount.get_by_user(self.request.org, [user], DailyCount.TYPE_REPLIES, since).month_totals()
#         else:
#             return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_REPLIES, since).month_totals()


from django.urls import re_path
from .views import (
    CasesClosedPerMonthChart,
    CasesOpenedPerMonthChart,
    DailyCountExportCRUDL,
    IncomingPerDayChart,
    MostUsedLabelsChart,
    RepliesPerMonthChart,
)


urlpatterns = DailyCountExportCRUDL().as_urlpatterns()

urlpatterns += [
    re_path(r"^incoming_chart/$", IncomingPerDayChart.as_view(), name="statistics.incoming_chart"),
    re_path(r"^replies_chart/$", RepliesPerMonthChart.as_view(), name="statistics.replies_chart"),
    re_path(r"^labels_pie_chart/$", MostUsedLabelsChart.as_view(), name="statistics.labels_pie_chart"),

# Path: casepro/statistics/views.py
# class CasesClosedPerMonthChart(BasePerMonthChart):
#     """
#     Chart of cases closed per month for the current org
#     """
# 
#     def get_month_totals(self, request, since):
#         return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_CASE_CLOSED, since).month_totals()
# 
# class CasesOpenedPerMonthChart(BasePerMonthChart):
#     """
#     Chart of cases opened per month for the current org
#     """
# 
#     def get_month_totals(self, request, since):
#         return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_CASE_OPENED, since).month_totals()
# 
# class DailyCountExportCRUDL(SmartCRUDL):
#     model = DailyCountExport
#     actions = ("create", "read")
# 
#     class Create(NonAtomicMixin, OrgPermsMixin, SmartCreateView):
#         def post(self, request, *args, **kwargs):
#             of_type = request.json["type"]
# 
#             # parse dates and adjust max so it's exclusive
#             after = parse_iso8601(request.json["after"]).date()
#             before = parse_iso8601(request.json["before"]).date() + timedelta(days=1)
# 
#             export = DailyCountExport.create(self.request.org, self.request.user, of_type, after, before)
# 
#             daily_count_export.delay(export.pk)
# 
#             return JsonResponse({"export_id": export.pk})
# 
#     class Read(BaseDownloadView):
#         title = _("Download Export")
#         filename = "daily_count_export.xls"
# 
# class IncomingPerDayChart(BasePerDayChart):
#     """
#     Chart of incoming per day for either the current org or a given label
#     """
# 
#     def get_day_totals(self, request, since):
#         label_id = request.GET.get("label")
# 
#         if label_id:
#             label = Label.get_all(org=request.org).get(pk=label_id)
#             return DailyCount.get_by_label([label], DailyCount.TYPE_INCOMING, since).day_totals()
#         else:
#             return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_INCOMING, since).day_totals()
# 
# class MostUsedLabelsChart(BaseChart):
#     """
#     Pie chart of top 10 labels used in last 30 days
#     """
# 
#     num_items = 10
#     num_days = 30
# 
#     def get_data(self, request):
#         since = timezone.now() - relativedelta(days=self.num_days)
#         labels = Label.get_all(request.org, request.user)
# 
#         counts_by_label = DailyCount.get_by_label(labels, DailyCount.TYPE_INCOMING, since).scope_totals()
# 
#         # sort by highest count DESC, label name ASC
#         by_usage = sorted(counts_by_label.items(), key=lambda c: (-c[1], c[0].name))
#         by_usage = [c for c in by_usage if c[1]]  # remove zero counts
# 
#         if len(by_usage) > self.num_items:
#             label_zones = by_usage[: self.num_items - 1]
#             others = by_usage[self.num_items - 1 :]
#         else:
#             label_zones = by_usage
#             others = []
# 
#         series = [{"name": l[0].name, "y": l[1]} for l in label_zones]
# 
#         # if there are remaining items, merge into single "Other" zone
#         if others:
#             series.append({"name": str(_("Other")), "y": sum([o[1] for o in others])})
# 
#         return {"series": series}
# 
# class RepliesPerMonthChart(BasePerMonthChart):
#     """
#     Chart of replies per month for either the current org, a given partner, or a given user
#     """
# 
#     def get_month_totals(self, request, since):
#         partner_id = request.GET.get("partner")
#         user_id = request.GET.get("user")
# 
#         if partner_id:
#             partner = Partner.objects.get(org=request.org, pk=partner_id)
#             return DailyCount.get_by_partner([partner], DailyCount.TYPE_REPLIES, since).month_totals()
#         elif user_id:
#             user = request.org.get_users().get(pk=user_id)
#             return DailyCount.get_by_user(self.request.org, [user], DailyCount.TYPE_REPLIES, since).month_totals()
#         else:
#             return DailyCount.get_by_org([self.request.org], DailyCount.TYPE_REPLIES, since).month_totals()


# Path: casepro/statistics/urls.py
from django.urls import re_path
from .views import (
    CasesClosedPerMonthChart,
    CasesOpenedPerMonthChart,
    DailyCountExportCRUDL,
    IncomingPerDayChart,
    MostUsedLabelsChart,
    RepliesPerMonthChart,
)



urlpatterns = DailyCountExportCRUDL().as_urlpatterns()

urlpatterns += [
    re_path(r"^incoming_chart/$", IncomingPerDayChart.as_view(), name="statistics.incoming_chart"),
    re_path(r"^replies_chart/$", RepliesPerMonthChart.as_view(), name="statistics.replies_chart"),
    re_path(r"^labels_pie_chart/$", MostUsedLabelsChart.as_view(), name="statistics.labels_pie_chart"),

    re_path(r"^cases_opened_chart/$", CasesOpenedPerMonthChart.as_view(), name="statistics.cases_opened_chart"),