import uuid
from unittest.mock import patch

import pytest
from django.urls import reverse
from rest_framework.test import APIRequestFactory, force_authenticate

from company.models.company import Company
from incident.models import Incident
from ncircc.enums.notifications import EventTypeEnum
from ncircc.models.comments import Comment
from ncircc.models.notification import Notification
from ncircc.tests.utils import mock_comments_request_post_status_200
from ncircc.views.notification_api import NotificationApiViewSet


@pytest.mark.django_db
class TestCommentApi:
    """Test Comments APi"""

    @pytest.fixture(autouse=True)
    def setup_test(self, add_user_with_permissions) -> None:
        self.company = Company.objects.create(name='Test_name_1', city='Moscow')
        self.user = add_user_with_permissions(username='test_user', password='TesTPasWord1', is_superuser=True)
        self.incident = Incident.objects.create(title='test_inc_1', importance=10, event_count=10, events='')
        self.notification = Notification.objects.create(
            incident=self.incident,
            affected_system_name='affected_system_name_test',
            event_description='event_description_test',
            type=EventTypeEnum.INVOLVING_INTO_MALWARE_INFRASTRUCTURE.value,
            uuid=uuid.uuid4()
        )

    @pytest.mark.unit
    @patch('requests.post', side_effect=mock_comments_request_post_status_200)
    def test_create_api_valid_data(self, *args):
        count_comment_before = Comment.objects.count()
        assert not count_comment_before
        data = {
            'text': 'Test text',
        }
        url = reverse('ncircc_notification-comments', args=[self.notification.pk])
        request = APIRequestFactory().post(url, data=data)
        view = NotificationApiViewSet.as_view({'post': 'comments_create'})
        force_authenticate(request, self.user)
        response = view(request, pk=self.notification.pk)
        count_comment_after = Comment.objects.count()
        assert count_comment_after == 1
        assert response.status_code == 201
        assert 'id' in response.data
        assert 'text' in response.data
        assert 'create_time' in response.data
        assert 'login' in response.data
        assert 'notification' in response.data
        assert 'id_in_ncircc' in response.data
        assert 'from_console' in response.data
        assert 'is_read' in response.data

    @pytest.mark.unit
    def test_not_valid_data_create(self):
        count_comment_before = Comment.objects.count()
        assert not count_comment_before
        data = {}
        url = reverse('ncircc_notification-comments', args=[self.notification.pk])
        request = APIRequestFactory().post(url, data=data)
        view = NotificationApiViewSet.as_view({'post': 'comments_create'})
        force_authenticate(request, self.user)
        response = view(request, self.notification.pk)
        count_comment_after = Comment.objects.count()
        assert not count_comment_after
        assert response.status_code == 400

    @pytest.mark.unit
    def test_comment_list_status(self):
        url = reverse('ncircc_notification-comments', args=[self.notification.pk])
        request = APIRequestFactory().get(url)
        view = NotificationApiViewSet.as_view({'get': 'comments'})
        force_authenticate(request, self.user)
        response = view(request, self.notification.pk)
        assert response.status_code == 200

    @pytest.mark.unit
    def test_404_with_get_comment_list(self):
        url = reverse('ncircc_notification-comments', args=[999999])
        request = APIRequestFactory().get(url)
        view = NotificationApiViewSet.as_view({'get': 'comments'})
        force_authenticate(request, self.user)
        response = view(request, 0)
        assert response.status_code == 404

    @pytest.mark.unit
    def test_404_with_post_comment(self):
        url = reverse('ncircc_notification-comments', args=[0])
        request = APIRequestFactory().post(url)
        view = NotificationApiViewSet.as_view({'post': 'comments_create'})
        force_authenticate(request, self.user)
        response = view(request, 0)
        assert response.status_code == 404
