import json
from datetime import datetime

from django.utils import timezone
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
from vbv_lernwelt.course_session.models import CourseSessionAssignment
from vbv_lernwelt.duedate.factories import DueDateFactory
from vbv_lernwelt.learnpath.models import LearningContentAssignment


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"], str(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"], str(self.course_session.id))

    def test_api_hasNoDueDates(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]["due_dates"], [])

    def test_api_hasDueDates(self):
        cs = CourseSession.objects.first()

        due_date = DueDateFactory(
            start=timezone.make_aware(datetime.now()),
            course_session=cs,
            title="Test Due Date",
        )
        csa = CourseSessionAssignment.objects.create(
            course_session=cs,
            learning_content=LearningContentAssignment.objects.get(
                slug=f"test-lehrgang-lp-circle-reisen-lc-mein-kundenstamm"
            ),
        )

        csa.submission_deadline = due_date

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

        self.assertEqual(response.status_code, 200)
        print(json.dumps(response.json(), indent=4))
        self.assertEqual(len(response.json()), 1)

        self.assertEqual(len(response.json()[0]["due_dates"]), 1)
        self.assertEqual(response.json()[0]["due_dates"][0]["title"], "Test Due Date")
