from graphene_django.utils import GraphQLTestCase

from vbv_lernwelt.course.creators.test_utils import (
    add_course_session_group_supervisor,
    add_course_session_user,
    create_circle,
    create_course,
    create_course_session,
    create_course_session_group,
    create_performance_criteria_page,
    create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.course.services import mark_course_completion


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

    def test_competence(self):
        # GIVEN
        course, course_page = create_course("Test Course")
        course_session = create_course_session(course=course, title="Test Bern 2022 a")

        supervisor = create_user("supervisor")
        group = create_course_session_group(course_session=course_session)
        add_course_session_group_supervisor(group=group, user=supervisor)

        member_one = create_user("member one")
        add_course_session_user(
            course_session=course_session,
            user=member_one,
            role=CourseSessionUser.Role.MEMBER,
        )

        member_two = create_user("member two")
        add_course_session_user(
            course_session=course_session,
            user=member_two,
            role=CourseSessionUser.Role.MEMBER,
        )

        circle, _ = create_circle(title="Test Circle", course_page=course_page)

        pc = create_performance_criteria_page(
            course=course, course_page=course_page, circle=circle
        )

        mark_course_completion(
            page=pc,
            user=member_one,
            course_session=course_session,
            completion_status="SUCCESS",
        )

        mark_course_completion(
            page=pc,
            user=member_two,
            course_session=course_session,
            completion_status="FAIL",
        )

        self.client.force_login(supervisor)

        query = f"""query($course_id: ID!) {{
                    course_statistics(course_id: $course_id) {{
                        competences {{
                            records {{
                                title
                                course_session_id
                                generation
                                circle_id
                                success_count
                                fail_count
                                details_url
                            }}
                            summary {{
                                success_total
                                fail_total
                            }}
                        }}
                    }}
                }}
                                    """
        variables = {"course_id": str(course.id)}

        # WHEN
        response = self.query(query, variables=variables)

        # THEN
        self.assertResponseNoErrors(response)

        competences = response.json()["data"]["course_statistics"]["competences"]
        records = competences["records"]

        self.assertEqual(records[0]["title"], "Learning Unit")
        self.assertEqual(records[0]["success_count"], 1)
        self.assertEqual(records[0]["fail_count"], 1)
        self.assertEqual(records[0]["circle_id"], str(circle.id))
        self.assertEqual(records[0]["course_session_id"], str(course_session.id))
        self.assertEqual(records[0]["generation"], "2023")
        self.assertEqual(
            records[0]["details_url"],
            f"/course/{course.slug}/cockpit?courseSessionId={course_session.id}",
        )

        summary = competences["summary"]
        self.assertEqual(summary["success_total"], 1)
        self.assertEqual(summary["fail_total"], 1)
