import asyncio

from addon.plugins.filmpalast_plugin import FilmpalastPlugin, SearchHit


def _fake_hits(_query: str) -> list[SearchHit]:
    return [
        SearchHit(title="Star Trek S01E01 Pilot", url="https://filmpalast.to/stream/star-trek-s01e01"),
        SearchHit(title="Star Trek S01E02 Zweiter Kontakt", url="https://filmpalast.to/stream/star-trek-s01e02"),
        SearchHit(title="Ein Hund namens Palma", url="https://filmpalast.to/stream/ein-hund-namens-palma"),
    ]


def test_search_groups_series_and_movies(monkeypatch):
    plugin = FilmpalastPlugin()
    monkeypatch.setattr(plugin, "_search_hits", _fake_hits)

    titles = asyncio.run(plugin.search_titles("trek"))

    assert titles == ["Ein Hund namens Palma", "Star Trek"]


def test_series_seasons_and_episodes(monkeypatch):
    plugin = FilmpalastPlugin()
    monkeypatch.setattr(plugin, "_search_hits", _fake_hits)

    asyncio.run(plugin.search_titles("trek"))

    assert plugin.is_movie("Star Trek") is False
    assert plugin.seasons_for("Star Trek") == ["Staffel 1"]
    assert plugin.episodes_for("Star Trek", "Staffel 1") == [
        "Episode 1 - Pilot",
        "Episode 2 - Zweiter Kontakt",
    ]


def test_movie_path_stays_unchanged(monkeypatch):
    plugin = FilmpalastPlugin()
    monkeypatch.setattr(plugin, "_search_hits", _fake_hits)

    asyncio.run(plugin.search_titles("hund"))

    assert plugin.is_movie("Ein Hund namens Palma") is True
    assert plugin.seasons_for("Ein Hund namens Palma") == ["Film"]
    assert plugin.episodes_for("Ein Hund namens Palma", "Film") == ["Stream"]
