import os

from django.test import TestCase

from vbv_lernwelt.assignment.models import AssignmentCompletion
from vbv_lernwelt.core.constants import (
    TEST_STUDENT1_USER_ID,
    TEST_STUDENT2_USER_ID,
    TEST_STUDENT3_USER_ID,
)
from vbv_lernwelt.core.create_default_users import create_default_users
from vbv_lernwelt.course.creators.test_course import create_test_course
from vbv_lernwelt.edoniq_test.result_import.services import (
    create_edoniq_csv_test_result,
    EdoniqCsvTestResult,
    filter_relevant_rows,
    get_distinct_course_release_ids,
    upsert_edoniq_test_result,
)


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

    def test_get_distinct_release_ids(self):
        release_ids = get_distinct_course_release_ids()
        self.assertEqual(len(release_ids), 2)
        self.assertSetEqual(release_ids, {"1689096523730", "1691157696911"})

    def test_filter_relevant_rows(self):
        csv_file_path = os.path.dirname(__file__) + "/edoniq_pruefung_resultate.csv"
        with open(
            csv_file_path,
            "r",
            encoding="utf-8",
        ) as file:
            csv_data = file.read()

            relevant_rows = filter_relevant_rows(csv_data)
            self.assertEqual(len(relevant_rows), 3)
            print(relevant_rows)

            self.assertEqual(relevant_rows[0]["Benutzer Login"], TEST_STUDENT1_USER_ID)
            self.assertEqual(relevant_rows[1]["Benutzer Login"], TEST_STUDENT2_USER_ID)
            self.assertEqual(relevant_rows[2]["Benutzer Login"], TEST_STUDENT3_USER_ID)

            self.assertEqual(relevant_rows[0]["erreichte Punkte absolut"], "21.25")
            self.assertEqual(relevant_rows[0]["erreichbares Punktetotal absolut"], "29")
            self.assertEqual(
                relevant_rows[0]["Bestanden-/Nicht-Bestanden Status"], "Bestanden"
            )
            self.assertEqual(relevant_rows[0]["Zeitstempel Beginn"], "06.09.2023 18:48")

            self.assertEqual(relevant_rows[2]["erreichte Punkte absolut"], "0.00")
            self.assertEqual(relevant_rows[2]["erreichbares Punktetotal absolut"], "29")
            self.assertEqual(
                relevant_rows[2]["Bestanden-/Nicht-Bestanden Status"], "Begonnen"
            )

            edoniq_test_result_row = create_edoniq_csv_test_result(relevant_rows[0])
            self.assertEqual(edoniq_test_result_row.user_id, TEST_STUDENT1_USER_ID)
            self.assertEqual(edoniq_test_result_row.user_points, 21.25)
            self.assertEqual(edoniq_test_result_row.max_points, 29)
            self.assertEqual(edoniq_test_result_row.passed, True)
            self.assertEqual(
                edoniq_test_result_row.edoniq_course_release_id, "1689096523730"
            )

    def test_upsert_edoniq_test_results(self):
        row = EdoniqCsvTestResult(
            user_id=TEST_STUDENT1_USER_ID,
            edoniq_course_release_id="1689096523730",
            user_points=21.25,
            max_points=29,
            passed=True,
        )

        _ac, created = upsert_edoniq_test_result(row)
        self.assertTrue(created)

        _ac, created = upsert_edoniq_test_result(row)
        self.assertFalse(created)

        ac = AssignmentCompletion.objects.get(
            assignment_user_id=TEST_STUDENT1_USER_ID,
        )

        self.assertEqual(ac.evaluation_points, 21.25)
        self.assertEqual(ac.evaluation_max_points, 29)
        self.assertTrue(ac.evaluation_passed)
