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.learnpath.models import LearningContent
from vbv_lernwelt.learnpath.tests.create_default_learning_path import create_default_learning_path
from vbv_lernwelt.learnpath.tests.test_create_default_learning_path import create_locales_for_wagtail


class CompletionApiTestCase(APITestCase):
    @classmethod
    def setUpClass(cls) -> None:
        super(CompletionApiTestCase, cls).setUpClass()
        create_locales_for_wagtail()
        create_default_users()
        create_default_learning_path()

    def setUp(self) -> None:
        self.user = User.objects.get(username='student')
        self.client.login(username='student', password='test')

    def test_completeLearningContent_works(self):
        learning_content = LearningContent.objects.get(title='Einleitung Circle "Anlayse"')
        learning_content_key = str(learning_content.translation_key)
        circle_key = str(learning_content.get_parent().translation_key)

        mark_url = f'/api/completion/circle/mark/'
        print(mark_url)

        response = self.client.post(mark_url, {
            'page_key': learning_content_key,
        })
        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]['circle_key'], circle_key)
        self.assertTrue(response_json[0]['completed'])

        # test getting the circle data
        response = self.client.get(f'/api/completion/circle/{circle_key}/')
        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]['circle_key'], circle_key)
        self.assertTrue(response_json[0]['completed'])
