from graphene_django.utils import GraphQLTestCase

from vbv_lernwelt.course.creators.test_utils import (
    add_course_session_group_course_session,
    add_course_session_group_supervisor,
    add_course_session_user,
    create_course,
    create_course_session,
    create_course_session_group,
    create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser


class DashboardTestCase(GraphQLTestCase):
    GRAPHQL_URL = "/server/graphql/"

    def test_selection_metrics(self):
        # GIVEN
        course_1, _ = create_course("Test Course 1")
        course_2, _ = create_course("Dummy Course 2")

        cs_1_a = create_course_session(course=course_1, title="Zug", generation="1984")
        cs_1_b = create_course_session(course=course_1, title="Bern", generation="1984")
        cs_1_c = create_course_session(course=course_1, title="Wil", generation="1984")
        cs_2_a = create_course_session(course=course_2, title="Baar", generation="1984")

        member_1 = create_user("member_1")
        member_2 = create_user("member_2")
        member_3 = create_user("member_3")
        member_4 = create_user("member_4")

        expert_1 = create_user("expert_1")
        expert_2 = create_user("expert_2")
        expert_3 = create_user("expert_3")
        expert_4 = create_user("expert_4")

        # CS 1 A
        add_course_session_user(
            course_session=cs_1_a, user=member_1, role=CourseSessionUser.Role.MEMBER
        )
        add_course_session_user(
            course_session=cs_1_b, user=member_2, role=CourseSessionUser.Role.MEMBER
        )

        # CS 1 B
        add_course_session_user(
            course_session=cs_1_a, user=expert_1, role=CourseSessionUser.Role.EXPERT
        )
        add_course_session_user(
            course_session=cs_1_b, user=expert_2, role=CourseSessionUser.Role.EXPERT
        )

        # CS 1 C
        add_course_session_user(
            course_session=cs_1_c, user=member_3, role=CourseSessionUser.Role.MEMBER
        )
        add_course_session_user(
            course_session=cs_1_c, user=expert_3, role=CourseSessionUser.Role.EXPERT
        )

        # CS 2 A
        add_course_session_user(
            course_session=cs_2_a, user=member_4, role=CourseSessionUser.Role.MEMBER
        )
        add_course_session_user(
            course_session=cs_2_a, user=expert_4, role=CourseSessionUser.Role.EXPERT
        )

        # SUPERVISOR of course 1, session a and b BUT NOT
        #  of course 1, session c or course 2, session a
        cs_1_ab_supervisor = create_user("supervisor")
        group = create_course_session_group(course_session=cs_1_a)
        add_course_session_group_course_session(course_session=cs_1_b, group=group)
        add_course_session_group_supervisor(group=group, user=cs_1_ab_supervisor)

        self.client.force_login(cs_1_ab_supervisor)

        # WHEN
        query = f"""query($course_id: ID!) {{
                        course_statistics(course_id: $course_id) {{
                            course_session_selection_metrics {{
                                expert_count
                                participant_count
                                session_count
                            }}
                        }}
                    }}"""

        variables = {"course_id": str(course_1.id)}
        response = self.query(query, variables=variables)

        # THEN
        self.assertResponseNoErrors(response)

        metrics = response.json()["data"]["course_statistics"][
            "course_session_selection_metrics"
        ]
        self.assertEqual(metrics["expert_count"], 2)
        self.assertEqual(metrics["participant_count"], 2)
        self.assertEqual(metrics["session_count"], 2)
