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)

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

    # Update and Delete are available for super admin (Django Admin)
    # and to the user that signed up (uid token)
    @skip("NotImplemented")
    def test_update_signup_token(self):
        pass

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