from app.services.audio_loader import load_audio
from app.services.transcription import transcribe
from app.db.database import SessionLocal
from app.db.models import CallRecord

def process_call(audio_path: str):
    db = SessionLocal()

    try:
        wav_path = load_audio(audio_path)
        result = transcribe(wav_path)

        record = CallRecord(
            original_audio_path=audio_path,
            processed_audio_path=wav_path,
            transcription_text=result["text"],
            language=result["language"]
        )

        db.add(record)
        db.commit()
        db.refresh(record)

        return {
            "id": record.id,
            "text": record.transcription_text,
            "language": record.language
        }

    finally:
        db.close()
