from sqlalchemy import Column, Integer, String, Boolean, Float, DateTime, Index, Text
from sqlalchemy.sql import func
from database import Base


class VoiceTranscription(Base):
    """One row per transcription attempt, success or failure.

    Deliberately NOT stored in `messages`:
      * `Message` has no generic metadata column, so reuse would mean an ALTER
        plus a `save_message` signature change on the hottest write path;
      * transcription is stateless relative to conversations — it can happen
        before a conversation exists, or for a dictation the user discards.
        Forcing a `messages` row would fabricate turns that never happened.

    `conversation_id` is a plain String with NO ForeignKey, for that same
    reason (and note TokenUsageLog.conversation_id IS a real FK — passing an
    unpersisted id there fails the constraint, and record_usage swallows the
    IntegrityError, losing the row silently. Pass None there.)

    Failures are recorded too: "the ASR was down for 20 minutes" is exactly the
    thing whose absence is indistinguishable from "logging broke".

    Audio bytes are NEVER persisted, here or anywhere else.
    """

    __tablename__ = "voice_transcriptions"

    id = Column(Integer, primary_key=True, index=True)

    # Who
    user_id = Column(Integer, nullable=False, index=True)
    company_id = Column(Integer, nullable=True, index=True)
    role_id = Column(Integer, nullable=True)
    conversation_id = Column(String, nullable=True)  # intentionally not a FK

    # Input facts
    source_mime = Column(String, nullable=True)
    source_bytes = Column(Integer, nullable=True)
    sniffed_container = Column(String, nullable=True)
    audio_seconds = Column(Float, nullable=True)
    transport = Column(String, nullable=True)  # "http" | "ws"

    # Pipeline facts
    preprocessed = Column(Boolean, nullable=False, default=False)
    filter_chain = Column(String, nullable=True)
    preprocess_ms = Column(Integer, nullable=True)
    asr_ms = Column(Integer, nullable=True)
    total_ms = Column(Integer, nullable=True)

    # Result facts
    language = Column(String, nullable=True)
    char_count = Column(Integer, nullable=True)
    segment_count = Column(Integer, nullable=True)
    response_shape = Column(String, nullable=True)

    # NULL unless VOICE_STORE_TRANSCRIPT_TEXT=true. Transcribed speech is
    # personal data, and it already lands in `messages` once the user sends it.
    # Duplicating it into a table with a different retention policy and a
    # different deletion path (DELETE /conversations/{id} does not reach here)
    # is pure liability.
    text = Column(Text, nullable=True)

    # Outcome
    status = Column(String, nullable=False)  # success|asr_error|preprocess_error|rejected
    error_code = Column(String, nullable=True)
    upstream_status = Column(Integer, nullable=True)

    created_at = Column(DateTime(timezone=True), server_default=func.now())


Index(
    "ix_voice_transcriptions_user_created",
    VoiceTranscription.user_id,
    VoiceTranscription.created_at,
)
