from __future__ import annotations

from datetime import datetime, timezone
from pathlib import Path

from youtube_cli_agent.models import TranscriptResult, VideoMetadata
from youtube_cli_agent.tools.markdown_persistence import MarkdownPersistenceTool


def make_metadata() -> VideoMetadata:
    return VideoMetadata(
        video_id="abc123",
        title="Webflow Automation: Start Here!",
        channel="No-Code Channel",
        url="https://www.youtube.com/watch?v=abc123",
        publish_date="2025-01-01",
        duration_seconds=125,
        description="description",
    )


def make_transcript() -> TranscriptResult:
    return TranscriptResult(
        source="Official Caption",
        language="en",
        text="This transcript is long enough for deterministic persistence validation and integrity checks.",
        generated_at=datetime(2025, 1, 1, tzinfo=timezone.utc),
    )


def test_markdown_persistence_writes_expected_file(tmp_path: Path) -> None:
    tool = MarkdownPersistenceTool(min_transcript_chars=20)

    result = tool.run(tmp_path, make_metadata(), make_transcript())

    assert result.skipped is False
    assert result.path.exists()
    text = result.path.read_text(encoding="utf-8")
    assert "# Webflow Automation: Start Here!" in text
    assert "- Transcript Source: Official Caption" in text
    assert "## Transcript" in text
