import http
from datetime import datetime, timedelta

import pytest
from django.utils import timezone

from incident.models import Incident
from perms.models import Perm


@pytest.mark.django_db
class TestIncidentsList:

    @pytest.fixture
    def now(self):
        return datetime.now(tz=timezone.utc)

    @pytest.fixture(autouse=True)
    def incidents(self, now):
        Incident.objects.bulk_create([
            Incident(timestamp=now, title='Test1', importance=50, event_count=10, events=''),
            Incident(timestamp=now - timedelta(hours=1),
                     title='Test2',
                     importance=20,
                     event_count=10,
                     events=''),
            Incident(timestamp=now - timedelta(days=1),
                     title='Test3',
                     importance=10,
                     event_count=10,
                     events=''),
        ])

    @pytest.fixture(autouse=True)
    def setup_tests(self, client, django_user_model, test_server):
        # User who can see incidents list
        admin = django_user_model.objects.create_superuser(username="root", password="password")
        user = django_user_model.objects.create_user(username='pro100ton', password='ponala61')
        user.user_permissions.add(Perm.get_rights(Perm.can_view_incidents))
        user.user_permissions.add(Perm.get_rights(Perm.can_work_with_incidents))
        user.user_permissions.add(Perm.get_rights(Perm.can_view_incidents_list))

        # User who can't see incidents list
        django_user_model.objects.create_user(username='pro100ton2', password='ponala61')

        # User who can work with incidents
        user = django_user_model.objects.create_user(username="pro100ton3", password="ponala61")
        user.user_permissions.add(Perm.get_rights(Perm.can_view_incidents))
        user.user_permissions.add(Perm.get_rights(Perm.can_work_with_incidents))
        user.user_permissions.add(Perm.get_rights(Perm.can_view_incidents_list))

    # TODO: fix this
    @pytest.mark.skip('research')
    @pytest.mark.unit
    def test_export_incidents_in_csv(self, add_user_with_permissions, get_url, test_server, client):
        username = 'user'
        password = 'pro100ton'
        user = add_user_with_permissions(username=username, password=password,
                                         permissions=[Perm.can_view_incidents_list, Perm.can_export_incidents_list])
        client.force_login(user)
        response = client.get(get_url('incident-csv-export'))
        assert response.status_code == http.HTTPStatus.OK

    @pytest.mark.unit
    def test_incidents_user_friendly_id_is_different(self):
        """ Test for checking if incidents user_friendly statuses differs from each other
            PASSED if ID's are different
            FAILED otherwise
        """
        inc_1 = Incident.objects.create(title='inc_1', importance=10, event_count=10, events='')
        inc_2 = Incident.objects.create(title='inc_2', importance=10, event_count=10, events='')
        assert inc_1.user_friendly_id != inc_2.user_friendly_id

    @pytest.mark.unit
    def test_incidents_user_friendly_id_is_different_after_deletion(self, client):
        """ Test for checking if incidents user_friendly statuses differs from each other with the deletion of other incs
            PASSED if ID's are different
            FAILED otherwise
        """
        inc_1 = Incident.objects.create(title='inc_1', importance=10, event_count=10, events='')
        inc_1.delete()
        inc_2 = Incident.objects.create(title='inc_2', importance=10, event_count=10, events='')
        assert inc_1.user_friendly_id != inc_2.user_friendly_id
