from datetime import datetime

import djclick as click
from django.utils import timezone

from vbv_lernwelt.assignment.models import Assignment, AssignmentCompletion
from vbv_lernwelt.competence.models import PerformanceCriteria
from vbv_lernwelt.core.constants import (
    TEST_COURSE_SESSION_BERN_ID,
    TEST_MENTOR1_USER_ID,
    TEST_STUDENT1_USER_ID,
    TEST_STUDENT1_VV_USER_ID,
    TEST_STUDENT2_USER_ID,
    TEST_STUDENT3_USER_ID,
    TEST_TRAINER1_USER_ID,
)
from vbv_lernwelt.core.models import Organisation, User
from vbv_lernwelt.course.consts import (
    COURSE_TEST_ID,
    COURSE_VERSICHERUNGSVERMITTLERIN_ID,
)
from vbv_lernwelt.course.creators.test_course import (
    create_edoniq_test_result_data,
    create_feedback_response_data,
    create_test_assignment_evaluation_data,
    create_test_assignment_submitted_data,
)
from vbv_lernwelt.course.models import (
    Course,
    CourseCompletion,
    CourseCompletionStatus,
    CourseSession,
    CourseSessionUser,
)
from vbv_lernwelt.course.services import mark_course_completion
from vbv_lernwelt.course_session.models import CourseSessionAttendanceCourse
from vbv_lernwelt.course_session.services.attendance import AttendanceUserStatus
from vbv_lernwelt.feedback.models import FeedbackResponse
from vbv_lernwelt.learning_mentor.models import LearningMentor
from vbv_lernwelt.learnpath.models import (
    LearningContentAttendanceCourse,
    LearningContentFeedbackUK,
    LearningContentFeedbackVV,
)
from vbv_lernwelt.notify.models import Notification
from vbv_lernwelt.self_evaluation_feedback.models import (
    CourseCompletionFeedback,
    SelfEvaluationFeedback,
)


@click.command()
@click.option(
    "--create-assignment-completion/--no-create-assignment-completion",
    default=False,
    help="will create assignment completion data for test-student1@example.com",
)
@click.option(
    "--create-assignment-evaluation/--no-create-assignment-evaluation",
    default=False,
    help="will create assignment evaluation data for test-student1@example.com",
)
@click.option(
    "--assignment-evaluation-scores",
    default=None,
    help="Provide assignment evaluation scores in the format: 6,6,6,3,3",
)
@click.option(
    "--create-edoniq-test-results",
    type=(int, int),
    default=(None, None),
    metavar="USER_POINTS MAX_POINTS",
    help="Create edoniq result data for test-student1@example.com with user points and max points",
)
@click.option(
    "--create-feedback-responses/--no-create-feedback-responses",
    default=False,
    help="will create feedback response data",
)
@click.option(
    "--create-course-completion-performance-criteria/--no-create-course-completion-performance-criteria",
    default=False,
    help="will create course completion performance criteria data",
)
@click.option(
    "--create-attendance-days/--no-create-attendance-days",
    default=False,
    help="will create attendance days data",
)
@click.option(
    "--enable-circle-documents/--no-enable-circle-documents",
    default=True,
    help="will enable circle documents for test course",
)
@click.option(
    "--create-learning-mentor/--no--create-learning-mentor",
    default=False,
    help="Will create a learning mentor for test user",
)
def command(
    create_assignment_completion,
    create_assignment_evaluation,
    assignment_evaluation_scores,
    create_edoniq_test_results,
    create_feedback_responses,
    create_course_completion_performance_criteria,
    create_attendance_days,
    enable_circle_documents,
    create_learning_mentor,
):
    print("cypress reset data")
    CourseCompletion.objects.all().delete()
    Notification.objects.all().delete()
    AssignmentCompletion.objects.all().delete()
    FeedbackResponse.objects.all().delete()
    CourseSessionAttendanceCourse.objects.all().update(attendance_user_list=[])

    SelfEvaluationFeedback.objects.all().delete()
    CourseCompletionFeedback.objects.all().delete()

    LearningMentor.objects.all().delete()
    User.objects.all().update(organisation=Organisation.objects.first())
    User.objects.all().update(language="de")
    User.objects.all().update(additional_json_data={})

    if create_assignment_completion or create_assignment_evaluation:
        print("create assignment completion data for test course")
        create_test_assignment_submitted_data(
            assignment=Assignment.objects.get(
                slug="test-lehrgang-assignment-überprüfen-einer-motorfahrzeugs-versicherungspolice"
            ),
            course_session=CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID),
            user=User.objects.get(id=TEST_STUDENT1_USER_ID),
        )
        create_test_assignment_submitted_data(
            assignment=Assignment.objects.get(
                slug="test-lehrgang-assignment-mein-kundenstamm"
            ),
            course_session=CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID),
            user=User.objects.get(id=TEST_STUDENT1_USER_ID),
        )
    if create_assignment_evaluation:
        if not assignment_evaluation_scores:
            assignment_evaluation_scores = [6, 6, 6, 3, 3]
        else:
            assignment_evaluation_scores = tuple(
                map(int, assignment_evaluation_scores.split(","))
            )
        print(
            "create assignment evaluation data for test course",
            assignment_evaluation_scores,
        )
        create_test_assignment_evaluation_data(
            assignment=Assignment.objects.get(
                slug="test-lehrgang-assignment-überprüfen-einer-motorfahrzeugs-versicherungspolice"
            ),
            course_session=CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID),
            assignment_user=User.objects.get(id=TEST_STUDENT1_USER_ID),
            evaluation_user=User.objects.get(id=TEST_TRAINER1_USER_ID),
            input_scores=assignment_evaluation_scores,
        )

    user_points, max_points = create_edoniq_test_results
    if user_points is not None and max_points is not None:
        print(
            f"Create edoniq test results: User Points: {user_points}, Max Points: {max_points}"
        )
        create_edoniq_test_result_data(
            assignment=Assignment.objects.get(
                slug="test-lehrgang-assignment-edoniq-wissens-und-verständisfragen-circle-fahrzeug-demo"
            ),
            course_session=CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID),
            assignment_user=User.objects.get(id=TEST_STUDENT1_USER_ID),
            user_points=user_points,
            max_points=max_points,
        )

    if create_feedback_responses:
        print("create_feedback_responses")
        course_session = CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID)

        # feedback fahrzeug
        learning_content_feedback_page = LearningContentFeedbackUK.objects.get(
            slug="test-lehrgang-lp-circle-fahrzeug-lc-feedback"
        )
        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT1_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 4,
                "goal_attainment": 3,
                "proficiency": 80,
                "preparation_task_clarity": True,
                "instructor_competence": 4,
                "instructor_respect": 4,
                "instructor_open_feedback": "Super Kurs!",
                "would_recommend": True,
                "course_negative_feedback": "Nichts Schlechtes",
                "course_positive_feedback": "Nur Gutes.",
                "feedback_type": "uk",
            },
        )

        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT2_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 4,
                "goal_attainment": 4,
                "proficiency": 100,
                "preparation_task_clarity": True,
                "instructor_competence": 3,
                "instructor_respect": 3,
                "instructor_open_feedback": "Super, bin begeistert",
                "would_recommend": True,
                "course_negative_feedback": "Es wäre praktisch, Zugang zu einer FAQ zu haben.",
                "course_positive_feedback": "Das Beispiel mit der Katze fand ich sehr gut veranschaulicht!",
                "feedback_type": "uk",
            },
        )

        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT3_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 2,
                "goal_attainment": 2,
                "proficiency": 40,
                "preparation_task_clarity": True,
                "instructor_competence": 1,
                "instructor_respect": 2,
                "instructor_open_feedback": "Ok, entspricht den Erwartungen",
                "would_recommend": False,
                "course_negative_feedback": "Mehr Videos wären schön.",
                "course_positive_feedback": "Die Präsentation war super",
                "feedback_type": "uk",
            },
        )

        # feedback reisen
        learning_content_feedback_page = LearningContentFeedbackVV.objects.get(
            slug="test-lehrgang-lp-circle-reisen-lc-feedback"
        )
        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT1_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 4,
                "goal_attainment": 3,
                "proficiency": 80,
                "preparation_task_clarity": True,
                "would_recommend": True,
                "course_negative_feedback": "Nichts Schlechtes",
                "course_positive_feedback": "Nur Gutes.",
                "feedback_type": "vv",
            },
        )

        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT2_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 4,
                "goal_attainment": 4,
                "proficiency": 100,
                "preparation_task_clarity": True,
                "would_recommend": True,
                "course_negative_feedback": "Es wäre praktisch, Zugang zu einer FAQ zu haben.",
                "course_positive_feedback": "Das Beispiel mit der Katze fand ich sehr gut veranschaulicht!",
                "feedback_type": "vv",
            },
        )

        create_feedback_response_data(
            feedback_user=User.objects.get(id=TEST_STUDENT3_USER_ID),
            course_session=course_session,
            learning_content_feedback_page=learning_content_feedback_page,
            submitted=True,
            feedback_data={
                "satisfaction": 2,
                "goal_attainment": 2,
                "proficiency": 40,
                "preparation_task_clarity": True,
                "would_recommend": False,
                "course_negative_feedback": "Mehr Videos wären schön.",
                "course_positive_feedback": "Die Präsentation war super",
                "feedback_type": "vv",
            },
        )

    if create_course_completion_performance_criteria:
        member = User.objects.get(id=TEST_STUDENT1_USER_ID)
        course_session = CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID)
        page = PerformanceCriteria.objects.get(
            slug="test-lehrgang-competencenavi-competences-crit-x11-allgemein"
        )

        mark_course_completion(
            page=page,
            user=member,
            course_session=course_session,
            completion_status=CourseCompletionStatus.SUCCESS.value,
        )

    if create_attendance_days:
        course_session = CourseSession.objects.get(id=TEST_COURSE_SESSION_BERN_ID)

        attendance_user_list = [
            {
                "user_id": TEST_STUDENT1_USER_ID,
                "status": AttendanceUserStatus.PRESENT.value,
            },
            {
                "user_id": TEST_STUDENT2_USER_ID,
                "status": AttendanceUserStatus.ABSENT.value,
            },
        ]

        attendance_course = CourseSessionAttendanceCourse.objects.get(
            course_session=course_session,
            learning_content=LearningContentAttendanceCourse.objects.get(
                slug="test-lehrgang-lp-circle-fahrzeug-lc-präsenzkurs-fahrzeug"
            ),
        )

        attendance_course.attendance_user_list = attendance_user_list
        attendance_course.due_date.start = timezone.make_aware(
            datetime(year=2000, month=10, day=31, hour=8)
        )
        attendance_course.due_date.end = timezone.make_aware(
            datetime(year=2000, month=10, day=31, hour=11)
        )
        attendance_course.save()

    if create_learning_mentor:
        uk_mentor = LearningMentor.objects.create(
            course=Course.objects.get(id=COURSE_TEST_ID),
            mentor=User.objects.get(id=TEST_MENTOR1_USER_ID),
        )
        uk_mentor.participants.add(
            CourseSessionUser.objects.get(
                user__id=TEST_STUDENT1_USER_ID,
                course_session=CourseSession.objects.get(
                    id=TEST_COURSE_SESSION_BERN_ID
                ),
            )
        )

        vv_course = Course.objects.get(id=COURSE_VERSICHERUNGSVERMITTLERIN_ID)
        vv_course_session = CourseSession.objects.get(course=vv_course)
        vv_mentor = LearningMentor.objects.create(
            course=vv_course,
            mentor=User.objects.get(id=TEST_MENTOR1_USER_ID),
        )
        vv_mentor.participants.add(
            CourseSessionUser.objects.get(
                user__id=TEST_STUDENT1_VV_USER_ID, course_session=vv_course_session
            )
        )

    course = Course.objects.get(id=COURSE_TEST_ID)
    course.enable_circle_documents = enable_circle_documents
    course.save()
