from django.db import models
from django.db.models import Case, Manager, Q, When
from django.utils import timezone

from datetime import timedelta


class PublishedManager(Manager):
    def published(self, public=True):
        """
        publish all posts with status 'PUBLIC'
        """
        if public:
            qs = self.get_queryset().filter(status="20")
        else:
            qs = self.get_queryset()
        return qs

    def published_all(self, public=True):
        """
        publish all posts with status 'PUBLIC' and 'ONLY_INTERN'
        """
        if public:
            qs = self.get_queryset().filter(~Q(status="10"))
        else:
            qs = self.get_queryset()
        return qs


class PostManager(PublishedManager, Manager):
    def get_queryset(self):
        qs = super().get_queryset()
        qs = qs.annotate(
            date=Case(
                When(post_type="N", then="public_date"),
                When(post_type="E", then="event_start__date"),
                When(post_type="F", then="event_start__date"),
            )
        )
        return qs.order_by("-date")

    def date_sorted_list(self, public=True):
        return self.published(public)

    def date_filtered_list(
        self,
        public=True,
        year=None,
        month=None,
        fet_meeting_only=None,
    ):
        qs_filter = Q()

        if fet_meeting_only:
            qs_filter &= Q(post_type="F")

        if year:
            qs_filter &= Q(date__year=year)
            if month:
                qs_filter &= Q(date__month=month)

        return self.published(public).filter(qs_filter)

    # use for finding tags at homepage - TODO: delete when new design published
    def get_last_months_posts(self, public=True):
        date_today = timezone.now().date()
        return self.published(public).filter(
            public_date__gt=date_today - timedelta(days=365)
        )


class ArticleManager(PublishedManager, Manager):
    """
    Provide a query set only for "Article"
    regular fet meetings should not be contained in the news stream
    """

    def get_queryset(self):
        qs = super().get_queryset().filter(Q(post_type="N") | Q(post_type="E"))
        qs = qs.annotate(
            date=Case(
                When(post_type="N", then="public_date"),
                When(post_type="E", then="event_start__date"),
            )
        )
        return qs.order_by("-date")

    def date_sorted_list(self, public=True):
        return self.published(public)

    def pinned(self, public=True):
        return self.published(public).filter(is_pinned=True).first()


class NewsManager(PublishedManager, Manager):
    """
    Provide a query set only for "News"
    """

    def get_queryset(self):
        qs = super().get_queryset().filter(post_type="N")
        qs = qs.annotate(
            date=Case(
                When(post_type="N", then="public_date"),
            )
        )
        return qs.order_by("-date")


class AllEventManager(PublishedManager, Manager):
    """
    Provide a query set for all events ("Event" and "Fet Meeting")
    """

    def get_queryset(self):
        qs = super().get_queryset().filter(Q(post_type="E") | Q(post_type="F"))
        qs = qs.annotate(
            date=Case(
                When(post_type="E", then="event_start__date"),
                When(post_type="F", then="event_start__date"),
            )
        )
        return qs.order_by("-date")

    def future_events(self, public=True):
        date_today = timezone.now()
        qs = self.published(public).filter(event_start__gt=date_today)
        return qs.reverse()


class EventManager(PublishedManager, Manager):
    """
    Provide a query set only for "Events"
    regular fet meetings should not be contained in the news stream
    """

    def get_queryset(self):
        qs = super().get_queryset().filter(post_type="E")
        qs = qs.annotate(
            date=Case(
                When(post_type="E", then="event_start__date"),
            )
        )
        return qs.order_by("-date")

    def future_events(self, public=True):
        date_today = timezone.now()
        qs = self.published(public).filter(event_start__gt=date_today)
        return qs.reverse()

    def past_events(self, public=True):
        date_today = timezone.now()
        qs = self.published(public).filter(event_start__lt=date_today)
        return qs


class FetMeetingManager(PublishedManager, Manager):
    """
    Provide a query set only for "Fet Meeting"
    """

    def get_queryset(self):
        qs = super().get_queryset().filter(post_type="F")
        qs = qs.annotate(
            date=Case(
                When(post_type="F", then="event_start__date"),
            )
        )
        return qs.order_by("-date")

    def future_events(self):
        date_today = timezone.now()
        qs = self.published().filter(event_start__gt=date_today)
        return qs.reverse()

    def past_events(self):
        date_today = timezone.now()
        qs = self.published().filter(event_start__lt=date_today)
        return qs
