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="test-student1@example.com")
        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.force_login(self.user)

    def test_completeLearningContent_happyCase(self):
        learning_content = LearningContentPlaceholder.objects.get(
            title="Fachcheck Reisen"
        )

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

        response = self.client.post(
            mark_url,
            {
                "page_id": learning_content.id,
                "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_id"], str(learning_content.id))
        self.assertEqual(
            response_json[0]["page_type"], "learnpath.LearningContentPlaceholder"
        )
        self.assertEqual(response_json[0]["completion_status"], "SUCCESS")

        db_entry = CourseCompletion.objects.get(
            user=self.user, course_session_id=self.cs.id, page_id=learning_content.id
        )
        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_id"], str(learning_content.id))
        self.assertEqual(
            response_json[0]["page_type"], "learnpath.LearningContentPlaceholder"
        )
        self.assertEqual(response_json[0]["completion_status"], "SUCCESS")

        # test with "fail"
        response = self.client.post(
            mark_url,
            {
                "page_id": learning_content.id,
                "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_id"], str(learning_content.id))
        self.assertEqual(
            response_json[0]["page_type"], "learnpath.LearningContentPlaceholder"
        )
        self.assertEqual(response_json[0]["completion_status"], "FAIL")

        db_entry = CourseCompletion.objects.get(
            user=self.user, course_session_id=self.cs.id, page_id=learning_content.id
        )
        self.assertEqual(db_entry.completion_status, "FAIL")
