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.course.consts import COURSE_TEST_ID
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
from vbv_lernwelt.learnpath.models import LearningPath


class TestRetrieveLearingPathContents(APITestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course()
        self.slug = "test-lehrgang-lp"
        self.learning_path = LearningPath.objects.get(slug=self.slug)

    def test_get_learnpath_page(self):
        self.user = User.objects.get(username="admin")
        self.client.login(username="admin", password="test")

        response = self.client.get(f"/api/course/page/{self.slug}/")

        self.assertEqual(response.status_code, 200)
        data = response.json()

        self.assertEqual(self.learning_path.title, data["title"])
        # topics and circles
        self.assertEqual(4, len(data["children"]))
        # circle "analyse" contents
        self.assertEqual(13, len(data["children"][3]["children"]))

    def test_normalUser_withoutCourseSession_cannotAccess(self):
        self.user = User.objects.get(username="student")
        self.client.login(username="student", password="test")

        response = self.client.get(f"/api/course/page/{self.slug}/")

        self.assertEqual(response.status_code, 403)

    def test_normalUser_withCourseSession_canAccess(self):
        self.user = User.objects.get(username="student")
        self.client.login(username="student", password="test")

        course_session = CourseSession.objects.create(
            course_id=COURSE_TEST_ID,
            title="Test Lehrgang Session",
        )
        CourseSessionUser.objects.create(
            course_session=course_session,
            user=self.user,
        )

        response = self.client.get(f"/api/course/page/{self.slug}/")
        self.assertEqual(response.status_code, 200)
