from autoshorts.rendering.static_captions import (
    WordCue,
    active_word_index_at,
    build_static_caption_segments,
    caption_state_at,
    segment_for_active_word,
)


def cues():
    words = "Your AI summary is useless because it has no decision target".split()
    return tuple(
        WordCue(start=i * 0.4, end=(i + 1) * 0.4, word=word, sentence="Your AI summary is useless because it has no decision target")
        for i, word in enumerate(words)
    )


def test_static_caption_segment_contains_multiple_words():
    segments = build_static_caption_segments(cues())

    assert segments
    assert all(len(segment.words) >= 3 for segment in segments)
    assert segments[0].text == "Your AI summary is useless because"


def test_active_word_changes_within_same_static_segment():
    word_cues = cues()
    segments = build_static_caption_segments(word_cues)

    early = caption_state_at(word_cues, segments, 0.1)
    later = caption_state_at(word_cues, segments, 1.1)

    assert early["segment_text"] == later["segment_text"]
    assert early["active_word"] == "Your"
    assert later["active_word"] == "summary"


def test_segment_text_remains_stable_while_highlight_moves_left_to_right():
    word_cues = cues()
    segments = build_static_caption_segments(word_cues)
    state_a = caption_state_at(word_cues, segments, 0.5)
    state_b = caption_state_at(word_cues, segments, 1.7)

    assert state_a["segment_words"] == state_b["segment_words"]
    assert state_a["active_word_index"] < state_b["active_word_index"]


def test_no_one_word_only_caption_mode_as_default_when_enough_words_exist():
    segments = build_static_caption_segments(cues())

    assert min(len(segment.words) for segment in segments) >= 3
