from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

from vbv_lernwelt.course.creators.test_utils import (
    create_course,
    create_course_session,
    create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.course_session_group.models import CourseSessionGroup
from vbv_lernwelt.learning_mentor.models import LearningMentor


class MeUserViewTest(APITestCase):
    def setUp(self) -> None:
        self.course, _ = create_course("Test Course")
        self.user = create_user("tester")
        self.url = reverse("get_cockpit_type", kwargs={"course_id": self.course.id})

    def test_no_cockpit(self) -> None:
        # GIVEN
        self.client.force_login(self.user)

        # WHEN
        response = self.client.get(self.url)

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEquals(response.data["type"], None)

    def test_mentor_cockpit(self) -> None:
        # GIVEN
        self.client.force_login(self.user)
        LearningMentor.objects.create(mentor=self.user, course=self.course)

        # WHEN
        response = self.client.get(self.url)

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEquals(response.data["type"], "mentor")

    def test_trainer_cockpit(self) -> None:
        # GIVEN
        self.client.force_login(self.user)
        course_session = create_course_session(course=self.course, title="Test Session")

        CourseSessionUser.objects.create(
            user=self.user,
            course_session=course_session,
            role=CourseSessionUser.Role.EXPERT,
        )

        # WHEN
        response = self.client.get(self.url)

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEquals(response.data["type"], "expert")

    def test_supervisor_cockpit(self):
        # GIVEN
        self.client.force_login(self.user)
        course_session = create_course_session(course=self.course, title="Test Session")

        csg = CourseSessionGroup.objects.create(
            name="Test Group",
            course=course_session.course,
        )

        csg.course_session.add(course_session)
        csg.supervisor.add(self.user)

        # WHEN
        response = self.client.get(self.url)

        # THEN
        self.assertEqual(response.status_code, status.HTTP_200_OK)
        self.assertEquals(response.data["type"], "expert")
