import os
import time
import pytest

from addon.plugins.serienstream_plugin import SerienstreamPlugin


@pytest.mark.live
@pytest.mark.perf
def test_live_titel_staffel_episode_timing():
    if not os.getenv("LIVE_TESTS"):
        pytest.skip("LIVE_TESTS not set")

    title = os.getenv("LIVE_TITLE", "Star Trek: Starfleet Academy")
    season = os.getenv("LIVE_SEASON", "Staffel 1")

    max_title_to_season = float(os.getenv("PERF_MAX_TITLE_TO_SEASON", "6.0"))
    max_season_to_episodes = float(os.getenv("PERF_MAX_SEASON_TO_EPISODES", "5.0"))

    plugin = SerienstreamPlugin()

    t0 = time.perf_counter()
    seasons = plugin.seasons_for(title)
    t1 = time.perf_counter()

    assert seasons, f"Keine Staffeln für Titel gefunden: {title}"
    assert season in seasons, f"Gewünschte Staffel fehlt: {season}; vorhanden: {seasons}"

    episodes = plugin.episodes_for(title, season)
    t2 = time.perf_counter()

    assert episodes, f"Keine Episoden für {title} / {season}"

    title_to_season = t1 - t0
    season_to_episodes = t2 - t1

    print(
        f"PERF title->seasons={title_to_season:.3f}s "
        f"season->episodes={season_to_episodes:.3f}s "
        f"episodes={len(episodes)}"
    )

    assert title_to_season <= max_title_to_season, (
        f"title->seasons zu langsam: {title_to_season:.3f}s > {max_title_to_season:.3f}s"
    )
    assert season_to_episodes <= max_season_to_episodes, (
        f"season->episodes zu langsam: {season_to_episodes:.3f}s > {max_season_to_episodes:.3f}s"
    )
