import traceback

import sentry_sdk
from celery_once import QueueOnce
from django.utils import timezone
from django.core.cache import cache
from djconfig import config
from telegram import Bot

from config.celery import app


@app.task(base=QueueOnce, once={'graceful': True})
def check_feeds():
    from feeds.models import Feed

    feeds = Feed.objects.all()
    enqueued = []
    for feed in feeds:
        if feed.run_check():
            enqueued.append(str(feed))
    return f'Following tasks were enqueued: {enqueued}'


@app.task()
def execute_feed(feed_pk):
    from feeds.models import Feed

    config._reload_maybe()

    feed = Feed.objects.get(pk=feed_pk)

    with sentry_sdk.configure_scope() as scope:
        scope.set_tag('feed', str(feed))
        try:
            bot = Bot(config.feed_bot_token)
            print(f'Last ID for "{feed}" = "{feed.last_id}"')
            for last_id in feed.config.execute(bot, feed.chat_id, feed.last_id):
                if last_id:
                    feed.last_id = last_id
                    feed.save()
                    print(f'Saved last ID for "{feed}" = "{feed.last_id}"')
            feed.last_check = timezone.now()
            feed.save()
        except Exception as e:
            sentry_sdk.capture_exception(e)
            traceback.print_exc()
        finally:
            if feed:
                cache.delete(feed.lock_key)
