# Standard Library
from unittest.mock import patch

# Django
from django.test import TestCase

# Project
from django_easy_quiz.models import (
    SavedQuiz,
    WeightedAnswersQuiz,
    WeightedAnswersQuizAnswer,
    WeightedAnswersQuizConclusion,
    WeightedAnswersQuizQuestion,
)


class WeightedAnswersQuizViewTests(TestCase):
    def _create_quiz(self):
        quiz = WeightedAnswersQuiz.objects.create(
            name="Weighted",
            description="<p>Desc</p>",
            show_answers_corrections_text=True,
        )
        question = WeightedAnswersQuizQuestion.objects.create(
            quiz=quiz,
            label="Q1",
            multiple_answers=True,
            question_explanation_text="<p>Question info</p>",
        )
        a1 = WeightedAnswersQuizAnswer.objects.create(
            question=question, label="A1", points=2, order=1
        )
        WeightedAnswersQuizAnswer.objects.create(
            question=question, label="A2", points=3, order=2
        )
        WeightedAnswersQuizConclusion.objects.create(
            quiz=quiz,
            min_points=0,
            max_points=10,
            description="<p>Conclusion</p>",
        )
        return quiz, question, a1

    def test_post_saves_new_answers_format_and_points(self):
        quiz, question, a1 = self._create_quiz()
        a2 = question.answers.get(order=2)
        saved_quiz = SavedQuiz.objects.create(quiz_type="Weighted", quiz_ended=False)

        with patch("django_easy_quiz.views.weightedanswers.SAVE_QUIZZES_RESULTS", True):
            response = self.client.post(
                f"/weighted-answers/{quiz.id}/",
                data={
                    f"question_{question.id}": [str(a1.id), str(a2.id)],
                    "saved_quiz": str(saved_quiz.id),
                },
            )

        self.assertEqual(response.status_code, 200)
        saved_quiz.refresh_from_db()
        self.assertTrue(saved_quiz.quiz_ended)
        self.assertEqual(saved_quiz.conclusion["points"], 5)
        selected_labels = [
            a["label"] for a in saved_quiz.answers[0]["answers"] if a["selected"]
        ]
        self.assertEqual(selected_labels, ["A1", "A2"])
        self.assertEqual(
            saved_quiz.answers[0]["question_explanation_text"], "<p>Question info</p>"
        )

    def test_post_without_matching_conclusion_fallback(self):
        quiz = WeightedAnswersQuiz.objects.create(name="Weighted 2", description="Desc")
        question = WeightedAnswersQuizQuestion.objects.create(quiz=quiz, label="Q2")
        a1 = WeightedAnswersQuizAnswer.objects.create(
            question=question, label="A1", points=99, order=1
        )
        WeightedAnswersQuizAnswer.objects.create(
            question=question, label="A2", points=1, order=2
        )
        saved_quiz = SavedQuiz.objects.create(quiz_type="Weighted", quiz_ended=False)

        with patch("django_easy_quiz.views.weightedanswers.SAVE_QUIZZES_RESULTS", True):
            response = self.client.post(
                f"/weighted-answers/{quiz.id}/",
                data={
                    f"question_{question.id}": str(a1.id),
                    "saved_quiz": str(saved_quiz.id),
                },
            )

        self.assertEqual(response.status_code, 200)
        saved_quiz.refresh_from_db()
        self.assertEqual(saved_quiz.conclusion["points"], 99)
        self.assertEqual(saved_quiz.conclusion["description"], "")

    def test_get_summary_from_querystring(self):
        quiz, question, a1 = self._create_quiz()
        response = self.client.get(
            f"/weighted-answers/{quiz.id}/?question={question.id}.{a1.id}"
        )
        self.assertEqual(response.status_code, 200)
        first = response.context["quiz_answers"][0]["answers"][0]
        self.assertEqual(first["label"], "A1")
        self.assertTrue(first["selected"])
