# Path: redditpoller/models.py
# class WatchedUser(models.Model):
#     user = models.ForeignKey(User)
#     watched_username = models.CharField(max_length=30)
#     submitted_date = models.DateTimeField(default=timezone.now)
#     last_checked_date = models.DateTimeField(default=timezone.now)
#     watch_interval = models.IntegerField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.watched_username
# 
# class WatchedUserComment(models.Model):
#     parent_user = models.ForeignKey(WatchedUser, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# class WatchedUserThread(models.Model):
#     parent_user = models.ForeignKey(WatchedUser, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
# Path: redditpoller/poller.py
# def get_user_threads(username, fetch_amount):
#     threads = r.redditor(username).submissions.new(limit=fetch_amount)
#     return threads
# 
# def get_user_comments(username, fetch_amount):
#     comments = r.redditor(username).comments.new(limit=fetch_amount)
#     return comments
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedUser, WatchedUserComment, WatchedUserThread
from redditpoller.poller import get_user_threads, get_user_comments, notify_user
from datetime import timedelta


class Command(BaseCommand):
    help = "Checks watched users and notifies users of updates"

    def handle(self, *args, **options):
        watched_users = WatchedUser.objects.all()
        user_count = watched_users.count()
        notifications_sent = 0
        for user in watched_users:
            current_time = timezone.now()
            if current_time - timedelta(hours=user.watch_interval) < user.last_checked_date:
                pass
            else:
                user.last_checked_date = current_time
                user.save()

# Path: redditpoller/models.py
# class WatchedUser(models.Model):
#     user = models.ForeignKey(User)
#     watched_username = models.CharField(max_length=30)
#     submitted_date = models.DateTimeField(default=timezone.now)
#     last_checked_date = models.DateTimeField(default=timezone.now)
#     watch_interval = models.IntegerField(blank=True, null=True)
# 
#     def __str__(self):
#         return self.watched_username
# 
# class WatchedUserComment(models.Model):
#     parent_user = models.ForeignKey(WatchedUser, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# class WatchedUserThread(models.Model):
#     parent_user = models.ForeignKey(WatchedUser, on_delete=models.CASCADE)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
# 
#     def __str__(self):
#         return self.title
# 
# Path: redditpoller/poller.py
# def get_user_threads(username, fetch_amount):
#     threads = r.redditor(username).submissions.new(limit=fetch_amount)
#     return threads
# 
# def get_user_comments(username, fetch_amount):
#     comments = r.redditor(username).comments.new(limit=fetch_amount)
#     return comments
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


# Path: redditpoller/management/commands/check_watched_users.py
from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedUser, WatchedUserComment, WatchedUserThread
from redditpoller.poller import get_user_threads, get_user_comments, notify_user
from datetime import timedelta



class Command(BaseCommand):
    help = "Checks watched users and notifies users of updates"

    def handle(self, *args, **options):
        watched_users = WatchedUser.objects.all()
        user_count = watched_users.count()
        notifications_sent = 0
        for user in watched_users:
            current_time = timezone.now()
            if current_time - timedelta(hours=user.watch_interval) < user.last_checked_date:
                pass
            else:
                user.last_checked_date = current_time
                user.save()

                new_threads = get_user_threads(user.watched_username, 10)