"""DEPRECATED - utterance.py

This module is deprecated. All transcription data now goes through
the new sessions router at /transcribe/sessions.

The old endpoints are kept for backwards compatibility but will be
removed in a future release. New integrations should use:
  POST /transcribe/sessions          - create session
  POST /transcribe/sessions/{id}/segments - batch upsert segments
"""

from fastapi import APIRouter, Request, HTTPException
import os
import json

router = APIRouter(deprecated=True)


@router.post("/handle_whisper_live_eos_utterance/{user_id}")
async def handle_whisper_live_eos_utterance(user_id: str, request: Request):
    """DEPRECATED - Use POST /transcribe/sessions instead."""
    data = await request.json()
    
    # Log deprecation warning
    print(f"[DEPRECATED] /handle_whisper_live_eos_utterance called for user {user_id}. "
          f"Please migrate to POST /transcribe/sessions/{{id}}/segments")
    
    # Keep writing to flat file for backwards compat
    user_dir = f"../../data/users/{user_id}/transcripts"
    if not os.path.exists(user_dir):
        os.makedirs(user_dir)

    log_file = os.path.join(user_dir, "utterances.log")
    with open(log_file, "a") as f:
        f.write(json.dumps(data) + "\n")

    return {"message": "Utterance logged (deprecated - migrate to sessions API)", "_deprecated": True}


@router.get("/get_utterances/{user_id}")
async def get_utterances(user_id: str):
    """DEPRECATED - Use GET /transcribe/sessions/{id}/segments instead."""
    print(f"[DEPRECATED] /get_utterances called for user {user_id}. "
          f"Please migrate to GET /transcribe/sessions/{{id}}/segments")
    
    user_dir = f"../../data/users/{user_id}/transcripts"
    log_file = os.path.join(user_dir, "utterances.log")
    if not os.path.exists(log_file):
        return {"utterances": [], "_deprecated": True}

    with open(log_file, "r") as f:
        utterances = [json.loads(line) for line in f]

    return {"utterances": utterances, "_deprecated": True}
