import pytest

from add_ticket_hook.utils import parse_prefixes, parse_ticket, is_special_commit, valid_commit_message, alter_message, \
    format_commit_message

SOME_PREFIX = "PROJ-"


def test_parse_prefixes__single_prefix__convert_list():
    assert parse_prefixes(["PROJ-"]) == ["PROJ-"]


@pytest.mark.parametrize(
    "message", [
        "TEST-124: anothu",
        "TEST-124",
        "TEST-124ASHT",
        "TEST-124-ASHT",
        "some text TEST-124 lorem ipsum",
        "some text [TEST-124] lorem ipsum",
        "some_text_TEST-124_lorem_ipsum",
        "test TEST-124"
    ]
)
def test_parse_ticket__ticket_in_message__is_found(message):
    ticket = parse_ticket(text=message, prefixes=["TEST-"], noticket_sentinel="NOTICKET")
    assert ticket == "TEST-124"


@pytest.mark.parametrize(
    "message", [
        "TEST 124: anothu",
        "TEST_124",
        "lorem ipsum",
        "T E S T-124",
    ]
)
def test_parse_ticket__ticket_not_in_message__not_found(message):
    ticket = parse_ticket(text=message, prefixes=["TEST-"], noticket_sentinel="NOTICKET")
    assert not ticket


def test_parse_ticket__ticket_not_in_message_but_sentinel_exist__sentinel_returned():
    message = "lorem ipsum NOTICKET GARBAGE-123"
    ticket = parse_ticket(text=message, prefixes=["TEST-"], noticket_sentinel="NOTICKET")
    assert ticket == "NOTICKET"


def test_is_special_commit__revert__ret_true():
    assert is_special_commit('Revert "PROJ-456_ test"') is True


def test_is_special_commit__merge__ret_true():
    assert is_special_commit("Merge branch 'testos' into PROJ-456_test") is True


def test_is_special_commit__regular_msg__ret_false():
    assert is_special_commit("Lorem ipsum") is False


def test_valid_commit_message__valid_format__ret_true():
    assert valid_commit_message("PROJ-123", "[PROJ-123] lorem ipsum") is True


def test_valid_commit_message__no_ticket__ret_false():
    assert valid_commit_message("", f"test test lorem ipsum") is False


def test_format_commit_message__formats_valid_message__roundtrip():
    formatted_commit = format_commit_message("PROJ-123", "bla bla bla")
    assert valid_commit_message("PROJ-123", formatted_commit)


def test_format_commit_message():
    formatted_commit = format_commit_message("PROJ-123", "bla bla bla")
    assert formatted_commit == "[PROJ-123] bla bla bla"


def test_alter_message__ticket_in_branch__taken_from_branch():
    new_message = alter_message(
        message="lorem ipsum",
        branch_name="PROJ-123_more_text",
        prefixes=["PROJ-"]
    )
    assert new_message == "[PROJ-123] lorem ipsum"


def test_alter_message__ticket_in_commit__taken_from_commit():
    new_message = alter_message(
        message=f"PROJ-123 more text",
        branch_name="PROJ-456_more_text",
        prefixes=["PROJ-"]
    )
    assert new_message == "[PROJ-123] more text"


def test_alter_message__noticket_in_commit__noticket_prevail():
    new_message = alter_message(
        message=f"test NOJIRA more text",
        branch_name="PROJ-456_more_text",
        prefixes=["PROJ-"],
        noticket_sentinel='NOJIRA'
    )
    assert new_message == "[NOJIRA] test NOJIRA more text"