import pytest
from unittest.mock import MagicMock
from datetime import datetime
from app.services.history_service import HistoryService
from app.models.entities import QuizEntity, UserAnswerEntity
from app.schemas.response import HistoryResultSchema, QuizHistoryResponse
from app.models.entities import AnswerItemEntity, QuestionItemEntity
import datetime
from bson import ObjectId


@pytest.fixture
def mock_quiz_repository():
    return MagicMock()


@pytest.fixture
def mock_answer_repository():
    return MagicMock()


@pytest.fixture
def history_service(mock_quiz_repository, mock_answer_repository):
    return HistoryService(
        quiz_repository=mock_quiz_repository, answer_repository=mock_answer_repository
    )


def test_get_history_by_user_id(
    history_service, mock_answer_repository, mock_quiz_repository
):
    mock_answers = [
        UserAnswerEntity(
            id="answer1",
            session_id="",
            quiz_id="6815da9f37a1ce472ba72819",
            user_id="user123",
            total_correct=8,
            total_score=80,
            answered_at=datetime.datetime(2024, 1, 1, 10, 30, 0),
            answers=[],
        )
    ]
    mock_quiz = [
        QuizEntity(
            _id=ObjectId("6815da9f37a1ce472ba72819"),
            subject_id="",
            title="Quiz Matematika",
            description="Soal dasar matematika",
            total_quiz=10,
            author_id="author1",
            date=datetime.datetime(2024, 2, 2, 14, 0, 0),
            question_listings=[],
        )
    ]

    mock_answer_repository.get_by_user.return_value = mock_answers
    mock_quiz_repository.get_by_ids.return_value = mock_quiz

    result = history_service.get_history_by_user_id("user123")

    assert len(result) == 1
    assert isinstance(result[0], HistoryResultSchema)
    assert result[0].quiz_id == "6815da9f37a1ce472ba72819"
    assert result[0].total_correct == 8
    assert result[0].total_question == 10


def test_get_history_by_user_id_no_data(history_service, mock_answer_repository):
    mock_answer_repository.get_by_user.return_value = []

    result = history_service.get_history_by_user_id("user123")

    assert result == []


def test_get_history_by_answer_id(
    history_service, mock_answer_repository, mock_quiz_repository
):
    mock_answer = UserAnswerEntity(
        id="answer1",
        session_id="",
        user_id="user1",
        quiz_id="quiz1",
        total_correct=7,
        total_score=70,
        answered_at=datetime.datetime(2024, 2, 2, 14, 0, 0),
        answers=[
            AnswerItemEntity(
                question_index=0,
                answer="B",
                is_correct=True,
                time_spent=12,
            )
        ],
    )

    mock_quiz = QuizEntity(
        id="quiz1",
        title="Quiz IPA",
        description="Ilmu Pengetahuan Alam",
        subject_id="subject1",
        date=datetime.datetime(2025, 5, 5, 0, 0),
        total_quiz=10,
        author_id="author1",
        question_listings=[
            QuestionItemEntity(
                index=0,
                question="Apa ibu kota Indonesia?",
                type="multiple_choice",
                target_answer="B",
                options=["A. Surabaya", "B. Jakarta", "C. Bandung"],
                duration=20,
            )
        ],
    )

    mock_answer_repository.get_by_id.return_value = mock_answer
    mock_quiz_repository.get_by_id.return_value = mock_quiz

    result = history_service.get_history_by_answer_id("answer1")

    assert isinstance(result, QuizHistoryResponse)
    assert result.total_correct == 7
    assert result.total_score == 70
    assert result.total_solve_time == 12
    assert len(result.question_listings) == 1
    assert result.question_listings[0].is_correct is True
    assert result.question_listings[0].user_answer == "B"
