from pathlib import Path

import pytest

from pep_sphinx_extensions.pep_zero_generator import parser, writer


def test_pep_zero_writer_emit_text_newline():
    pep0_writer = writer.PEPZeroWriter()
    pep0_writer.emit_text("my text 1")
    pep0_writer.emit_newline()
    pep0_writer.emit_text("my text 2")

    assert pep0_writer.output == ["my text 1", "", "my text 2"]


def test_pep_zero_writer_emit_title():
    pep0_writer = writer.PEPZeroWriter()
    pep0_writer.emit_title("My Title")
    pep0_writer.emit_subtitle("My Subtitle")

    assert pep0_writer.output == [
        "My Title",
        "========",
        "",
        "My Subtitle",
        "-----------",
        "",
    ]


@pytest.mark.parametrize(
    ("test_input", "expected"),
    [
        (
            "pep-9000.rst",
            {
                "Francis Fussyreverend": "one@example.com",
                "Javier Soulfulcommodore": "two@example.com",
            },
        ),
        (
            "pep-9001.rst",
            {"Francis Fussyreverend": "", "Javier Soulfulcommodore": ""},
        ),
    ],
)
def test_verify_email_addresses(test_input, expected):
    # Arrange
    peps = [parser.PEP(Path(f"pep_sphinx_extensions/tests/peps/{test_input}"))]

    # Act
    out = writer._verify_email_addresses(peps)

    # Assert
    assert out == expected


def test_verify_email_addresses_multiple_emails():
    # Arrange
    peps = [
        parser.PEP(Path("pep_sphinx_extensions/tests/peps/pep-9000.rst")),
        parser.PEP(Path("pep_sphinx_extensions/tests/peps/pep-9003.rst")),
    ]

    # Act
    out = writer._verify_email_addresses(peps)

    # Assert: Francis has two emails combined, Javier's single email is not duplicated
    assert out == {
        "Francis Fussyreverend": "one@example.com, different@example.com",
        "Javier Soulfulcommodore": "two@example.com",
    }


def test_sort_authors():
    # Arrange
    authors_dict = {
        "Zebra, Zoë": "zoe@example.com",
        "lowercase, laurence": "laurence@example.com",
        "Aardvark, Alfred": "alfred@example.com",
    }

    # Act
    out = writer._sort_authors(authors_dict)

    # Assert
    assert out == ["Aardvark, Alfred", "lowercase, laurence", "Zebra, Zoë"]


@pytest.mark.parametrize(
    ("python_version", "expected"),
    [
        ("3.14", "     - :pep:`3.14 <745>`"),
        (
            "2.4, 2.5, 2.6",
            "     - :pep:`2.4 <320>`, :pep:`2.5 <356>`, :pep:`2.6 <361>`",
        ),
        ("2.4, 2.9", "     - :pep:`2.4 <320>`, 2.9"),
        ("1.5.2", "     - 1.5.2"),
        ("", "     - "),
    ],
)
def test_emit_pep_row_links_python_version_to_release_pep(
    python_version,
    expected,
):
    # Arrange
    release_peps = {
        "2.4": 320,
        "2.5": 356,
        "2.6": 361,
        "3.14": 745,
    }
    pep0_writer = writer.PEPZeroWriter(release_peps=release_peps)

    # Act
    pep0_writer.emit_pep_row(
        shorthand="Active",
        number=999,
        title="Test PEP",
        authors="Test Author",
        python_version=python_version,
    )

    # Assert
    assert expected in pep0_writer.output
