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, CourseSessionUser
from vbv_lernwelt.importer.services import create_or_update_student
from vbv_lernwelt.importer.utils import calc_header_tuple_list_from_pyxl_sheet

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


class ImportStudentsTestCase(TestCase):
    def setUp(self):
        self.course = create_test_course(include_vv=False)
        self.course_session_a = CourseSession.objects.create(
            course=self.course,
            title="Deutschschweiz 2023 A",
            import_id="DE 2023 A",
            group="A",
        )

    def test_import_excel_file(self):
        workbook = load_workbook(filename=f"{test_dir}/Schulungen_Teilnehmende.xlsx")
        sheet = workbook.active

        tuple_list = calc_header_tuple_list_from_pyxl_sheet(sheet)
        for row in tuple_list:
            print(row)
            create_or_update_student(dict(row))

        self.assertEqual(CourseSessionUser.objects.count(), 15)


class CreateOrUpdateStudentTestCase(TestCase):
    def setUp(self):
        self.course = create_test_course(include_vv=False)
        self.course_session_a = CourseSession.objects.create(
            course=self.course,
            title="Deutschschweiz 2023 A",
            import_id="DE 2023 A",
            group="A",
        )

        self.user_dict = {
            "Name": "Rascher",
            "Vorname": "Barbara",
            "Email": "barbara.rascher@vbv-afa.ch",
            "Sprache": "de",
            "Durchführungen": "DE 2023 A",
            "Lehrvertragsnummer": "1234",
            "Tel. Privat": "079 593 83 43",
            "Geburtsdatum": "01.01.2000",
            "email_notification_categories": ["INFORMATION"],
        }

    def test_create_student(self):
        create_or_update_student(self.user_dict)

        self.assertEqual(
            CourseSessionUser.objects.filter(
                user__email=self.user_dict["Email"]
            ).count(),
            1,
        )

        csu = CourseSessionUser.objects.filter(
            course_session=self.course_session_a,
        ).first()

        self.assertEqual(csu.role, CourseSessionUser.Role.MEMBER)
        self.assertEqual(csu.user.email, self.user_dict["Email"])
        self.assertEqual(csu.user.additional_json_data, self.user_dict)

    def test_update_student(self):
        create_or_update_student(self.user_dict)

        self.user_dict["Email"] = "br@vbv.ch"
        create_or_update_student(self.user_dict)

        self.assertEqual(
            CourseSessionUser.objects.filter(
                user__email="barbara.rascher@vbv-afa.ch"
            ).count(),
            0,
        )

        self.assertEqual(
            CourseSessionUser.objects.filter(
                user__additional_json_data__Lehrvertragsnummer=self.user_dict[
                    "Lehrvertragsnummer"
                ]
            ).count(),
            1,
        )

        csu = CourseSessionUser.objects.filter(
            course_session=self.course_session_a,
        ).first()

        self.assertEqual(csu.user.additional_json_data, self.user_dict)
