"""Convert ASR DTOs to domain entities."""

from __future__ import annotations

from typing import TYPE_CHECKING

from noteflow.domain.entities import WordTiming

if TYPE_CHECKING:
    from noteflow.infrastructure.asr import dto
    from noteflow.infrastructure.asr.dto import AsrResult


class AsrConverter:
    """Convert ASR DTOs to domain entities."""

    @staticmethod
    def word_timing_to_domain(asr_word: dto.WordTiming) -> WordTiming:
        """Convert ASR WordTiming DTO to domain WordTiming entity.

        Map field names from ASR convention (start/end) to domain
        convention (start_time/end_time).

        Args:
            asr_word: ASR WordTiming DTO from faster-whisper engine.

        Returns:
            Domain WordTiming entity with validated timing.

        Raises:
            ValueError: If timing validation fails.
        """
        return WordTiming(
            word=asr_word.word,
            start_time=asr_word.start,
            end_time=asr_word.end,
            probability=asr_word.probability,
        )

    @staticmethod
    def result_to_domain_words(result: AsrResult) -> list[WordTiming]:
        """Convert all words from ASR result to domain entities.

        Args:
            result: ASR transcription result with word timings.

        Returns:
            List of domain WordTiming entities.
        """
        return [AsrConverter.word_timing_to_domain(word) for word in result.words]
