from rest_framework.test import APITestCase

from vbv_lernwelt.core.admin import User
from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.learnpath.models import LearningPath, Circle
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 TestRetrieveLearingPathContents(APITestCase):
    @classmethod
    def setUpClass(cls) -> None:
        super(TestRetrieveLearingPathContents, cls).setUpClass()
        create_locales_for_wagtail()
        create_default_users()
        create_default_learning_path()

    def setUp(self) -> None:
        qs = LearningPath.objects.filter(title="Versicherungsvermittler/in")
        self.credentials = {
            'username': 'admin',
            'password': 'admin'}

        user = User.objects.get(username='admin')
        self.client.force_authenticate(user=user)
        self.client.post('/login/', self.credentials, follow=True)

    def test_get_circle(self):
        circle = Circle.objects.get(slug='analyse')
        response = self.client.get(f'/wagtailapi/v2/pages/{circle.id}/')
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.data['title'], 'Analyse')

    def test_get_circle_has_learning_sequences(self):
        circle = Circle.objects.get(slug='analyse')
        response = self.client.get(f'/wagtailapi/v2/pages/{circle.id}/')
        self.assertTrue(len(response.data['learning_sequences']) > 2)
        self.assertEqual(response.data['learning_sequences'][0]['title'], 'Starten')


    def test_get_circle_has_learning_sequences_learningpackages(self):
        circle = Circle.objects.get(slug='analyse')
        response = self.client.get(f'/wagtailapi/v2/pages/{circle.id}/')
        self.assertTrue(len(response.data['learning_sequences'][0]['learnging_packages']) > 1)
        self.assertTrue(response.data['learning_sequences'][0]['learnging_packages'][0]['title'])
