import json

from graphene_django.utils.testing import GraphQLTestCase

from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseSession


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

    def setUp(self):
        create_default_users()
        create_test_course(include_vv=False, with_sessions=True)
        self.course_session = CourseSession.objects.get(title="Test Bern 2022 a")
        self.attendance_course = (
            self.course_session.coursesessionattendancecourse_set.first()
        )
        self.trainer = User.objects.get(username="test-trainer1@example.com")
        self.client.force_login(self.trainer)

    def test_simple_query(self):
        response = self.query(
            f"""
            {{
              course_session_attendance_course(id:{self.attendance_course.id}) {{
                id
                trainer
              }}
            }}
            """
        )

        content = json.loads(response.content)

        self.assertResponseNoErrors(response)
        self.assertEqual(
            content["data"]["course_session_attendance_course"]["trainer"],
            "Roland Grossenbacher, roland.grossenbacher@helvetia.ch",
        )

    def test_mutation_add_user_when_present(self):
        student = User.objects.get(username="test-student1@example.com")

        query = f"""
        mutation {{
          update_course_session_attendance_course_users(
            id:{self.attendance_course.id}, 
            attendance_user_list:[
              {{user_id: "{student.id}", status: PRESENT}},
              {{user_id: "3372836c-1c95-4721-881d-5342a60a3bfe", status: PRESENT}},
            ]
          ) {{
            course_session_attendance_course {{
              id
              attendance_user_list {{
                user_id
                first_name
                last_name
                email
                status
              }}
            }}
          }}
        }}
        """
        print(query)
        response = self.query(query)
        self.assertResponseNoErrors(response)

        data = json.loads(response.content)

        self.maxDiff = None
        self.assertDictEqual(
            {
                "data": {
                    "update_course_session_attendance_course_users": {
                        "course_session_attendance_course": {
                            "id": str(self.attendance_course.id),
                            "attendance_user_list": [
                                {
                                    "user_id": str(student.id),
                                    "first_name": student.first_name,
                                    "last_name": student.last_name,
                                    "email": student.email,
                                    "status": "PRESENT",
                                }
                            ],
                        }
                    }
                }
            },
            data,
        )
