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 CourseSession, CourseSessionUser


class CourseCompletionApiTestCase(APITestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course(include_uk=False)

        self.user = User.objects.get(username="student")

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

        self.client.login(username="student", password="test")

    def test_api_noCourseSession_withoutCourseSessionUser(self):
        response = self.client.get(f"/api/course/sessions/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 0)

    def test_api_courseSession_withCourseSessionUser(self):
        csu = CourseSessionUser.objects.create(
            course_session=self.course_session,
            user=self.user,
        )
        response = self.client.get(f"/api/course/sessions/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 1)

        print(json.dumps(response.json(), indent=4))
        self.assertEqual(response.json()[0]["id"], self.course_session.id)

    def test_api_superUser_canAccessEveryCourseSession(self):
        self.client.login(username="admin", password="test")
        response = self.client.get(f"/api/course/sessions/")

        self.assertEqual(response.status_code, 200)
        self.assertEqual(len(response.json()), 1)

        print(json.dumps(response.json(), indent=4))
        self.assertEqual(response.json()[0]["id"], self.course_session.id)
