import os

from django.test import TestCase
from openpyxl.reader.excel import load_workbook

from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseSession
from vbv_lernwelt.importer.services import create_or_update_course_session
from vbv_lernwelt.importer.utils import calc_header_tuple_list_from_pyxl_sheet

test_dir = os.path.dirname(os.path.abspath(__file__))


class ImportCourseSessionTestCase(TestCase):
    def setUp(self):
        self.course = create_test_course(include_vv=False)

    def test_import_excel_file(self):
        workbook = load_workbook(
            filename=f"{test_dir}/Schulungen_Durchfuehrung_Trainer.xlsx"
        )
        sheet = workbook["Schulungen Durchführung"]

        tuple_list = calc_header_tuple_list_from_pyxl_sheet(sheet)
        for row in tuple_list:
            print(row)
            create_or_update_course_session(
                self.course, dict(row), language="de", circles=["Fahrzeug"]
            )

        self.assertEqual(CourseSession.objects.count(), 3)


class CreateOrUpdateCourseSessionTestCase(TestCase):
    def setUp(self):
        self.course = create_test_course(include_vv=False)

    def test_create_course_session(self):
        row = [
            ("ID", "DE 2023 A"),
            ("Generation", 2023),
            ("Region", "Deutschschweiz"),
            ("Klasse", "A"),
            ("Fahrzeug Start", "06.06.2023, 13:30"),
            ("Fahrzeug Ende", "06.06.2023, 15:00"),
            (
                "Fahrzeug Raum",
                "https://teams.microsoft.com/l/meetup-join/19%3ameeting_N2I5YzViZTQtYTM2Ny00OTYwLTgzNzAtYWI4OTQzODcxNTlj%40thread.v2/0?context=%7b%22Tid%22%3a%22fedd03c8-a756-4803-8f27-0db8f7c488f2%22%2c%22Oid%22%3a%22f92e6382-3884-4e71-a2fd-b305a75d9812%22%7d",
            ),
            ("Fahrzeug Standort", None),
            ("Fahrzeug Adresse", None),
        ]

        data = dict(row)

        cs = create_or_update_course_session(self.course, data, circles=["Fahrzeug"])

        self.assertEqual(cs.import_id, "DE 2023 A")
        self.assertEqual(cs.title, "Deutschschweiz 2023 A")
        self.assertEqual(cs.generation, "2023")
        self.assertEqual(cs.region, "Deutschschweiz")
        self.assertEqual(cs.group, "A")

        attendance_course = cs.attendance_courses[0]
        attendance_course = {
            k: v
            for k, v in attendance_course.items()
            if k not in ["learningContentId", "location"]
        }

        self.assertDictEqual(
            attendance_course,
            {
                "start": "2023-06-06T13:30:00",
                "end": "2023-06-06T15:00:00",
                "trainer": "",
            },
        )

    def test_update_course_session(self):
        cs = CourseSession.objects.create(
            course_id=self.course.id,
            title="Deutschschweiz 2023 A",
            import_id="DE 2023",
            group="A",
        )

        row = [
            ("ID", "DE 2023"),
            ("Generation", 2023),
            ("Region", "Deutschschweiz"),
            ("Klasse", "A"),
            ("Fahrzeug Start", "06.06.2023, 13:30"),
            ("Fahrzeug Ende", "06.06.2023, 15:00"),
            (
                "Fahrzeug Raum",
                "https://teams.microsoft.com/l/meetup-join/19%3ameeting_N2I5YzViZTQtYTM2Ny00OTYwLTgzNzAtYWI4OTQzODcxNTlj%40thread.v2/0?context=%7b%22Tid%22%3a%22fedd03c8-a756-4803-8f27-0db8f7c488f2%22%2c%22Oid%22%3a%22f92e6382-3884-4e71-a2fd-b305a75d9812%22%7d",
            ),
            ("Fahrzeug Standort", None),
            ("Fahrzeug Adresse", None),
        ]

        data = dict(row)

        cs = create_or_update_course_session(self.course, data, circles=["Fahrzeug"])

        self.assertEqual(1, CourseSession.objects.count())

        self.assertEqual(cs.import_id, "DE 2023")
        self.assertEqual(cs.title, "Deutschschweiz 2023 A")
        self.assertEqual(cs.generation, "2023")
        self.assertEqual(cs.region, "Deutschschweiz")
        self.assertEqual(cs.group, "A")

        attendance_course = cs.attendance_courses[0]
        attendance_course = {
            k: v
            for k, v in attendance_course.items()
            if k not in ["learningContentId", "location"]
        }

        self.assertDictEqual(
            attendance_course,
            {
                "start": "2023-06-06T13:30:00",
                "end": "2023-06-06T15:00:00",
                "trainer": "",
            },
        )
