from __future__ import absolute_import, unicode_literals

import logging

from celery import shared_task

from company.models.company import Company
from ncircc.enums.notifications import NotificationStatusEnum
from ncircc.models.notification import Notification
from ncircc.services.comments import CommentsUpdaterService
from ncircc.services.notification import NotificationGetterUpdatedStatusServices


_log = logging.getLogger(__name__)


@shared_task()
def update_status_notification() -> None:
    if not Company.objects.exists():
        _log.debug(f'Task update. Not Company.')
        return
    notification_list = Notification.objects.exclude(notification_status=NotificationStatusEnum.ARCHIVED.value)
    for notification in notification_list:
        sender = NotificationGetterUpdatedStatusServices(notification.pk)
        msg, status = sender.send()
        if status != 200:
            _log.error(f'Updating notification status from ncircc failed: {msg}')


@shared_task()
def update_comments() -> None:
    if not Company.objects.exists():
        _log.debug(f'Task update. Not Company.')
        return
    updated = CommentsUpdaterService()
    for index in range(5):
        msg, status = updated.send()
        if status == 200:
            _log.info('Task update comments success.')
            break
        else:
            _log.error(f'Task update comments not success: {msg}. Retry count {index}')
