"""Export operations mixin for NoteFlowClient."""

from __future__ import annotations

from typing import TYPE_CHECKING

import grpc

from noteflow.grpc._client_mixins.converters import export_format_to_proto
from noteflow.grpc._types import ExportResult
from noteflow.grpc.proto import noteflow_pb2
from noteflow.infrastructure.logging import get_logger

if TYPE_CHECKING:
    from noteflow.grpc._client_mixins.protocols import ClientHost

logger = get_logger(__name__)


class ExportClientMixin:
    """Mixin providing export operations for NoteFlowClient."""

    def export_transcript(
        self: ClientHost,
        meeting_id: str,
        format_name: str = "markdown",
    ) -> ExportResult | None:
        """Export meeting transcript.

        Args:
            meeting_id: Meeting ID.
            format_name: Export format (markdown, html).

        Returns:
            ExportResult or None if request fails.
        """
        if not self.stub:
            return None

        try:
            proto_format = export_format_to_proto(format_name)
            request = noteflow_pb2.ExportTranscriptRequest(
                meeting_id=meeting_id,
                format=proto_format,
            )
            response = self.stub.ExportTranscript(request)
            return ExportResult(
                content=response.content,
                format_name=response.format_name,
                file_extension=response.file_extension,
            )
        except grpc.RpcError as e:
            logger.error("Failed to export transcript: %s", e)
            return None
