import pytest

from srs2std.evaluate.decontextualize_context import (
    DecontextualizeContext,
    construct_context,
)
from srs2std.srs.instance import SRS, Section, Subsection
from srs2std.srs.structure import SectionTitle


@pytest.fixture
def section1():
    section_title = SectionTitle(number="1", name="Section 1")

    # consist of one sentence
    content = "This is section 1."

    return Section(title=section_title, content=content, subsections=[])


@pytest.fixture
def section2():
    section_title = SectionTitle(number="2", name="Section 2")

    # consist of two sentences
    content = """\
This is 1st in section 2.
This is 2nd in section 2.
"""

    return Section(title=section_title, content=content, subsections=[])


@pytest.fixture
def section3():
    section_title = SectionTitle(number="3", name="Section 3")

    # consist of one sentence
    content = "This is section 3."

    # has two subsections
    subsection1 = Subsection(
        header="## Subsection A",
        content="This is subsection A.",
        subsections=[],
    )
    subsection2 = Subsection(
        header="## Subsection B",
        content="This is subsection B.",
        subsections=[],
    )

    return Section(
        title=section_title, content=content, subsections=[subsection1, subsection2]
    )


def test_construct_context_section1(section1):
    srs_title = "SRS Title"

    expected = """
SRS Title
# 1 Section 1
This is section 1.
""".strip()

    assert expected == construct_context(srs_title, section1)


def test_construct_context_section2(section2):
    srs_title = "SRS Title"

    expected = """
SRS Title
# 2 Section 2
This is 1st in section 2.
This is 2nd in section 2.
""".strip()

    assert expected == construct_context(srs_title, section2)


def test_construct_context_section3(section3):
    srs_title = "SRS Title"

    expected = """
SRS Title
# 3 Section 3
This is section 3.
## Subsection A
This is subsection A.
## Subsection B
This is subsection B.
""".strip()

    assert expected == construct_context(srs_title, section3)


def test_generate_examples_multiple_sections(section1):
    srs_title = "SRS Title"
    srs = SRS(sections=[section1, section1])

    expected = [
        ("This is section 1.", construct_context(srs_title, section1)),
        ("This is section 1.", construct_context(srs_title, section1)),
    ]

    context = DecontextualizeContext(srs_title, srs)
    generator = context.generate_examples()

    for expected_example in expected:
        assert expected_example == next(generator)


def test_generate_examples_section_has_two_sentences(section2):
    srs_title = "SRS Title"
    srs = SRS(sections=[section2])

    expected = [
        ("This is 1st in section 2.", construct_context(srs_title, section2)),
        ("This is 2nd in section 2.", construct_context(srs_title, section2)),
    ]

    context = DecontextualizeContext(srs_title, srs)
    generator = context.generate_examples()

    for expected_example in expected:
        assert expected_example == next(generator)


def test_generate_examples_section_has_subsections(section3):
    srs_title = "SRS Title"
    srs = SRS(sections=[section3])

    expected = [
        ("This is section 3.", construct_context(srs_title, section3)),
        ("This is subsection A.", construct_context(srs_title, section3)),
        ("This is subsection B.", construct_context(srs_title, section3)),
    ]

    context = DecontextualizeContext(srs_title, srs)
    generator = context.generate_examples()

    for expected_example in expected:
        assert expected_example == next(generator)


def test_generate_examples_section_complex(section2, section3):
    srs_title = "SRS Title"
    srs = SRS(sections=[section2, section3])

    expected = [
        ("This is 1st in section 2.", construct_context(srs_title, section2)),
        ("This is 2nd in section 2.", construct_context(srs_title, section2)),
        ("This is section 3.", construct_context(srs_title, section3)),
        ("This is subsection A.", construct_context(srs_title, section3)),
        ("This is subsection B.", construct_context(srs_title, section3)),
    ]

    context = DecontextualizeContext(srs_title, srs)
    generator = context.generate_examples()

    for expected_example in expected:
        assert expected_example == next(generator)
