from django.test import TestCase

from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.creators.test_utils import (
    create_circle,
    create_course,
    create_course_session,
    create_learning_unit,
)
from vbv_lernwelt.learnpath.models import LearningContentPlaceholder


class SaveSlugTestCase(TestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course(include_uk=False)

    def test_save_willHandleSlug(self):
        lc_fachcheck = LearningContentPlaceholder.objects.get(title="Fachcheck Reisen")
        self.assertEqual(
            lc_fachcheck.slug, "test-lehrgang-lp-circle-reisen-lc-fachcheck-reisen"
        )

        # only changing minutes should not change slug
        lc_fachcheck.minutes = 135
        lc_fachcheck.save()
        lc_fachcheck = LearningContentPlaceholder.objects.get(id=lc_fachcheck.id)
        self.assertEqual(
            lc_fachcheck.slug, "test-lehrgang-lp-circle-reisen-lc-fachcheck-reisen"
        )

        # changing title should change slug
        lc_fachcheck.title = "Fachcheck Foobar"
        lc_fachcheck.save()
        lc_fachcheck = LearningContentPlaceholder.objects.get(id=lc_fachcheck.id)
        self.assertEqual(
            lc_fachcheck.slug, "test-lehrgang-lp-circle-reisen-lc-fachcheck-foobar"
        )

    def test_learning_unit_frontend_url_parts(self):
        # GIVEN
        course, course_page = create_course(course_page_title="What Ever Course Page")
        course_session = create_course_session(course=course, title=":)")
        circle, _ = create_circle(title="A-nice Circle", course_page=course_page)

        # WHEN
        cut = create_learning_unit(
            course_category_title="course category title",
            circle=circle,
            course=course,
        )
        course_part, circle_part, learning_unit_part = cut.get_frontend_url_parts()

        # THEN
        self.assertEqual(course_part, "what-ever-course-page")
        self.assertEqual(circle_part, "a-nice-circle")
        self.assertEqual(learning_unit_part, "course-category-title")
