# Third party
from hypothesis import given
from hypothesis import strategies as st

# Project
from django_easy_quiz.exporters.saved_quiz_export import (
    _question_answer,
    _question_explanation,
    _sanitize_sheet_title_text,
    _sheet_title,
    _to_text,
)


@given(st.text())
def test_to_text_always_returns_string(value):
    assert isinstance(_to_text(value), str)


@given(st.text())
def test_sanitize_sheet_title_text_never_empty(value):
    sanitized = _sanitize_sheet_title_text(value)
    assert isinstance(sanitized, str)
    assert sanitized != ""


@given(st.integers(min_value=1, max_value=999999), st.text())
def test_sheet_title_respects_excel_constraints(quiz_id, display_name):
    title = _sheet_title(quiz_id, display_name)
    assert isinstance(title, str)
    assert 1 <= len(title) <= 31
    assert title.startswith(f"#{quiz_id} - ")


@given(
    st.lists(
        st.one_of(
            st.none(),
            st.text(),
            st.fixed_dictionaries(
                {
                    "label": st.text(),
                    "info": st.text(),
                }
            ),
        ),
        max_size=10,
    )
)
def test_question_answer_helpers_handle_legacy_and_new_formats(answers):
    payload = {"answers": answers}
    answer = _question_answer(payload)
    question_explanation = _question_explanation(payload)
    assert isinstance(answer, str)
    assert isinstance(question_explanation, str)
