from django.db import models
from django_bleach.models import BleachField
from django.core.validators import MaxLengthValidator
from django.utils.translation import gettext as _
from django.utils import timezone

from redditors.models import User
from subs.models import Sub


# class LoanrequestVotesManager(models.Manager):
#     def get_queryset(self):
#         return super().get_queryset().prefetch_related('votes')


class Loanrequest(models.Model):
    # This is the default because it is not very common to
    # not need to calculate upvotes and that will result in
    # a lot of db hits if we don't prefetch the votes
    # objects = LoanrequestVotesManager()

    # objects_no_votes = models.Manager()

    # set default so that management command can overwrite
    created = models.DateTimeField(default=timezone.now)
    updated = models.DateTimeField(auto_now=True)

    title = BleachField(
        validators=[MaxLengthValidator(
            150,
            message=_("The title can only be 150 characters in length.")
        )]
    )
    loanamount = models.IntegerField(blank=False)

    # loanamount = BleachField(default=240,
    #                          blank=False,
    #     validators=[MaxLengthValidator(
    #         10,
    #         message=_("The number can only be 10 characters in length or less.")
    #     )]
    # )

    body = BleachField(blank=True)

    subreddit = models.ForeignKey(
        Sub,
        on_delete=models.CASCADE,
        related_name="loanrequests"
    )
    authorsender = models.ForeignKey(
        User,
        on_delete=models.SET_NULL,
        null=True,
        related_name="loanrequests"
    )

    # voters = models.ManyToManyField(
    #     User,
    #     through='votes.LoanrequestVote',
    #     related_name='voted_loanrequests',
    # )

    # Reverse FK to Comment related_name="comments"

    # @property
    # def upvotes(self):
    #     """
    #     Just add up the prefetched votes vote_types.
    #     """
    #     return sum([vote.vote_type for vote in self.votes.all()])

    def __str__(self):
        return str(self.title)
