# Standard Library
from unittest.mock import patch

# Django
from django.test import TestCase

# Project
from django_easy_quiz.models import (
    InterpretationQuiz,
    InterpretationQuizAnswer,
    InterpretationQuizConclusion,
    InterpretationQuizQuestion,
    SavedQuiz,
)


class InterpretationQuizViewTests(TestCase):
    def _create_quiz(self):
        quiz = InterpretationQuiz.objects.create(
            name="Interpretation",
            description="<p>Desc</p>",
        )
        for sym, desc in zip(
            ("■", "▲", "◆", "●"),
            ("<p>C1</p>", "<p>C2</p>", "<p>C3</p>", "<p>C4</p>"),
        ):
            InterpretationQuizConclusion.objects.create(
                quiz=quiz, symbol=sym, description=desc
            )
        q1 = InterpretationQuizQuestion.objects.create(quiz=quiz, label="Q1")
        for order, (sym, lab) in enumerate(
            zip(("■", "▲", "◆", "●"), ("A1", "A2", "A3", "A4")), start=1
        ):
            InterpretationQuizAnswer.objects.create(
                question=q1, symbol=sym, label=lab, order=order
            )
        q2 = InterpretationQuizQuestion.objects.create(quiz=quiz, label="Q2")
        for order, (sym, lab) in enumerate(
            zip(("■", "▲", "◆", "●"), ("B1", "B2", "B3", "B4")), start=1
        ):
            InterpretationQuizAnswer.objects.create(
                question=q2, symbol=sym, label=lab, order=order
            )
        return quiz, q1, q2

    def test_post_majority_symbol_and_saved_quiz(self):
        quiz, q1, q2 = self._create_quiz()
        saved_quiz = SavedQuiz.objects.create(
            quiz_type="Interpretation", quiz_ended=False
        )
        a1 = q1.answers.get(symbol="▲")
        a2 = q2.answers.get(symbol="▲")

        with patch("django_easy_quiz.views.interpretation.SAVE_QUIZZES_RESULTS", True):
            response = self.client.post(
                f"/interpretation/{quiz.id}/",
                data={
                    f"question_{q1.id}": str(a1.id),
                    f"question_{q2.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.interpretation_max_symbol, "▲")
        self.assertEqual(saved_quiz.conclusion, "<p>C2</p>")

    def test_post_tie_keeps_first_answer_occurrence(self):
        quiz, q1, q2 = self._create_quiz()
        saved_quiz = SavedQuiz.objects.create(
            quiz_type="Interpretation", quiz_ended=False
        )
        a1_square = q1.answers.get(symbol="■")
        a2_triangle = q2.answers.get(symbol="▲")

        with patch("django_easy_quiz.views.interpretation.SAVE_QUIZZES_RESULTS", True):
            response = self.client.post(
                f"/interpretation/{quiz.id}/",
                data={
                    f"question_{q1.id}": str(a1_square.id),
                    f"question_{q2.id}": str(a2_triangle.id),
                    "saved_quiz": str(saved_quiz.id),
                },
            )

        self.assertEqual(response.status_code, 200)
        saved_quiz.refresh_from_db()
        self.assertEqual(saved_quiz.interpretation_max_symbol, "■")
        self.assertEqual(saved_quiz.conclusion, "<p>C1</p>")
