from __future__ import annotations
from pathlib import Path
import httpx
from deepgram import (
    DeepgramClient,
    DeepgramClientOptions,
    PrerecordedOptions,
    FileSource,
)
from config.settings import settings

class TranscriptionService:
    def __init__(self):
        if not settings.DEEPGRAM_API_KEY:
            raise ValueError("DEEPGRAM_API_KEY is not set in environment variables")
        
        # Initialize Deepgram client with options
        config = DeepgramClientOptions(
            verbose=False,  # Set to True for debugging
        )
        self.client = DeepgramClient(settings.DEEPGRAM_API_KEY, config)
        
    def transcribe_file(self, audio_path: str | Path) -> str:
        """
        Transcribe an audio file using Deepgram.
        
        Args:
            audio_path: Path to the audio file
            
        Returns:
            Transcribed text with proper formatting
        """
        try:
            print(f"Transcribing audio file: {audio_path}")
            
            # Read file into buffer
            with open(audio_path, "rb") as file:
                buffer_data = file.read()

            # Create payload
            payload: FileSource = {
                "buffer": buffer_data,
            }
            
            # Configure transcription options
            options = PrerecordedOptions(
                model="nova-2",
                smart_format=True,
                language="en-US",
                utterances=True,
                punctuate=True,
                diarize=True
            )
            
            # Transcribe with timeout
            response = self.client.listen.rest.v("1").transcribe_file(
                payload,
                options,
                timeout=httpx.Timeout(300.0, connect=10.0)
            )
            
            # Extract the transcript from the response
            transcript = response.results.channels[0].alternatives[0].transcript
            
            return transcript.strip()
            
        except Exception as e:
            raise Exception(f"Error transcribing with Deepgram: {str(e)}")