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

from datetime import timedelta


class PublishedManager(Manager):
    def published(self, public=True):
        if public:
            qs = self.get_queryset().filter(status="20")
        else:
            qs = self.get_queryset()
        return qs


class PostManager(PublishedManager, Manager):
    def get_queryset(self):
        return super().get_queryset().order_by("-public_date")

    def date_sorted_list(self, public=True):
        post_list = []
        for post in self.published(public):
            if post.post_type != "N":
                post_list.append((post, post.event_start.date()))
            else:
                post_list.append((post, post.public_date))

        result = sorted(post_list, key=lambda x: x[1], reverse=True)
        posts = [x[0] for x in result]

        return posts

    def date_filtered_list(
        self,
        public=True,
        year=None,
        month=None,
        fet_meeting_only=None,
    ):
        post_list = []
        qs = self.published(public)

        if not fet_meeting_only:
            posts = qs.filter(~Q(post_type="N"))
        else:
            posts = qs.filter(post_type="F")

        if year:
            posts = posts.filter(event_start__year=year)
            if month:
                posts = posts.filter(event_start__month=month)

        for post in posts:
            post_list.append((post, post.event_start.date()))

        if not fet_meeting_only:
            posts = qs.filter(post_type="N")

            if year:
                posts = posts.filter(public_date__year=year)
                if month:
                    posts = posts.filter(public_date__month=month)

            for post in posts:
                post_list.append((post, post.public_date))

        result = sorted(post_list, key=lambda x: x[1], reverse=True)
        posts = [x[0] for x in result]

        return posts

    # 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="E") | Q(post_type="N"))
        return qs.order_by("-public_date")

    def date_sorted_list(self, public=True):
        post_list = []
        for post in self.published(public):
            if post.post_type != "N":
                post_list.append((post, post.event_start.date()))
            else:
                post_list.append((post, post.public_date))

        result = sorted(post_list, key=lambda x: x[1], reverse=True)
        posts = [x[0] for x in result]

        return posts

    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):
        return super().get_queryset().filter(post_type="N").order_by("-public_date")


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

    def get_queryset(self):
        return (
            super()
            .get_queryset()
            .filter(Q(post_type="E") | Q(post_type="F"))
            .order_by("-event_start")
        )

    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):
        return super().get_queryset().filter(post_type="E").order_by("-event_start")

    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):
        return super().get_queryset().filter(post_type="F").order_by("-event_start")

    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
