from srs2std.srs.instance import parse_content_as_section, parse_md_as_srs
from srs2std.srs.structure import SectionTitle, TableOfContents


def test_section_with_nested_subsections():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A
This is content of A.

### Subsection A.1
This is content of A.1.
"""
    section = parse_content_as_section(section_title, section_content)

    assert section.subsections[0].header == "## Subsection A"
    assert section.subsections[0].content == "This is content of A."

    assert section.subsections[0].subsections[0].header == "### Subsection A.1"
    assert section.subsections[0].subsections[0].content == "This is content of A.1."


def test_section_with_parallel_subsections():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A
This is content of A.

## Subsection B
This is content of B.
"""
    section = parse_content_as_section(section_title, section_content)

    assert section.subsections[0].header == "## Subsection A"
    assert section.subsections[0].content == "This is content of A."

    assert section.subsections[1].header == "## Subsection B"
    assert section.subsections[1].content == "This is content of B."


def test_section_with_nested_and_parallel():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A
This is content of A.

### Subsection A.1
This is content of A.1.

## Subsection B
This is content of B.
"""
    section = parse_content_as_section(section_title, section_content)

    assert section.subsections[0].header == "## Subsection A"
    assert section.subsections[0].content == "This is content of A."

    assert section.subsections[0].subsections[0].header == "### Subsection A.1"
    assert section.subsections[0].subsections[0].content == "This is content of A.1."

    assert section.subsections[1].header == "## Subsection B"
    assert section.subsections[1].content == "This is content of B."


def test_na_propagation():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A

### Subsection A.1
[N/A]

### Subsection A.2
[NULL]
"""
    section = parse_content_as_section(section_title, section_content)
    assert section.to_str() == ""


def test_non_na_propagation():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A
This is content of A.

### Subsection A.1
[N/A]
"""

    expected = """\
This is content of A.
"""

    section = parse_content_as_section(section_title, section_content)
    assert section.to_str() == expected.strip()


def test_section_to_sentences():
    section_title = SectionTitle(number="1", name="Title")
    section_content = """\
## Subsection A
This is the first sentence in A.

### Subsection A.1
This is the first sentence in A.1.
This is the second sentence in A.1.

## Subsection B
This is the first sentence in B.
"""

    expected = [
        "This is the first sentence in A.",
        "This is the first sentence in A.1.",
        "This is the second sentence in A.1.",
        "This is the first sentence in B.",
    ]

    section = parse_content_as_section(section_title, section_content)
    assert section.to_sentences() == expected


def test_parse_md_as_srs():
    md_doc = """\
# 1 Introduction
This is the introduction.

## 1.1 Section
This is content of section.

## Subsection
This is content of subsection.

# 2 Another
[N/A]
"""

    srs = parse_md_as_srs(md_doc)

    expected_1 = """\
This is the introduction.
"""
    expected_1_1 = """\
This is content of section.
This is content of subsection.
"""
    expected_2 = """\
"""

    assert srs.sections[0].to_str() == expected_1.strip()
    assert srs.sections[1].to_str() == expected_1_1.strip()
    assert srs.sections[2].to_str() == expected_2.strip()


def test_parse_md_as_srs_with_toc():
    md_doc = """\
# 1 Introduction
This is the introduction.

## 1.1 Section
This is content of section.

## Subsection
This is content of subsection.

# 2 Another
[N/A]
"""
    toc = TableOfContents(
        titles=[
            SectionTitle(number="1", name="Introduction"),
            SectionTitle(number="2", name="Another"),
        ]
    )

    srs = parse_md_as_srs(md_doc, toc)

    expected_1 = """\
This is the introduction.
This is content of section.
This is content of subsection.
"""
    expected_2 = """\
"""

    assert srs.sections[0].to_str() == expected_1.strip()
    assert srs.sections[1].to_str() == expected_2.strip()
