# import pytest
# from datetime import datetime
# from app.services.quiz_service import QuizService
# from app.exception import DataNotFoundException, ValidationException
# from app.models.entities import QuizEntity, SubjectEntity, QuestionItemEntity
# from bson import ObjectId
# from unittest.mock import MagicMock


# @pytest.fixture
# def mock_repositories():
#     return {
#         "quiz_repository": MagicMock(),
#         "user_repository": MagicMock(),
#         "subject_repository": MagicMock(),
#     }


# @pytest.fixture
# def quiz_service(mock_repositories):
#     return QuizService(
#         quiz_repository=mock_repositories["quiz_repository"],
#         user_repository=mock_repositories["user_repository"],
#         subject_repository=mock_repositories["subject_repository"],
#     )


# def test_get_quiz_found(quiz_service, mock_repositories):
#     mock_quiz = QuizEntity(
#         id=ObjectId(),
#         author_id="user1",
#         subject_id="subj1",
#         title="Ulangan Harian",
#         description="Tes harian",
#         is_public=True,
#         date=datetime.now(),
#         total_quiz=2,
#         limit_duration=60,
#         total_user_playing=10,
#         question_listings=[],
#     )
#     mock_subject = SubjectEntity(
#         id=ObjectId(),
#         name="Matematika",
#         short_name="MTK",
#         description="Deskripsi MTK",
#         icon="math.png",
#     )

#     mock_repositories["quiz_repository"].get_by_id.return_value = mock_quiz
#     mock_repositories["subject_repository"].get_by_id.return_value = mock_subject

#     result = quiz_service.get_quiz("quiz123")
#     assert result.title == "Ulangan Harian"
#     mock_repositories["quiz_repository"].get_by_id.assert_called_once_with("quiz123")


# def test_get_quiz_not_found(quiz_service, mock_repositories):
#     mock_repositories["quiz_repository"].get_by_id.return_value = None
#     with pytest.raises(DataNotFoundException):
#         quiz_service.get_quiz("invalid_id")


# def test_create_quiz_valid(quiz_service, mock_repositories):
#     quiz_data = MagicMock()
#     quiz_data.question_listings = [
#         MagicMock(type="option", options=["a", "b", "c", "d"], duration=30),
#         MagicMock(type="true_false", duration=10),
#     ]
#     quiz_service.create_quiz(quiz_data)
#     assert mock_repositories["quiz_repository"].create.called


# def test_create_quiz_invalid_options(quiz_service):
#     quiz_data = MagicMock()
#     quiz_data.question_listings = [
#         MagicMock(type="option", options=["a", "b"], duration=30)
#     ]
#     with pytest.raises(ValidationException):
#         quiz_service.create_quiz(quiz_data)


# def test_search_quiz(quiz_service, mock_repositories):
#     quiz = QuizEntity(
#         id=ObjectId(),
#         author_id="user1",
#         subject_id="subj1",
#         title="Kuis Sejarah",
#         description=None,
#         is_public=True,
#         date=datetime.now(),
#         total_quiz=1,
#         limit_duration=30,
#         total_user_playing=0,
#         question_listings=[],
#     )
#     author = MagicMock()

#     mock_repositories["quiz_repository"].search_by_title_or_category.return_value = [
#         quiz
#     ]
#     mock_repositories["quiz_repository"].count_by_search.return_value = 1
#     mock_repositories["user_repository"].get_user_by_id.return_value = author

#     quizzes, total = quiz_service.search_quiz("sejarah", "subj1")
#     assert total == 1
#     assert len(quizzes) == 1


# def test_get_user_quiz_empty(quiz_service, mock_repositories):
#     mock_repositories["quiz_repository"].get_by_user_id.return_value = []
#     result = quiz_service.get_user_quiz("user1")
#     assert result.total == 0
#     assert result.quizzes == []


# def test_get_quiz_recommendation_found(quiz_service, mock_repositories):
#     quiz = QuizEntity(
#         id=ObjectId(),
#         author_id="user1",
#         subject_id="subj1",
#         title="Top Quiz",
#         description=None,
#         is_public=True,
#         date=datetime.now(),
#         total_quiz=1,
#         limit_duration=30,
#         total_user_playing=100,
#         question_listings=[],
#     )
#     mock_repositories["quiz_repository"].get_top_played_quizzes.return_value = [quiz]
#     mock_repositories["user_repository"].get_user_by_id.return_value = MagicMock()

#     result = quiz_service.get_quiz_recommendation(1, 5)
#     assert len(result) == 1


# def test_get_quiz_recommendation_not_found(quiz_service, mock_repositories):
#     mock_repositories["quiz_repository"].get_top_played_quizzes.return_value = []
#     with pytest.raises(DataNotFoundException):
#         quiz_service.get_quiz_recommendation(1, 5)


# def test_update_quiz(quiz_service, mock_repositories):
#     mock_repositories["quiz_repository"].update.return_value = True
#     result = quiz_service.update_quiz("quiz123", {"title": "Updated Title"})
#     assert result is True
#     mock_repositories["quiz_repository"].update.assert_called_once_with(
#         "quiz123", {"title": "Updated Title"}
#     )


# def test_delete_quiz(quiz_service, mock_repositories):
#     mock_repositories["quiz_repository"].delete.return_value = True
#     result = quiz_service.delete_quiz("quiz123")
#     assert result is True
#     mock_repositories["quiz_repository"].delete.assert_called_once_with("quiz123")
