from unittest import skip
from django.contrib.auth.models import User
from rest_framework import status
from rest_framework.test import APITestCase
from webapp.serializers import SignupSerializer
from webapp.models import Signup
from webapp.tests.signup_fixture import createSignupForm, createSignupObject, createSignupRequest, ALL_QUESTION_TYPES, ALL_QUESTION_TYPES_ANSWER

URL = "/api/signup/"


class SignupTestCase(APITestCase):

    def setUp(self):
        self.signupForm = createSignupForm()
        self.hiddenForm = createSignupForm(visible=False)

        self.signup1 = createSignupObject("1", self.signupForm, ALL_QUESTION_TYPES)
        self.signup2 = createSignupObject("2", self.signupForm, ALL_QUESTION_TYPES)

        username, password = "test_admin", "password123"
        self.authClient = User.objects.create_superuser(username, "myemail@test.com", password)

    def test_get_signups(self):
        expected = SignupSerializer(
            self.signupForm.signup_set.all(),
            many=True
        )
        # Authenticate
        self.client.force_authenticate(user=self.authClient)
        response = self.client.get(URL, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data["results"], expected.data)

    def test_get_single_signup(self):
        id = self.signup1.id
        expected = SignupSerializer(
            Signup.objects.get(id=id)
        )
        # Authenticate
        self.client.force_authenticate(user=self.authClient)
        response = self.client.get(f"{URL}{id}/", format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(response.data, expected.data)

    def test_create_signup(self):
        new = createSignupRequest("asd", self.signupForm.id, ALL_QUESTION_TYPES_ANSWER)
        response = self.client.post(URL, new, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Signup.objects.count(), 3)

    # Can signup to a hidden form
    def test_create_signup_hidden(self):
        new = createSignupRequest("asd", self.hiddenForm.id, ALL_QUESTION_TYPES_ANSWER)
        response = self.client.post(URL, new, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
        self.assertEqual(Signup.objects.count(), 3)

    def test_delete_as_admin(self):
        id = self.signup1.id
        no_auth_response = self.client.delete(f"{URL}{id}/", format="json")
        self.assertEqual(no_auth_response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.client.force_authenticate(user=self.authClient)
        response = self.client.delete(f"{URL}{id}/", format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Signup.objects.get(id=id).deleted, True)

    @skip("NotImplemented")
    def test_get_hidden_forms_admin(self):
        pass

    def test_update_signup_token(self):
        id = self.signup1.id
        uuid = self.signup1.uuid
        clone = ALL_QUESTION_TYPES_ANSWER.copy()
        clone["-naY2R1-h"] = "Edited Testi"
        new = createSignupRequest("asd", self.signupForm.id, clone)
        response = self.client.put(f"{URL}{id}/edit/?uuid={uuid}", new, format="json")
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEqual(Signup.objects.get(id=id).answer["-naY2R1-h"], "Edited Testi")

    @skip("NotImplemented")
    def test_delete_signup_token(self):
        pass

    # TODO: Use some mocking library and check that sendgrid is actually called
    def test_signupee_sendemail(self):
        form = self.signupForm
        emailURL = f"/api/signupForm/{form.id}/sendemail/"
        payload = {
            "subject": "Email subject",
            "content": "Markdown",
            "mode": "actual"
        }
        no_auth_response = self.client.post(emailURL, payload, format="json")
        self.assertEqual(no_auth_response.status_code, status.HTTP_401_UNAUTHORIZED)
        self.client.force_authenticate(user=self.authClient)
        response = self.client.post(emailURL, payload, format="json")
        self.assertEqual(response.status_code, status.HTTP_201_CREATED)
