from pathlib import Path
import shutil

import pytest

from autoshorts.demo.truetrace_preview_packages import (
    TRUE_TRACE_TOP3_CANDIDATE_IDS,
    build_top_truetrace_preview_packages,
    render_top_truetrace_preview_summary,
)


pytestmark = pytest.mark.skipif(shutil.which("ffmpeg") is None, reason="ffmpeg is required for MP4 preview rendering")


def test_build_top_truetrace_preview_packages_renders_three_mp4_approval_packages(tmp_path):
    result = build_top_truetrace_preview_packages(output_dir=tmp_path, approval_language="de")

    assert result.batch_id == "truetrace-top3-preview-packages-001"
    assert [artifact.candidate.candidate_id for artifact in result.artifacts] == list(TRUE_TRACE_TOP3_CANDIDATE_IDS)
    assert len(result.artifacts) == 3
    assert result.side_effects == ("write_png", "write_concat", "call_ffmpeg", "write_mp4")
    assert result.external_calls == ("ffmpeg",)

    for artifact in result.artifacts:
        assert artifact.package.requires_human_approval is True
        assert artifact.package.approval_language == "de"
        assert artifact.package.final_video_language == "en"
        assert artifact.subtitle_track.renderer_owned is True
        assert artifact.subtitle_track.side_effects == ()
        assert artifact.subtitle_track.external_calls == ()
        assert artifact.subtitle_track.cues[0].start_ms == 0
        assert artifact.subtitle_track.cues[-1].end_ms == artifact.candidate.duration_seconds * 1000
        assert artifact.tts_plan.provider == "edge_tts"
        assert artifact.tts_plan.voice == "en-GB-RyanNeural"
        assert artifact.tts_plan.blocks_video_gpu is False
        assert artifact.package.side_effects == ()
        assert artifact.package.external_calls == ()
        assert Path(artifact.package.preview_video_path).exists()
        assert artifact.package.preview_video_path.endswith("preview.mp4")
        assert artifact.manual_posting_pack.requires_human_manual_upload is True
        assert artifact.manual_posting_pack.side_effects == ()
        assert artifact.manual_posting_pack.external_calls == ()
        assert artifact.manual_posting_pack.video_path == artifact.package.preview_video_path
        assert tuple(post.platform for post in artifact.manual_posting_pack.platform_posts) == ("youtube", "tiktok")


def test_build_top_truetrace_preview_packages_accepts_relative_output_dir(tmp_path, monkeypatch):
    monkeypatch.chdir(tmp_path)

    result = build_top_truetrace_preview_packages(output_dir="relative-previews", approval_language="de")

    for artifact in result.artifacts:
        assert Path(artifact.package.preview_video_path).is_absolute()
        assert Path(artifact.package.preview_video_path).exists()


def test_render_top_truetrace_preview_summary_is_telegram_safe_and_references_three_videos(tmp_path):
    result = build_top_truetrace_preview_packages(output_dir=tmp_path, approval_language="de")

    text = render_top_truetrace_preview_summary(result)

    assert "## TrueTrace Top-3 Preview Packages" in text
    assert "Packages: 3" in text
    assert "Approval language: de" in text
    assert "Final video language: en" in text
    assert text.count("Preview video:") == 3
    assert "Subtitle cues:" in text
    assert "TTS baseline: edge_tts / en-GB-RyanNeural" in text
    assert "Freigabe alle" in text
    assert "Manual upload pack prepared: yes" in text
    assert "publish" not in text.casefold()
    assert "post" not in text.casefold()
    assert len(text) < 4096
