import pytest

from incident.models import Incident
from ncircc.enums.notifications import EventTypeEnum
from ncircc.models.notification import Notification
from rotation.enums import RotationType
from rotation.models import IncidentRotationSettings
from rotation.tasks import _rotate_incidents


@pytest.mark.django_db
class TestRotate:

    @pytest.mark.unit
    def test_rotate_table_for_incident(self, add_user_with_permissions):
        """Testing rotation of Incident table by time type."""
        add_user_with_permissions(username='test_admin', password='pwdadmin', is_superuser=True)
        incidents = [Incident(title=f'title_{status}', importance=10, event_count=10, events='', status=status)
                     for status in Incident.Status]
        Incident.objects.bulk_create(incidents)
        incident_with_notification = Incident.objects.create(title='test_inc_123', importance=10, event_count=10, events='',
                                                             status=Incident.Status.RESOLVED)
        notification = Notification.objects.create(
            incident=incident_with_notification,
            affected_system_name='affected_system_name_test',
            event_description='event_description_test',
            type=EventTypeEnum.INVOLVING_INTO_MALWARE_INFRASTRUCTURE.value,
        )
        rotate_settings = IncidentRotationSettings.get_solo()
        rotate_settings.rotation_type = RotationType.TIME.value
        rotate_settings.save()
        rotate_settings.clear_cache()
        all_count = Incident.objects.count()
        _rotate_incidents()

        after_rotate_count = Incident.objects.count()
        assert (all_count - after_rotate_count) == 2
