from django.test import TestCase

from vbv_lernwelt.course.creators.test_utils import (
    add_course_session_user,
    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 has_role_in_course, is_user_mentor
from vbv_lernwelt.learning_mentor.models import LearningMentor


class RoleTestCase(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_has_role_regional(self):
        # GIVEN
        csg = CourseSessionGroup.objects.create(name="Test Group", course=self.course)
        csg.supervisor.add(self.user)

        # WHEN
        has_role = has_role_in_course(user=self.user, course=self.course)

        # THEN
        self.assertTrue(has_role)

    def test_has_role_course_session(self):
        # GIVEN
        add_course_session_user(
            self.course_session,
            self.user,
            role=CourseSessionUser.Role.MEMBER,
        )

        # WHEN
        has_role = has_role_in_course(user=self.user, course=self.course)

        # THEN
        self.assertTrue(has_role)

    def test_has_role_mentor(self):
        # GIVEN
        LearningMentor.objects.create(
            mentor=self.user,
            course=self.course,
        )

        # WHEN
        has_role = has_role_in_course(user=self.user, course=self.course)

        # THEN
        self.assertTrue(has_role)

    def test_is_user_mentor(self):
        # GIVEN
        member = create_user("member")
        mentor = create_user("mentor")

        course, course_page = create_course("Test Course")
        course_session = create_course_session(course=course, title=":)")

        member_csu = add_course_session_user(
            course_session=course_session,
            user=member,
            role=CourseSessionUser.Role.MEMBER,
        )

        learning_mentor = LearningMentor.objects.create(
            mentor=mentor,
            course=course,
        )

        learning_mentor.participants.add(member_csu)
        learning_mentor.save()

        # WHEN
        is_mentor = is_user_mentor(
            mentor=mentor,
            participant_user_id=member.id,
            course_session_id=course_session.id,
        )

        # THEN
        self.assertTrue(is_mentor)

    def test_not_is_user_mentor(self):
        # GIVEN
        member = create_user("member")
        wanna_be_mentor = create_user("wanna_be_mentor")

        course, course_page = create_course("Test Course")
        course_session = create_course_session(course=course, title=":)")

        add_course_session_user(
            course_session=course_session,
            user=member,
            role=CourseSessionUser.Role.MEMBER,
        )

        # WHEN
        is_mentor = is_user_mentor(
            mentor=wanna_be_mentor,
            participant_user_id=member.id,
            course_session_id=course_session.id,
        )

        # THEN
        self.assertFalse(is_mentor)

    def test_no_role(self):
        # GIVEN
        other_course, _ = create_course("Other Test Course")
        other_course_session = create_course_session(
            course=other_course, title="Other Test Session"
        )
        add_course_session_user(
            other_course_session,
            self.user,
            role=CourseSessionUser.Role.MEMBER,
        )

        # WHEN
        has_role = has_role_in_course(user=self.user, course=self.course)

        # THEN
        self.assertFalse(has_role)
