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_circle_expert,
    create_course,
    create_course_session,
    create_course_session_group,
    create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.feedback.models import FeedbackResponse


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

    def test_feedback(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 = create_user("member")
        add_course_session_user(
            course_session=course_session,
            user=member,
            role=CourseSessionUser.Role.MEMBER,
        )

        circle1, _ = create_circle(title="Test Circle 1", course_page=course_page)
        circle2, _ = create_circle(title="Test Circle 2", course_page=course_page)

        create_circle_expert(
            course_session=course_session, circle=circle1, username="Expert 1"
        )
        create_circle_expert(
            course_session=course_session, circle=circle1, username="Expert 2"
        )

        FeedbackResponse.objects.create(
            feedback_user=member,
            data={"satisfaction": 3},
            circle=circle1,
            course_session=course_session,
            submitted=True,
        )
        FeedbackResponse.objects.create(
            feedback_user=member,
            data={"satisfaction": 4},
            circle=circle1,
            course_session=course_session,
            submitted=True,
        )

        # Create Feedbacks for circle2
        FeedbackResponse.objects.create(
            feedback_user=member,
            data={"satisfaction": 1},
            circle=circle2,
            course_session=course_session,
            submitted=True,
        )
        FeedbackResponse.objects.create(
            feedback_user=member,
            data={"satisfaction": 2},
            circle=circle2,
            course_session=course_session,
            submitted=True,
        )

        self.client.force_login(supervisor)

        query = f"""query($course_id: ID!) {{
                    course_statistics(course_id: $course_id) {{
                        feedback_responses {{
                            records {{
                                course_session_id
                                generation
                                circle_id
                                satisfaction_average
                                satisfaction_max
                                details_url
                                experts
                            }}
                            summary {{
                                satisfaction_average
                                satisfaction_max
                                total_responses
                            }}
                        }}
                    }}
                }}
                                    """
        variables = {"course_id": str(course.id)}

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

        # THEN
        self.assertResponseNoErrors(response)

        course_statistics = response.json()["data"]["course_statistics"]
        feedback_responses = course_statistics["feedback_responses"]

        records = feedback_responses["records"]
        self.assertEqual(len(records), 2)

        circle1_record = next(
            (r for r in records if r["circle_id"] == str(circle1.id)), None
        )
        self.assertEqual(circle1_record["satisfaction_average"], 3.5)
        self.assertEqual(circle1_record["course_session_id"], str(course_session.id))
        self.assertEqual(circle1_record["generation"], "2023")
        self.assertEqual(
            circle1_record["details_url"],
            f"/course/{course.slug}/cockpit/feedback/{circle1.id}?courseSessionId={course_session.id}",
        )
        self.assertEqual(circle1_record["experts"], "Test Expert 1, Test Expert 2")

        circle2_record = next(
            (r for r in records if r["circle_id"] == str(circle2.id)), None
        )
        self.assertEqual(circle2_record["satisfaction_average"], 1.5)
        self.assertEqual(circle2_record["course_session_id"], str(course_session.id))
        self.assertEqual(circle2_record["generation"], "2023")
        self.assertEqual(
            circle2_record["details_url"],
            f"/course/{course.slug}/cockpit/feedback/{circle2.id}?courseSessionId={course_session.id}",
        )
        self.assertEqual(circle2_record["experts"], "")

        summary = feedback_responses["summary"]
        self.assertEqual(summary["satisfaction_average"], 2.5)
        self.assertEqual(summary["satisfaction_max"], 4)
        self.assertEqual(summary["total_responses"], 4)
