"""Format renderers: json / verbose_json / text / srt / vtt."""
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from app.formats import render  # noqa: E402

_META = {
    "duration": 5.0,
    "language": "en",
    "segments": [
        {"start": 0.0, "end": 1.5, "text": "Hello world"},
        {"start": 1.5, "end": 3.25, "text": "second line"},
    ],
}


def test_text_format():
    body, mtype = render("Hello world second line", _META, "text")
    assert body == "Hello world second line"
    assert "text/plain" in mtype


def test_json_minimal():
    import json
    body, mtype = render("hi", _META, "json")
    assert json.loads(body) == {"text": "hi"}
    assert "application/json" in mtype


def test_verbose_json_has_segments():
    import json
    body, _ = render("Hello world second line", _META, "verbose_json")
    data = json.loads(body)
    assert data["language"] == "en"
    assert data["duration"] == 5.0
    assert len(data["segments"]) == 2
    assert data["segments"][0]["id"] == 0
    assert data["segments"][1]["start"] == 1.5


def test_srt_timestamps_use_comma():
    body, _ = render("x", _META, "srt")
    assert "00:00:00,000 --> 00:00:01,500" in body
    assert "00:00:01,500 --> 00:00:03,250" in body
    assert body.startswith("1\n")


def test_vtt_header_and_dot_timestamps():
    body, _ = render("x", _META, "vtt")
    assert body.startswith("WEBVTT")
    assert "00:00:00.000 --> 00:00:01.500" in body


def test_unknown_format_falls_back_to_json():
    import json
    body, mtype = render("hi", _META, "bogus")
    assert json.loads(body) == {"text": "hi"}


def test_srt_hours_rollover():
    meta = {"segments": [{"start": 3661.5, "end": 3662.0, "text": "late"}]}
    body, _ = render("late", meta, "srt")
    assert "01:01:01,500 --> 01:01:02,000" in body
