import csv

from django.test import TestCase

from vbv_lernwelt.core.admin import User
from vbv_lernwelt.core.constants import (
    TEST_STUDENT1_USER_ID,
    TEST_STUDENT2_USER_ID,
    TEST_TRAINER1_USER_ID,
)
from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.course.consts import COURSE_TEST_ID
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.course.models import CourseSession, CourseSessionUser
from vbv_lernwelt.edoniq_test.views import (
    fetch_course_session_all_users,
    fetch_course_session_users,
    generate_export_response,
)


class EdoniqUserExportTestCase(TestCase):
    def setUp(self) -> None:
        create_default_users()
        create_test_course(with_sessions=True)

        user1 = User.objects.get(id=TEST_STUDENT1_USER_ID)
        user1.additional_json_data = {
            "Lehrvertragsnummer": "23456",
            "Geburtsdatum": "01.01.1991",
        }
        user1.save()

        user2 = User.objects.get(id=TEST_STUDENT2_USER_ID)
        user2.additional_json_data = {
            "Firmenname": "Test AG",
            "Lehrvertragsnummer": "12345",
            "Geburtsdatum": "01.01.1990",
        }
        user2.save()

    def test_fetch_course_session_users(self):
        users = fetch_course_session_users([COURSE_TEST_ID], excluded_domains=[])
        self.assertEqual(len(users), 3)

    def test_fetch_course_session_trainers(self):
        users = fetch_course_session_users(
            [COURSE_TEST_ID], role=CourseSessionUser.Role.EXPERT, excluded_domains=[]
        )
        self.assertEqual(len(users), 2)

    def test_remove_eiger_versicherungen(self):
        user1 = User.objects.get(id=TEST_STUDENT1_USER_ID)
        user1.email = "some@eiger-versicherungen.ch"
        user1.save()
        users = fetch_course_session_users(
            [COURSE_TEST_ID], excluded_domains=["eiger-versicherungen.ch"]
        )
        self.assertEqual(len(users), 2)

    def test_export_students_and_trainers(self):
        users = fetch_course_session_all_users([COURSE_TEST_ID], excluded_domains=[])
        self.assertEqual(len(users), 5)

    def test_deduplicates_users(self):
        trainer1 = User.objects.get(id=TEST_TRAINER1_USER_ID)
        cs_zrh = CourseSession.objects.get(
            title="Test Zürich 2022 a",
        )
        _csu = CourseSessionUser.objects.create(
            course_session=cs_zrh,
            user=trainer1,
        )
        users = fetch_course_session_all_users([COURSE_TEST_ID], excluded_domains=[])
        self.assertEqual(len(users), 5)

    def test_response_csv(self):
        users = fetch_course_session_users([COURSE_TEST_ID], excluded_domains=[])
        response = generate_export_response(users)

        user_data = [
            [
                str(user.id),
                user.first_name,
                user.last_name,
                "m",
                user.language,
                user.email,
                "",
                "Lernende",
                user.additional_json_data.get("Firmenname", ""),
                user.additional_json_data.get("Lehrvertragsnummer", ""),
                user.coursesessionuser_set.first().course_session.import_id,
                "",
            ]
            for user in users
        ]

        expected_response_data = [
            [
                "\ufefflogin",
                "firstname",
                "lastname",
                "gender",
                "preferred_language",
                "email",
                "birthday",
                "group",
                "region",
                "division",
                "oe",
                "superior_login",
            ]
        ] + user_data

        # Perform assertions on the response
        self.assertEqual(response.status_code, 200)
        csv_data = response.content.decode("utf-8")
        reader = csv.reader(csv_data.splitlines())
        response_data = list(reader)
        self.assertEqual(response_data, expected_response_data)
