import itertools
import logging
from datetime import datetime, timedelta

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.db.models import Q
from django.http import HttpRequest
from django.http.response import HttpResponse as HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.urls import reverse_lazy
from django.views import generic
from django_datatables_view.base_datatable_view import BaseDatatableView

from module_iam import iam_constant, permission
from module_iam.iam_constant import PRINCIPAL_TYPE_USER
from module_iam.models import IAmPrincipal
from module_project import constants, date_utils
from module_project.mixins import ActionMixin
from module_project.service import OneSignalService
from module_project.utils import JsonResponseUtil

from .forms import PushNotificationForm
from .models import PushNotification


# Create your views here.
class NotificationView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.TemplateView):
    page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
    resource = iam_constant.RESOURCE_MANAGE_NOTIFICATION
    template_name = "module_notification/notification.html"

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["page_name"] = self.page_name
        return context


class NotificationListJsonView(BaseDatatableView):
    model = PushNotification
    columns = ["id", "title", "message", "active", "timestamp"]
    order_columns = ["id", "title", "message", "active", "timestamp"]

    FILTER_ICONTAINS = "icontains"

    def get_filter_method(self):
        """Returns preferred filter method"""
        return self.FILTER_ICONTAINS

    def get_initial_queryset(self):
        deleted_flag = self.request.GET.get("deleted_flag", None)
        return self.model.objects.filter(deleted=deleted_flag)

    def ordering(self, qs):
        order = self.request.GET.get('order[0][dir]', None)
        if order:
            column_index = int(self.request.GET.get('order[0][column]', None)) - 1
            order_column = self.order_columns[column_index]

            if order == "asc":
                qs = qs.order_by(order_column)
            elif order == "desc":
                qs = qs.order_by("-" + order_column)

        return qs


class NotificationCreateOrUpdateView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.View):
    # Set the page_name and resource
    page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
    resource = iam_constant.RESOURCE_MANAGE_NOTIFICATION

    # Initialize the action as ACTION_CREATE (can change based on logic)
    action = iam_constant.ACTION_CREATE  # Default action

    template_name = "module_notification/add_notification.html"
    model = PushNotification
    form_class = PushNotificationForm
    success_url = reverse_lazy("module_notification:notification")
    error_message = "An error occurred while saving the data."

    # Determine the success message dynamically based on whether it's an update or create
    def get_success_message(self):
        self.success_message = (
            constants.RECORD_CREATED if not self.object else constants.RECORD_UPDATED
        )
        return self.success_message

    # Get the object (if exists) based on URL parameter 'pk
    def get_object(self):
        pk = self.kwargs.get("pk")
        return get_object_or_404(self.model, pk=pk) if pk else None

    # Add page_name and operation to the context
    def get_context_data(self, **kwargs):
        context = {
            "page_name": self.page_name,
            "operation": "Add" if not self.object else "Edit",
        }
        context.update(kwargs)  # Include any additional context data passed to the view
        return context

    def get(self, request, *args, **kwargs):
        self.object = self.get_object()

        # If an object is found, change action to ACTION_UPDATE
        if self.object is not None:
            self.action = iam_constant.ACTION_UPDATE

        form = self.form_class(instance=self.object)
        context = self.get_context_data(form=form)
        return render(request, self.template_name, context=context)

    def post(self, request, *args, **kwargs):
        print("Request data: ", request.POST)
        self.object = self.get_object()

        # If an object is found, change action to ACTION_UPDATE
        if self.object is not None:
            self.action = iam_constant.ACTION_UPDATE

        form = self.form_class(request.POST, instance=self.object)
        if not form.is_valid():
            print(form.errors)
            context = self.get_context_data(form=form)
            return render(request, self.template_name, context=context)

        form.save()
        messages.success(self.request, self.get_success_message())
        return redirect(self.success_url)


class NotificationActionView(ActionMixin):
    model = PushNotification

class NotificationArchiveView(permission.ResourcePermissionRequiredMixin, LoginRequiredMixin, generic.TemplateView):
    page_name = iam_constant.RESOURCE_MANAGE_NOTIFICATION
    resource = iam_constant.RESOURCE_MANAGE_NOTIFICATION
    action = None
    template_name = "module_notification/notification_archive.html"
    model = PushNotification

    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context["page_name"] = self.page_name
        return context

class NotificationSendView(generic.View):
    model = PushNotification

    def get_image_url(self, obj, field_name, request):
        image_field = getattr(obj, field_name)
        if image_field:
            return request.build_absolute_uri(image_field.url)
        return ""

    def post(self, request, *args, **kwargs):
        id = request.POST.get("id")
        obj = self.model.objects.filter(pk=int(id)).first()

        if not obj:
            return JsonResponseUtil.error(
                message="No notification with such ID exists."
            )
        
        if not obj.active:
            return JsonResponseUtil.error(
                message="The notification cannot be sent because it is inactive. Please activate the notification before attempting to send it."
            )

        # Get the current date and subtract 15 days
        fifteen_days_ago = datetime.now() - timedelta(days=3)

        # Filter the IAmPrincipal objects based on the last_login field being greater than or equal to fifteen_days_ago
        player_ids = list(
            IAmPrincipal.objects.filter(
                principal_type__name=PRINCIPAL_TYPE_USER,
                last_login__gte=fifteen_days_ago,
            ).values_list("player_id", flat=True)
        )
        # removing none from list
        player_ids = list(itertools.filterfalse(lambda x: x is None, player_ids))

        print(f"player id is {player_ids}")

        try:
            notification = OneSignalService()
            response = notification.send_notification(
                headings=obj.title,
                contents=obj.message,
                include_player_ids=player_ids,
            )
        except Exception as e:
            error_response = {
                "status": 400,
                "message": constants.INTERNAL_SERVER_ERROR,
                "errors": str(e),
            }
            return JsonResponseUtil.error(**error_response)

        return JsonResponseUtil.success(message="success")


