"""
Transcriber Agent for CrewAI workflow.
"""
from crewai import Agent, Task
from utils.speech_processing import YouTubeTranscriber
from typing import Dict, Any

class TranscriberAgent:
    """Agent responsible for transcribing YouTube videos."""
    
    def __init__(self, perplexity_llm):
        """
        Initialize the transcriber agent.
        
        Args:
            perplexity_llm: Configured LLM for CrewAI
        """
        self.youtube_transcriber = YouTubeTranscriber()
        self.agent = self._create_agent(perplexity_llm)
    
    def _create_agent(self, llm) -> Agent:
        """Create the CrewAI agent for transcription."""
        return Agent(
            role='YouTube Transcriber',
            goal='Extract audio from YouTube videos and generate accurate transcriptions',
            backstory="""You are an expert speech recognition specialist with advanced 
            capabilities in audio processing and transcription. You excel at extracting 
            clear audio from YouTube videos and converting speech to text with high 
            accuracy. Your expertise includes handling various audio qualities, accents, 
            and speaking styles.""",
            verbose=True,
            allow_delegation=False
        )
    
    def create_transcription_task(self, youtube_url: str) -> Task:
        """
        Create a transcription task for a YouTube video.
        
        Args:
            youtube_url: The YouTube video URL to transcribe
            
        Returns:
            CrewAI Task for transcription
        """
        return Task(
            description=f"""
            Transcribe the YouTube video located at: {youtube_url}
            
            Your task is to:
            1. Extract the audio from the YouTube video
            2. Use Whisper AI to transcribe the audio to text
            3. Return the complete transcript
            4. Ensure the transcript captures all spoken content accurately
            
            Return only the transcribed text without any additional formatting or comments.
            """,
            expected_output="Complete transcript of the YouTube video as plain text",
            agent=self.agent
        )
    
    def transcribe(self, youtube_url: str) -> str:
        """
        Transcribe a YouTube video.
        
        Args:
            youtube_url: URL of the YouTube video
            
        Returns:
            Transcribed text
        """
        try:
            return self.youtube_transcriber.transcribe_youtube_video(youtube_url)
        except Exception as e:
            return f"Error transcribing video: {str(e)}"


