from http.cookies import SimpleCookie

from django.test import TestCase
from rest_framework.test import APITestCase
from rest_framework import status

from account.models import User
from notification.models import Subscriber


class BaseTestCase(APITestCase):

    def setUp(self):
        self.username = 'sedragurda'
        self.password = 'sedragurdaredips19'
        self.email = 'sedragurda@desoz.com'
        self.user = User.objects.create_user(username=self.username, email=self.email, password=self.password)
        # get tokens
        tokens = User.create_jwt_tokens(self.user)
        self.client.cookies = SimpleCookie({'access_token': tokens.get('access_token'),
                                            'refresh_token': tokens.get('refresh_token')})


class NotificationAnonSubscribeTestCase(APITestCase):

    def test_subscribe(self):

        test_data = {
            "email": "test@email.com",
            "state": 1
        }

        response = self.client.post("/api/web/notifications/subscribe/", data=test_data, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()["email"], test_data["email"])
        self.assertEqual(response.json()["state"], test_data["state"])


class NotificationSubscribeTestCase(BaseTestCase):

    def setUp(self):
        super().setUp()

        self.test_data = {
            "email": self.email,
            "state": 1
        }

    def test_subscribe(self):

        response = self.client.post("/api/web/notifications/subscribe/", data=self.test_data, format="json")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.json()["email"], self.email)
        self.assertEqual(response.json()["state"], self.test_data["state"])

    def test_subscribe_info_auth_user(self):

        Subscriber.objects.create(user=self.user, email=self.email, state=1)

        response = self.client.get("/api/web/notifications/subscribe-info/", data=self.test_data, format="json")

        self.assertEqual(response.status_code, status.HTTP_200_OK)


class NotificationSubscribeInfoTestCase(APITestCase):

    def test_subscribe_info(self):

        self.username = 'sedragurda'
        self.password = 'sedragurdaredips19'
        self.email = 'sedragurda@desoz.com'
        self.user = User.objects.create_user(username=self.username, email=self.email, password=self.password)

        test_subscriber = Subscriber.objects.create(user=self.user, email=self.email, state=1)

        response = self.client.get(f"/api/web/notifications/subscribe-info/{test_subscriber.update_code}/")

        self.assertEqual(response.status_code, status.HTTP_200_OK)


class NotificationUnsubscribeAuthUserTestCase(BaseTestCase):

    def test_unsubscribe_auth_user(self):

        Subscriber.objects.create(user=self.user, email=self.email, state=1)

        self.test_data = {
            "email": self.email,
            "state": 1
        }

        response = self.client.patch("/api/web/notifications/unsubscribe/", data=self.test_data, format="json")

        self.assertEqual(response.status_code, status.HTTP_200_OK)


class NotificationUnsubscribeTestCase(APITestCase):

    def test_unsubscribe(self):
        self.username = 'sedragurda'
        self.password = 'sedragurdaredips19'
        self.email = 'sedragurda@desoz.com'
        self.user = User.objects.create_user(username=self.username, email=self.email, password=self.password)

        self.test_data = {
            "email": self.email,
            "state": 1
        }

        test_subscriber = Subscriber.objects.create(user=self.user, email=self.email, state=1)

        response = self.client.patch(f"/api/web/notifications/unsubscribe/{test_subscriber.update_code}/",
                                     data=self.test_data, format="json")

        self.assertEqual(response.status_code, status.HTTP_200_OK)
