# Path: redditpoller/models.py
# class WatchedThread(models.Model):
#     user = models.ForeignKey(User)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
#     archived = models.BooleanField(default=False)
#     self_post = models.BooleanField(default=False)
#     self_text = models.CharField(max_length=10000, null=True, blank=True)
#     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.title
# 
# class WatchedComment(models.Model):
#     parent_thread = models.ForeignKey(WatchedThread, on_delete=models.CASCADE)
#     comment_author = models.CharField(max_length=100, null=True, blank=True)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# Path: redditpoller/poller.py
# def get_reddit_thread(reddit_url):
#     thread = r.submission(url=reddit_url)
#     return thread
# 
# def get_reddit_comments_from_thread(thread):
#     thread.comments.replace_more(limit=0)
#     thread_comments = thread.comments.list()
#     return thread_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 WatchedThread, WatchedComment
from redditpoller.poller import get_reddit_thread, get_reddit_comments_from_thread, notify_user
from datetime import timedelta


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

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

# Path: redditpoller/models.py
# class WatchedThread(models.Model):
#     user = models.ForeignKey(User)
#     url = models.CharField(max_length=500)
#     title = models.CharField(max_length=500)
#     thread_date = models.CharField(max_length=50)
#     archived = models.BooleanField(default=False)
#     self_post = models.BooleanField(default=False)
#     self_text = models.CharField(max_length=10000, null=True, blank=True)
#     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.title
# 
# class WatchedComment(models.Model):
#     parent_thread = models.ForeignKey(WatchedThread, on_delete=models.CASCADE)
#     comment_author = models.CharField(max_length=100, null=True, blank=True)
#     url = models.CharField(max_length=500)
#     text = models.CharField(max_length=10000)
# 
#     def __str__(self):
#         return self.url
# 
# Path: redditpoller/poller.py
# def get_reddit_thread(reddit_url):
#     thread = r.submission(url=reddit_url)
#     return thread
# 
# def get_reddit_comments_from_thread(thread):
#     thread.comments.replace_more(limit=0)
#     thread_comments = thread.comments.list()
#     return thread_comments
# 
# def notify_user(redditor, message):
#     r.redditor(redditor).message('Reddit Watcher', message)


# Path: redditpoller/management/commands/check_watched_threads.py
from django.core.management import BaseCommand
from django.utils import timezone
from redditpoller.models import WatchedThread, WatchedComment
from redditpoller.poller import get_reddit_thread, get_reddit_comments_from_thread, notify_user
from datetime import timedelta



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

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

                praw_thread = get_reddit_thread('https://reddit.com' + thread.url)