from sqlalchemy.orm import Session
from ..database import Meeting, AnalysisResult
from .llm_service import call_gemini_api
from ..prompt_library import get_prompt
from typing import Dict, Any, List

def _format_transcript_for_translation(chunks: List[Any]) -> str:
    """Formats the transcript into a single string for the translation prompt."""
    full_transcript = []
    # Ensure chunks are treated correctly, whether they are dicts or objects
    for chunk in chunks:
        json_content = getattr(chunk, 'json_content', None)
        if not json_content:
            continue
        for line in json_content:
            speaker = line.get("speaker", "Unknown")
            text = line.get("text", "")
            full_transcript.append(f"{speaker}: {text}")
    return "\n".join(full_transcript)

def translate_transcript(db: Session, meeting_id: int, target_language: str) -> AnalysisResult:
    """
    Translates the transcript of a meeting and stores it.
    """
    meeting = db.query(Meeting).filter(Meeting.id == meeting_id).first()
    if not meeting:
        raise ValueError("Meeting not found")

    prompt_key = f"translation_{target_language.lower()}"

    # Check if translation already exists
    existing_translation = db.query(AnalysisResult).filter(
        AnalysisResult.meeting_id == meeting_id,
        AnalysisResult.prompt_key == prompt_key
    ).first()

    if existing_translation:
        return existing_translation

    # Prepare transcript
    transcript_text = _format_transcript_for_translation(meeting.chunks)
    if not transcript_text:
        raise ValueError("Transcript is empty, cannot translate.")

    # Get prompt from library
    prompt = get_prompt('translate_transcript', {
        'transcript': transcript_text,
        'target_language': target_language
    })

    # Call Gemini API using the new service function
    translated_text = call_gemini_api(prompt)

    # Store result
    translation_result = AnalysisResult(
        meeting_id=meeting_id,
        prompt_key=prompt_key,
        result_text=translated_text
    )
    db.add(translation_result)
    db.commit()
    db.refresh(translation_result)

    return translation_result
