from datetime import timedelta

from django.utils import timezone
from graphene_django.utils import GraphQLTestCase

from vbv_lernwelt.course.creators.test_utils import (
    add_course_session_group_supervisor,
    add_course_session_user,
    create_attendance_course,
    create_circle,
    create_course,
    create_course_session,
    create_course_session_group,
    create_user,
)
from vbv_lernwelt.course.models import CourseSessionUser
from vbv_lernwelt.course_session.services.attendance import AttendanceUserStatus


class DashboardAttendanceTestCase(GraphQLTestCase):
    GRAPHQL_URL = "/server/graphql/"

    def test_attendance_day_presences(self):
        # GIVEN
        course, course_page = create_course("Test Course")
        course_session = create_course_session(course=course, title="Test Bern 2022 a")

        supervisor = create_user("supervisor")

        group = create_course_session_group(course_session=course_session)
        add_course_session_group_supervisor(group=group, user=supervisor)

        circle, _ = create_circle(title="Test Circle", course_page=course_page)

        m1 = create_user("member_1")
        add_course_session_user(
            course_session=course_session,
            user=m1,
            role=CourseSessionUser.Role.MEMBER,
        )

        m2 = create_user("member_2")
        add_course_session_user(
            course_session=course_session,
            user=m2,
            role=CourseSessionUser.Role.MEMBER,
        )

        m3 = create_user("member_3")
        add_course_session_user(
            course_session=course_session,
            user=m3,
            role=CourseSessionUser.Role.MEMBER,
        )

        e1 = create_user("expert_1")
        add_course_session_user(
            course_session=course_session,
            user=e1,
            role=CourseSessionUser.Role.EXPERT,
        )

        attendance_user_list = [
            {"user_id": str(m1.id), "status": AttendanceUserStatus.PRESENT.value},
            {"user_id": str(m2.id), "status": AttendanceUserStatus.ABSENT.value},
        ]

        due_date_end = timezone.now() - timedelta(hours=2)
        attendance_course = create_attendance_course(
            course_session=course_session,
            circle=circle,
            attendance_user_list=attendance_user_list,
            due_date_end=due_date_end,
        )

        self.client.force_login(supervisor)

        query = f"""
                query($course_id: ID!) {{
                    course_statistics(course_id: $course_id) {{
                        attendance_day_presences{{
                            summary{{
                                days_completed
                                participants_present
                            }}
                            records{{
                                course_session_id
                                generation
                                circle_id
                                due_date
                                participants_present
                                participants_total
                                details_url
                            }}
                        }}
                    }}
                }}
                """

        # WHEN
        response = self.query(query, variables={"course_id": str(course.id)})

        self.assertResponseNoErrors(response)

        data = response.json()["data"]

        attendance_day_presences = data["course_statistics"]["attendance_day_presences"]

        record = attendance_day_presences["records"][0]

        self.assertEqual(record["course_session_id"], str(course_session.id))
        self.assertEqual(record["generation"], "2023")
        self.assertEqual(record["participants_present"], 1)
        self.assertEqual(record["participants_total"], 3)
        self.assertEqual(
            record["details_url"],
            f"/course/test-lehrgang/cockpit/attendance?id={attendance_course.learning_content.id}"
            f"&courseSessionId={course_session.id}",
        )

        summary = attendance_day_presences["summary"]
        self.assertEqual(summary["days_completed"], 1)
        self.assertEqual(summary["participants_present"], 34)
