from autoshorts.rendering.static_captions import (
    CaptionLayoutConfig,
    WordCue,
    build_safe_caption_segments,
    layout_caption_segment,
)


def _cue_words(text: str):
    words = text.split()
    return tuple(
        WordCue(start=i * 0.3, end=(i + 1) * 0.3, word=word, sentence=text)
        for i, word in enumerate(words)
    )


def test_long_phrase_wraps_into_two_lines_within_safe_width():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, horizontal_margin_px=60)
    segment = build_safe_caption_segments(_cue_words("Before you pay check three important things"), layout=cfg)[0]

    layout = layout_caption_segment(segment.words, active_word=segment.words[0], config=cfg)

    assert 1 <= len(layout.lines) <= 2
    assert layout.width_px <= cfg.safe_width_px


def test_caption_and_active_word_highlight_never_exceed_safe_bounds():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, horizontal_margin_px=60)
    segment = build_safe_caption_segments(_cue_words("sender domain urgency language payment route"), layout=cfg)[0]

    for word in segment.words:
        layout = layout_caption_segment(segment.words, active_word=word, config=cfg)
        assert layout.left_px >= cfg.horizontal_margin_px
        assert layout.right_px <= cfg.video_width - cfg.horizontal_margin_px
        assert layout.active_word_box is not None
        assert layout.active_word_box[0] >= cfg.horizontal_margin_px
        assert layout.active_word_box[2] <= cfg.video_width - cfg.horizontal_margin_px


def test_no_word_clipped_at_left_or_right_edge():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, horizontal_margin_px=60)
    segment = build_safe_caption_segments(_cue_words("This invoice looks normal"), layout=cfg)[0]

    layout = layout_caption_segment(segment.words, active_word="invoice", config=cfg)

    assert all(box[0] >= cfg.horizontal_margin_px for box in layout.word_boxes)
    assert all(box[2] <= cfg.video_width - cfg.horizontal_margin_px for box in layout.word_boxes)


def test_max_two_lines_enforced_by_splitting_earlier():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, horizontal_margin_px=60)
    text = "Before you pay check sender domain urgency language and payment route carefully"
    segments = build_safe_caption_segments(_cue_words(text), layout=cfg)

    assert len(segments) > 1
    for segment in segments:
        layout = layout_caption_segment(segment.words, active_word=segment.words[0], config=cfg)
        assert len(layout.lines) <= 2
        assert layout.width_px <= cfg.safe_width_px


def test_mobile_bottom_safe_zone_respected():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, bottom_margin_px=260)
    segment = build_safe_caption_segments(_cue_words("Urgency is not proof"), layout=cfg)[0]

    layout = layout_caption_segment(segment.words, active_word="not", config=cfg)

    assert layout.bottom_px <= cfg.video_height - cfg.bottom_margin_px


def test_domain_strings_wrap_or_shrink_safely():
    cfg = CaptionLayoutConfig(video_width=1080, video_height=1920, horizontal_margin_px=60)
    segment = build_safe_caption_segments(_cue_words("verify example-company.com before paying"), layout=cfg)[0]

    layout = layout_caption_segment(segment.words, active_word="example-company.com", config=cfg)

    assert layout.width_px <= cfg.safe_width_px
    assert layout.right_px <= cfg.video_width - cfg.horizontal_margin_px
    assert layout.font_size_px >= cfg.min_font_size_px
