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.consts import COURSE_TEST_ID
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseSession
from vbv_lernwelt.feedback.models import FeedbackResponse
from vbv_lernwelt.learnpath.models import LearningContentFeedbackUK


class FeedbackMutationTestCase(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.learning_content_feedback_page = LearningContentFeedbackUK.objects.get(
            slug="test-lehrgang-lp-circle-fahrzeug-lc-feedback"
        )
        self.student = User.objects.get(username="test-student1@example.com")
        self.client.force_login(self.student)

    def test_creates_response(self):
        data = {
            "course_negative_feedback": "schlecht",
            "course_positive_feedback": "gut",
            "feedback_type": "uk",
            "goal_attainment": 3,
            "preparation_task_clarity": False,
            "proficiency": 100,
            "satisfaction": 3,
            "would_recommend": False,
            "instructor_competence": None,
            "instructor_respect": None,
            "instructor_open_feedback": None,
        }

        response = self.query(
            f"""
mutation {{
  send_feedback(
    course_session_id: "{COURSE_TEST_ID}"
    learning_content_page_id: "{self.learning_content_feedback_page.id}"
    learning_content_type: "learnpath.LearningContentFeedbackUK"
    data: {{
        course_negative_feedback: "{data['course_negative_feedback']}",
        course_positive_feedback: "{data['course_positive_feedback']}",
        feedback_type: null,
        goal_attainment: {data['goal_attainment']},
        preparation_task_clarity: {str(data['preparation_task_clarity']).lower()},
        proficiency: {data['proficiency']},
        satisfaction: {data['satisfaction']},
        would_recommend: {str(data['would_recommend']).lower()},
        instructor_competence: null,
        instructor_respect: null,
        instructor_open_feedback: null,
    }},
    submitted: false
  ) {{
    feedback_response {{
      id
      data
      submitted
      __typename
    }}
    errors {{
      field
      messages
      __typename
    }}
    __typename
  }}
}}
            """
        )

        content = json.loads(response.content)

        self.assertResponseNoErrors(response)
        self.assertDictEqual(
            content["data"]["send_feedback"]["feedback_response"]["data"], data
        )

        feedback = FeedbackResponse.objects.first()
        self.assertEqual(feedback.data, data)
        self.assertEqual(feedback.submitted, False)
        self.assertEqual(feedback.feedback_user, self.student)
