import json

from rest_framework.test import APITestCase

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 (
    CourseCompletion,
    CourseSession,
    CourseSessionUser,
)
from vbv_lernwelt.learnpath.models import LearningContentPlaceholder


class CourseCompletionApiTestCase(APITestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course(include_uk=False)
        self.user = User.objects.get(username="admin")
        self.cs = CourseSession.objects.create(
            course_id=COURSE_TEST_ID,
            title="Test Lehrgang Session",
        )
        csu = CourseSessionUser.objects.create(
            course_session=self.cs,
            user=self.user,
        )
        self.client.login(username="admin", password="test")

    def test_completeLearningContent_works(self):
        learning_content = LearningContentPlaceholder.objects.get(
            title="Fachcheck Reisen"
        )
        learning_content_key = str(learning_content.translation_key)

        mark_url = f"/api/course/completion/mark/"

        response = self.client.post(
            mark_url,
            {
                "page_key": learning_content_key,
                "course_session_id": self.cs.id,
            },
        )
        response_json = response.json()
        print(json.dumps(response.json(), indent=2))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response_json), 1)
        self.assertEqual(response_json[0]["page_key"], learning_content_key)
        self.assertEqual(response_json[0]["completion_status"], "success")

        db_entry = CourseCompletion.objects.get(
            user=self.user, course_session_id=self.cs.id, page_key=learning_content_key
        )
        self.assertEqual(db_entry.completion_status, "success")

        # test getting the circle data
        response = self.client.get(f"/api/course/completion/{self.cs.id}/")
        print(response.status_code)
        response_json = response.json()
        print(json.dumps(response.json(), indent=2))

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response_json), 1)
        self.assertEqual(response_json[0]["page_key"], learning_content_key)
        self.assertTrue(response_json[0]["completion_status"], "success")

        # test with "fail"
        response = self.client.post(
            mark_url,
            {
                "page_key": learning_content_key,
                "completion_status": "fail",
                "course_session_id": self.cs.id,
            },
        )

        response_json = response.json()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response_json), 1)
        self.assertEqual(response_json[0]["page_key"], learning_content_key)
        self.assertEqual(response_json[0]["completion_status"], "fail")

        db_entry = CourseCompletion.objects.get(
            user=self.user, course_session_id=self.cs.id, page_key=learning_content_key
        )
        self.assertEqual(db_entry.completion_status, "fail")
