# Generated by Django 3.2.20 on 2023-09-11 14:39

from django.db import migrations
from django.db.models import Q

from vbv_lernwelt.assignment.models import Assignment
from vbv_lernwelt.competence.models import (
    ActionCompetence,
    ActionCompetenceListPage,
    CompetenceCertificate,
    CompetenceCertificateList,
    CompetenceNaviPage,
)
from vbv_lernwelt.course.consts import (
    COURSE_TEST_ID,
    COURSE_UK,
    COURSE_UK_FR,
    COURSE_UK_IT,
)
from vbv_lernwelt.course.creators.test_course import create_edoniq_test_assignment
from vbv_lernwelt.course.models import CoursePage
from vbv_lernwelt.learnpath.models import Circle, LearningContentEdoniqTest


def refactor_competence_wagtail_tree(apps=None, schema_editor=None):
    # create `CompetenceNaviPage` for every course where there is none
    for course_page in CoursePage.objects.all():
        competence_navi_page = (
            course_page.get_descendants().exact_type(CompetenceNaviPage).first()
        )
        if not competence_navi_page:
            competence_navi_page = CompetenceNaviPage(
                title="KompetenzNavi",
            )
            course_page.add_child(instance=competence_navi_page)

        acl = course_page.get_descendants().exact_type(ActionCompetenceListPage).first()
        acl.title = "Handlungskompetenzen"
        acl.save()
        acl.specific.save_revision().publish()

        acl = course_page.get_descendants().exact_type(ActionCompetenceListPage).first()
        acl.move(competence_navi_page, pos="last-child")

        # create `CompetenceCertificateList`
        competence_certificate_list = (
            competence_navi_page.get_descendants()
            .exact_type(CompetenceCertificateList)
            .first()
        )

        if course_page.course.id in [
            COURSE_TEST_ID,
            COURSE_UK,
            COURSE_UK_FR,
            COURSE_UK_IT,
        ]:
            if not competence_certificate_list:
                title = "Kompetenznachweise"
                if course_page.course.id == COURSE_UK_FR:
                    title = "Contrôles de compétences"
                if course_page.course.id == COURSE_UK_IT:
                    title = "Controlli delle competenze"

                competence_certificate_list = CompetenceCertificateList(title=title)
                competence_navi_page.add_child(instance=competence_certificate_list)

                title = "Kompetenznachweis 1"
                if course_page.course.id == COURSE_UK_FR:
                    title = "Contrôle de compétences 1"
                if course_page.course.id == COURSE_UK_IT:
                    title = "Controllo delle competenze 1"
                competence_certificate = CompetenceCertificate(
                    title=title,
                )
                competence_certificate_list.add_child(instance=competence_certificate)

                for casework_assignment in Assignment.objects.filter(
                    assignment_type="CASEWORK"
                ).descendant_of(course_page):
                    casework_assignment.competence_certificate = competence_certificate
                    casework_assignment.save()

                title = "Wissens- und Verständnisfragen"
                if course_page.course.id == COURSE_UK_FR:
                    title = "Questions de connaissance et de compréhension "
                if course_page.course.id == COURSE_UK_IT:
                    title = "Domande di conoscenza e di comprensione"

                edoniq_test = create_edoniq_test_assignment(
                    course_id=course_page.course.id,
                    title=title,
                    competence_certificate=competence_certificate,
                )

                circle_basis = (
                    Circle.objects.descendant_of(course_page)
                    .filter(Q(title="Basis") | Q(title="Base"))
                    .first()
                )

                if circle_basis:
                    for (
                        learning_content_edoniq
                    ) in LearningContentEdoniqTest.objects.all().descendant_of(
                        circle_basis
                    ):
                        learning_content_edoniq.content_assignment = edoniq_test
                        learning_content_edoniq.save()

    for ac in ActionCompetenceListPage.objects.all():
        # trigger slug update by saving
        ac.save()

    for competence in ActionCompetence.objects.all():
        if competence.competence_id.endswith(":"):
            # remove trailing colon
            competence.competence_id = competence.competence_id[:-1]
            competence.save()


class Migration(migrations.Migration):
    dependencies = [
        ("competence", "0004_rename_models"),
        ("learnpath", "0005_alter_learningcontentedoniqtest_content_assignment"),
    ]

    operations = [
        migrations.RunPython(refactor_competence_wagtail_tree),
    ]
