from django.test import TestCase

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.iam.permissions import is_course_session_expert


class ExpertTestCase(TestCase):
    def setUp(self):
        self.course, _ = create_course("Test Course")
        self.course_session = create_course_session(
            course=self.course, title="Test Session"
        )

        self.user = create_user("user")

    def test_member(self):
        # GIVEN
        csu = CourseSessionUser.objects.create(
            course_session=self.course_session,
            user=self.user,
            role=CourseSessionUser.Role.MEMBER,
        )

        # WHEN
        is_expert = is_course_session_expert(
            user=csu.user, course_session_id=self.course_session.id
        )

        # THEN
        self.assertFalse(is_expert)

    def test_supervisor(self):
        # GIVEN
        csg = CourseSessionGroup.objects.create(name="Test Group", course=self.course)
        csg.course_session.add(self.course_session)
        csg.supervisor.add(self.user)

        # WHEN
        is_expert = is_course_session_expert(
            user=self.user, course_session_id=self.course_session.id
        )

        # THEN
        self.assertTrue(is_expert)

    def test_expert(self):
        # GIVEN
        csu = CourseSessionUser.objects.create(
            course_session=self.course_session,
            user=self.user,
            role=CourseSessionUser.Role.EXPERT,
        )

        # WHEN
        is_expert = is_course_session_expert(
            user=csu.user, course_session_id=self.course_session.id
        )

        # THEN
        self.assertTrue(is_expert)
