from wagtail.models import Page

from vbv_lernwelt.course.models import CourseCompletion
from vbv_lernwelt.learnpath.utils import get_wagtail_type


def mark_course_completion(page_key, user, course_session, completion_status="success"):
    page = Page.objects.get(translation_key=page_key, locale__language_code="de-CH")
    page_type = get_wagtail_type(page.specific)
    course = page.specific.get_course()
    cc, created = CourseCompletion.objects.get_or_create(
        user=user,
        page_key=page_key,
        course_session_id=course_session.id,
    )
    cc.page_slug = page.slug
    cc.page_type = page_type
    cc.completion_status = completion_status
    cc.save()
    return cc
