from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import pytest

from youtube_cli_agent.errors import ValidationError
from youtube_cli_agent.tools.transcription import TranscriptionTool


@dataclass
class FakeEngine:
    text: str

    def transcribe(self, audio_path: Path, language: str | None = None) -> str:
        return self.text


def test_transcription_tool_success(tmp_path: Path) -> None:
    audio = tmp_path / "audio.m4a"
    audio.write_bytes(b"not-empty")

    tool = TranscriptionTool(
        engine=FakeEngine(text="This is a deterministic transcript long enough for validation checks."),
        min_chars=20,
    )
    result = tool.run(audio)

    assert result.source == "Automatic Transcription"
    assert len(result.text) > 20


def test_transcription_tool_fails_short_text(tmp_path: Path) -> None:
    audio = tmp_path / "audio.m4a"
    audio.write_bytes(b"not-empty")

    tool = TranscriptionTool(engine=FakeEngine(text="short"), min_chars=20)
    with pytest.raises(ValidationError) as exc:
        tool.run(audio)

    assert exc.value.code == "TRANSCRIPT_TOO_SHORT"
