import os
import random

import djclick as click

from vbv_lernwelt.assignment.creators.create_assignments import (
    create_uk_basis_prep_assignment,
    create_uk_fahrzeug_casework,
    create_uk_fahrzeug_prep_assignment,
    create_uk_fr_fahrzeug_casework,
    create_uk_fr_fahrzeug_prep_assignment,
    create_uk_fr_reflection,
    create_uk_it_fahrzeug_casework,
    create_uk_it_fahrzeug_prep_assignment,
    create_uk_it_reflection,
    create_uk_kickoff_prep_assignment,
    create_uk_reflection,
    create_vv_einkommenssicherung_casework,
    create_vv_gesundheit_casework,
    create_vv_gewinnen_casework,
    create_vv_reflection,
)
from vbv_lernwelt.assignment.models import Assignment
from vbv_lernwelt.assignment.services import update_assignment_completion
from vbv_lernwelt.assignment.tests.assignment_factories import AssignmentListPageFactory
from vbv_lernwelt.competence.create_uk_competence_profile import (
    create_uk_competence_profile,
    create_uk_fr_competence_profile,
    create_uk_it_competence_profile,
)
from vbv_lernwelt.competence.create_uk_training_competence_profile import (
    create_uk_fr_training_competence_profile,
    create_uk_it_training_competence_profile,
    create_uk_training_competence_profile,
)
from vbv_lernwelt.competence.create_vv_new_competence_profile import (
    create_vv_new_competence_profile,
)
from vbv_lernwelt.competence.models import PerformanceCriteria
from vbv_lernwelt.core.create_default_users import default_users
from vbv_lernwelt.core.models import User
from vbv_lernwelt.course.consts import (
    COURSE_TEST_ID,
    COURSE_UK,
    COURSE_UK_FR,
    COURSE_UK_IT,
    COURSE_UK_TRAINING,
    COURSE_UK_TRAINING_FR,
    COURSE_UK_TRAINING_IT,
    COURSE_VERSICHERUNGSVERMITTLERIN_ID,
)
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.creators.uk_course import (
    create_uk_fr_learning_path,
    create_uk_it_learning_path,
    create_uk_learning_path,
)
from vbv_lernwelt.course.creators.uk_training_course import (
    create_uk_training_learning_path,
)
from vbv_lernwelt.course.creators.versicherungsvermittlerin import (
    create_versicherungsvermittlerin_with_categories,
)
from vbv_lernwelt.course.models import (
    Course,
    CoursePage,
    CourseSession,
    CourseSessionUser,
)
from vbv_lernwelt.course.services import mark_course_completion
from vbv_lernwelt.feedback.creators.create_demo_feedback import create_feedback
from vbv_lernwelt.importer.services import (
    import_course_sessions_from_excel,
    import_students_from_excel,
    import_trainers_from_excel,
)
from vbv_lernwelt.learnpath.create_vv_new_learning_path import (
    create_vv_new_learning_path,
)
from vbv_lernwelt.learnpath.models import (
    Circle,
    LearningContent,
    LearningContentAssignment,
    LearningContentAttendanceCourse,
)
from vbv_lernwelt.media_library.create_default_media_library import (
    create_default_media_library,
)

ADMIN_EMAILS = ["info@iterativ.ch", "admin"]


@click.command()
@click.option(
    "--course",
    "-c",
    multiple=True,
    default=[
        COURSE_TEST_ID,
        COURSE_UK,
        COURSE_VERSICHERUNGSVERMITTLERIN_ID,
        COURSE_UK_FR,
        COURSE_UK_IT,
        COURSE_UK_TRAINING,
        COURSE_UK_TRAINING_FR,
        COURSE_UK_TRAINING_IT,
    ],
)
def command(course):
    print("Creating default courses", course)

    if COURSE_VERSICHERUNGSVERMITTLERIN_ID in course:
        create_versicherungsvermittlerin_course()

    if COURSE_UK in course:
        create_course_uk_de()
        create_course_uk_de_completion_data(
            CourseSession.objects.get(title="Bern 2023 a")
        )
        create_course_uk_de_assignment_completion_data(
            assignment=Assignment.objects.get(
                slug="überbetriebliche-kurse-assignment-überprüfen-einer-motorfahrzeugs-versicherungspolice"
            ),
            course_session=CourseSession.objects.get(title="Bern 2023 a"),
            user=User.objects.get(email="lina.egger@example.com"),
        )

    if COURSE_UK_FR in course:
        create_course_uk_fr()

    if COURSE_UK_IT in course:
        create_course_uk_it()

    if COURSE_TEST_ID in course:
        create_test_course(with_sessions=True)

    if COURSE_UK_TRAINING in course:
        create_course_training_de()

    if COURSE_UK_TRAINING_FR in course:
        create_course_training_fr()

    if COURSE_UK_TRAINING_IT in course:
        create_course_training_it()


def create_versicherungsvermittlerin_course():
    # Versicherungsvermittler/in mit neuen Circles
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID,
        title="Versicherungsvermittler/-in",
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )

    create_vv_new_competence_profile(course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
    create_default_media_library(course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
    create_vv_einkommenssicherung_casework(
        course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID
    )
    create_vv_gesundheit_casework(course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
    create_vv_gewinnen_casework(course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
    create_vv_reflection(course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
    create_vv_new_learning_path()

    cs = CourseSession.objects.create(
        course_id=COURSE_VERSICHERUNGSVERMITTLERIN_ID,
        title="Versicherungsvermittler/-in",
    )
    for user_data in default_users:
        CourseSessionUser.objects.create(
            course_session=cs,
            user=User.objects.get(username=user_data["email"]),
        )

    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="student-vv@eiger-versicherungen.ch"),
    )
    expert1 = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="expert-vv.expert1@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )

    expert2 = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="expert-vv.expert2@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )

    expert3 = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="expert-vv.expert3@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )

    experts = [expert1, expert2, expert3]

    circles = Circle.objects.filter(slug__startswith="versicherungsvermittler-in-lp")
    for i, circle in enumerate(circles):
        expert = experts[i % len(experts)]
        expert.expert.add(circle)
        create_feedback(circle, cs, 3)

    for admin_email in ADMIN_EMAILS:
        CourseSessionUser.objects.create(
            course_session=cs,
            user=User.objects.get(email=admin_email),
        )


def create_course_uk_de():
    # Überbetriebliche Kurse DE
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK, title="Überbetriebliche Kurse"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_kickoff_prep_assignment(course_id=COURSE_UK)
    create_uk_basis_prep_assignment(course_id=COURSE_UK)
    create_uk_fahrzeug_casework(course_id=COURSE_UK)
    create_uk_fahrzeug_prep_assignment(course_id=COURSE_UK)
    create_uk_reflection(course_id=COURSE_UK)

    # learning path
    create_uk_learning_path(course_id=COURSE_UK)
    create_uk_competence_profile(course_id=COURSE_UK)
    create_default_media_library(course_id=COURSE_UK)

    cs = CourseSession.objects.create(
        course_id=COURSE_UK,
        title="Bern 2023 a",
        attendance_courses=[
            {
                "learningContentId": LearningContentAttendanceCourse.objects.get(
                    slug="überbetriebliche-kurse-lp-circle-fahrzeug-lc-präsenzkurs-fahrzeug"
                ).id,
                "start": "2023-05-23T08:30:00+0200",
                "end": "2023-05-23T17:00:00+0200",
                "location": "Handelsschule KV Bern, Zimmer 123, Eigerstrasse 16, 3012 Bern",
                "trainer": "Roland Grossenbacher, roland.grossenbacher@helvetia.ch",
            }
        ],
        assignment_details_list=[
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug="überbetriebliche-kurse-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug="überbetriebliche-kurse-lp-circle-fahrzeug-lc-fahrzeug-mein-erstes-auto"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
        ],
    )

    # figma demo users and data
    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="patrizia.huggel@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )
    csu.expert.add(Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-kickoff"))
    csu.expert.add(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-haushalt-teil-1")
    )
    csu.expert.add(Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-fahrzeug"))
    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="andreas.feuz@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )
    csu.expert.add(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-haushalt-teil-2")
    )
    csu.expert.add(Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis"))
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="michael.meier@example.com"),
    )
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="lina.egger@example.com"),
    )
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="evelyn.schmid@example.com"),
    )

    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="christoph.bosshard@vbv-afa.ch"),
    )

    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="axel.manderbach@lernetz.ch"),
    )

    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="patrick.muster@eiger-versicherungen.ch"),
    )

    create_feedback(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-kickoff"),
        cs,
        3,
    )
    create_feedback(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-haushalt-teil-2"),
        cs,
        14,
    )
    create_feedback(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis"), cs, 4
    )

    # course session Überbetriebliche Kurse Lehrjahr 1 - Region Zürich
    cs = CourseSession.objects.create(
        course_id=COURSE_UK,
        title="Zürich 2023 a",
    )
    # for user_data in default_users:
    #     CourseSessionUser.objects.create(
    #         course_session=cs,
    #         user=User.objects.get(username=user_data["email"]),
    #     )
    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="trainer-uk1.einstieg@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )
    csu.expert.add(Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis"))
    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="trainer-uk1.analyse@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )
    csu.expert.add(
        Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-haushalt-teil-2")
    )
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="student-uk1-zurich@eiger-versicherungen.ch"),
    )
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="michael.meier@example.com"),
    )


def create_course_uk_fr():
    # Überbetriebliche Kurse FR
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK_FR, title="Cours interentreprises"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_fr_fahrzeug_casework(course_id=COURSE_UK_FR)
    create_uk_fr_fahrzeug_prep_assignment(course_id=COURSE_UK_FR)
    create_uk_fr_reflection(course_id=COURSE_UK_FR)
    create_uk_fr_learning_path(course_id=COURSE_UK_FR)
    create_uk_fr_competence_profile(course_id=COURSE_UK_FR)
    create_default_media_library(course_id=COURSE_UK_FR)

    cs = CourseSession.objects.create(
        course_id=COURSE_UK_FR,
        title="Cours interentreprises année 1 - Région Fribourg",
    )

    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="geraldine.kolly@assurance.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )
    csu.expert.add(
        Circle.objects.get(slug="cours-interentreprises-lp-circle-coup-denvoi")
    )
    _csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="luca.dupont@assurance.ch"),
    )
    csu = CourseSessionUser.objects.create(
        course_session=cs,
        user=User.objects.get(username="andreas.feuz@eiger-versicherungen.ch"),
        role=CourseSessionUser.Role.EXPERT,
    )

    for fr_circle in Circle.objects.filter(
        slug__startswith="cours-interentreprises-lp"
    ):
        csu.expert.add(fr_circle)


def create_course_uk_it():
    # Überbetriebliche Kurse FR
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK_IT, title="Corso interaziendale"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_it_fahrzeug_casework(course_id=COURSE_UK_IT)
    create_uk_it_fahrzeug_prep_assignment(course_id=COURSE_UK_IT)
    create_uk_it_reflection(course_id=COURSE_UK_IT)
    create_uk_it_learning_path(course_id=COURSE_UK_IT)
    create_uk_it_competence_profile(course_id=COURSE_UK_IT)
    create_default_media_library(course_id=COURSE_UK_IT)

    _cs = CourseSession.objects.create(
        course_id=COURSE_UK_IT,
        title="Corso interaziendale Ticino",
    )


def create_course_uk_de_assignment_completion_data(assignment, course_session, user):
    subtasks = assignment.filter_user_subtasks(subtask_types=["user_text_input"])
    for index, subtask in enumerate(subtasks):
        user_text = f"Lorem ipsum dolor sit amet... {index}"

        update_assignment_completion(
            assignment_user=user,
            assignment=assignment,
            course_session=course_session,
            completion_data={
                subtask["id"]: {
                    "user_data": {"text": user_text},
                }
            },
        )
    update_assignment_completion(
        assignment_user=user,
        assignment=assignment,
        course_session=course_session,
        completion_status="SUBMITTED",
    )


def create_course_uk_de_completion_data(course_session):
    circles = Circle.objects.filter(slug__startswith="überbetriebliche-kurse-lp-circle")
    for circle in circles:
        for index, lc in enumerate(circle.get_descendants().type(LearningContent)):
            mark_course_completion(
                str(lc.translation_key),
                User.objects.get(email="lina.egger@example.com"),
                course_session=course_session,
                completion_status="success",
            )

            random_number = random.randint(1, 3)
            if index % random_number == 0:
                mark_course_completion(
                    str(lc.translation_key),
                    User.objects.get(email="michael.meier@example.com"),
                    course_session=course_session,
                    completion_status="success",
                )

    performance_criteria = (
        CoursePage.objects.get(course_id=COURSE_UK)
        .get_descendants()
        .type(PerformanceCriteria)
    )
    for index, pc in enumerate(performance_criteria):
        mark_course_completion(
            str(pc.translation_key),
            User.objects.get(email="lina.egger@example.com"),
            course_session=course_session,
            completion_status="success",
        )

        random_number = random.randint(1, 4)
        if index % random_number == 0:
            mark_course_completion(
                str(pc.translation_key),
                User.objects.get(email="michael.meier@example.com"),
                course_session=course_session,
                completion_status="success",
            )
        if index % random_number == 1:
            mark_course_completion(
                str(pc.translation_key),
                User.objects.get(email="michael.meier@example.com"),
                course_session=course_session,
                completion_status="fail",
            )


def create_course_training_de():
    # Test Lehrgang für üK Trainer
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK_TRAINING, title="myVBV Training"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_fahrzeug_casework(course_id=COURSE_UK_TRAINING)
    create_uk_fahrzeug_prep_assignment(course_id=COURSE_UK_TRAINING)
    create_uk_reflection(course_id=COURSE_UK_TRAINING)

    create_uk_training_learning_path(course_id=COURSE_UK_TRAINING)
    create_uk_training_competence_profile(course_id=COURSE_UK_TRAINING)
    create_default_media_library(course_id=COURSE_UK_TRAINING)

    current_dir = os.path.dirname(os.path.realpath(__file__))
    print(current_dir)
    course = Course.objects.get(id=COURSE_UK_TRAINING)
    import_course_sessions_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
    )
    import_trainers_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
    )
    import_students_from_excel(
        f"{current_dir}/../../../importer/tests/Schulungen_Teilnehmende.xlsx",
    )

    for cs in CourseSession.objects.filter(course_id=COURSE_UK_TRAINING):
        cs.assignment_details_list = [
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-fahrzeug-lc-überprüfen-einer-motorfahrzeug-versicherungspolice"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-fahrzeug-lc-fahrzeug-mein-erstes-auto"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
        ]
        cs.save()

    # attach users as trainers to ÜK course
    course_uk = Course.objects.filter(id=COURSE_UK).first()
    if course_uk:
        users = [
            csu.user
            for csu in CourseSessionUser.objects.filter(
                course_session__course_id=COURSE_UK_TRAINING
            )
        ]

        cs = CourseSession.objects.get(course_id=COURSE_UK, title="Bern 2023 a")

        for user in users:
            csu, _created = CourseSessionUser.objects.get_or_create(
                course_session_id=cs.id, user_id=user.id
            )
            csu.role = CourseSessionUser.Role.EXPERT
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-kickoff")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-fahrzeug")
            )

            csu.save()


def create_course_training_fr():
    # Test Lehrgang für üK Trainer FR
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK_TRAINING_FR, title="myVBV Training (FR)"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_fr_fahrzeug_casework(course_id=COURSE_UK_TRAINING_FR)
    create_uk_fr_fahrzeug_prep_assignment(course_id=COURSE_UK_TRAINING_FR)
    create_uk_fr_reflection(course_id=COURSE_UK_TRAINING_FR)

    create_uk_training_learning_path(course_id=COURSE_UK_TRAINING_FR)
    create_uk_fr_training_competence_profile(course_id=COURSE_UK_TRAINING_FR)
    create_default_media_library(course_id=COURSE_UK_TRAINING_FR)

    current_dir = os.path.dirname(os.path.realpath(__file__))
    print(current_dir)
    course = Course.objects.get(id=COURSE_UK_TRAINING_FR)
    import_course_sessions_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
        language="fr",
    )
    import_trainers_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
        language="fr",
    )
    import_students_from_excel(
        f"{current_dir}/../../../importer/tests/Schulungen_Teilnehmende.xlsx",
    )

    for cs in CourseSession.objects.filter(course_id=COURSE_UK_TRAINING_FR):
        cs.assignment_details_list = [
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-véhicule-lc-vérification-dune-police-dassurance-de-véhicule-à-moteur"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-véhicule-lc-véhicule-à-moteur-ma-première-voiture"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-13T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
        ]
        cs.save()

    # attach users as trainers to ÜK course
    course_uk = Course.objects.filter(id=COURSE_UK_FR).first()
    if course_uk:
        users = [
            csu.user
            for csu in CourseSessionUser.objects.filter(
                course_session__course_id=COURSE_UK_TRAINING_FR
            )
        ]

        cs = CourseSession.objects.get(
            course_id=COURSE_UK_FR,
            title="Cours interentreprises année 1 - Région Fribourg",
        )

        for user in users:
            csu, _created = CourseSessionUser.objects.get_or_create(
                course_session_id=cs.id, user_id=user.id
            )
            csu.role = CourseSessionUser.Role.EXPERT
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-kickoff")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-fahrzeug")
            )

            csu.save()


def create_course_training_it():
    # Test Lehrgang für üK Trainer FR
    course = create_versicherungsvermittlerin_with_categories(
        course_id=COURSE_UK_TRAINING_IT, title="myVBV Training (IT)"
    )

    # assignments create assignments parent page
    _assignment_list_page = AssignmentListPageFactory(
        parent=course.coursepage,
    )
    create_uk_it_fahrzeug_casework(course_id=COURSE_UK_TRAINING_IT)
    create_uk_it_fahrzeug_prep_assignment(course_id=COURSE_UK_TRAINING_IT)
    create_uk_it_reflection(course_id=COURSE_UK_TRAINING_IT)

    create_uk_training_learning_path(course_id=COURSE_UK_TRAINING_IT)
    create_uk_it_training_competence_profile(course_id=COURSE_UK_TRAINING_IT)
    create_default_media_library(course_id=COURSE_UK_TRAINING_IT)

    current_dir = os.path.dirname(os.path.realpath(__file__))
    print(current_dir)
    course = Course.objects.get(id=COURSE_UK_TRAINING_IT)
    import_course_sessions_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
        language="it",
    )
    import_trainers_from_excel(
        course,
        f"{current_dir}/../../../importer/tests/Schulungen_Durchfuehrung_Trainer.xlsx",
        language="it",
    )
    import_students_from_excel(
        f"{current_dir}/../../../importer/tests/Schulungen_Teilnehmende.xlsx",
    )

    for cs in CourseSession.objects.filter(course_id=COURSE_UK_TRAINING_IT):
        cs.assignment_details_list = [
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-veicolo-lc-verifica-di-una-polizza-di-assicurazione-veicoli-a-motore"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-20T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
            {
                "learningContentId": LearningContentAssignment.objects.get(
                    slug=f"{course.slug}-lp-circle-veicolo-lc-veicolo-la-mia-prima-auto"
                ).id,
                "submissionDeadlineDateTimeUtc": "2023-06-20T19:00:00Z",
                "evaluationDeadlineDateTimeUtc": "2023-06-27T19:00:00Z",
            },
        ]
        cs.save()

    # attach users as trainers to ÜK course
    course_uk = Course.objects.filter(id=COURSE_UK_IT).first()
    if course_uk:
        users = [
            csu.user
            for csu in CourseSessionUser.objects.filter(
                course_session__course_id=COURSE_UK_TRAINING_IT
            )
        ]

        cs = CourseSession.objects.get(
            course_id=COURSE_UK_IT,
            title="Corso interaziendale Ticino",
        )

        for user in users:
            csu, _created = CourseSessionUser.objects.get_or_create(
                course_session_id=cs.id, user_id=user.id
            )
            csu.role = CourseSessionUser.Role.EXPERT
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-kickoff")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-basis")
            )
            csu.expert.add(
                Circle.objects.get(slug="überbetriebliche-kurse-lp-circle-fahrzeug")
            )

            csu.save()
