from django.test import TestCase

from vbv_lernwelt.assignment.models import (
    AssignmentCompletion,
    AssignmentCompletionStatus,
    AssignmentType,
)
from vbv_lernwelt.course.creators.test_utils import (
    create_assignment,
    create_assignment_learning_content,
    create_circle,
    create_course,
    create_course_session,
    create_course_session_assignment,
    create_user,
)
from vbv_lernwelt.learning_mentor.content.praxis_assignment import (
    get_assignment_completions,
    get_praxis_assignments,
)
from vbv_lernwelt.learning_mentor.entities import MentorCompletionStatus


class AttendanceServicesTestCase(TestCase):
    def setUp(self):
        self.mentor = create_user("Mentor")
        self.user1 = create_user("Alpha")
        self.user2 = create_user("Beta")
        self.user3 = create_user("Kappa")
        self.user4 = create_user("Gamma")

        self.course, self.course_page = create_course("Test Course")
        self.course_session = create_course_session(course=self.course, title=":)")
        self.circle, _ = create_circle(title="Circle", course_page=self.course_page)

        self.assignment = create_assignment(
            course=self.course, assignment_type=AssignmentType.PRAXIS_ASSIGNMENT
        )

        AssignmentCompletion.objects.create(
            assignment_user=self.user1,
            course_session=self.course_session,
            assignment=self.assignment,
            completion_status=AssignmentCompletionStatus.EVALUATION_SUBMITTED.value,
            evaluation_user=self.mentor,
        )
        AssignmentCompletion.objects.create(
            assignment_user=self.user2,
            course_session=self.course_session,
            assignment=self.assignment,
            completion_status=AssignmentCompletionStatus.SUBMITTED.value,
            evaluation_user=self.mentor,
        )
        AssignmentCompletion.objects.create(
            assignment_user=self.user3,
            course_session=self.course_session,
            assignment=self.assignment,
            completion_status=AssignmentCompletionStatus.IN_PROGRESS.value,
            evaluation_user=self.mentor,
        )

    def test_assignment_completions(self):
        # GIVEN
        participants = [self.user1, self.user2, self.user3, self.user4]

        # WHEN
        results = get_assignment_completions(
            course_session=self.course_session,
            assignment=self.assignment,
            participants=participants,
            evaluation_user=self.mentor,
        )

        # THEN
        expected_order = ["Beta", "Alpha", "Gamma", "Kappa"]
        expected_statuses = {
            "Alpha": MentorCompletionStatus.EVALUATED,  # user1
            "Beta": MentorCompletionStatus.SUBMITTED,  # user2
            "Gamma": MentorCompletionStatus.UNKNOWN,  # user4 (no AssignmentCompletion)
            "Kappa": MentorCompletionStatus.UNKNOWN,  # user3 (IN_PROGRESS should be PENDING)
        }

        self.assertEqual(len(results), len(participants))
        for i, result in enumerate(results):
            self.assertEqual(result.last_name, expected_order[i])
            self.assertEqual(result.status, expected_statuses[result.last_name])
            self.assertEqual(
                result.url,
                f"/course/test-lehrgang/cockpit/assignment/{self.assignment.id}/{result.user_id}",
            )

    def test_praxis_assignment_status(self):
        # GIVEN
        lca = create_assignment_learning_content(self.circle, self.assignment)
        create_course_session_assignment(
            course_session=self.course_session, learning_content_assignment=lca
        )
        participants = [self.user1, self.user2, self.user3, self.user4]

        # WHEN
        assignments, circle_ids = get_praxis_assignments(
            course_session=self.course_session,
            participants=participants,
            evaluation_user=self.mentor,
        )

        # THEN
        assignment = assignments[0]
        self.assertEqual(assignment.pending_evaluations, 1)
        self.assertEqual(assignment.title, "Dummy Assignment (PRAXIS_ASSIGNMENT)")
        self.assertEqual(assignment.circle_id, self.circle.id)
        self.assertEqual(list(circle_ids)[0], self.circle.id)
