"""add voice_transcriptions table

One row per transcription attempt (HA-499), success or failure.

Deliberately a dedicated table rather than reusing `messages`: Message has no
generic metadata column, and transcription is stateless relative to
conversations — it can happen before one exists, or for a dictation the user
discards. `conversation_id` is a plain String with NO ForeignKey for that same
reason.

`text` is nullable and stays NULL unless VOICE_STORE_TRANSCRIPT_TEXT=true.
Audio bytes are never persisted.

Revision ID: e9c41b7d2f60
Revises: d3a7f1c05b28
Create Date: 2026-09-17 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = 'e9c41b7d2f60'
down_revision: Union[str, Sequence[str], None] = 'd3a7f1c05b28'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        'voice_transcriptions',
        sa.Column('id', sa.Integer(), primary_key=True, index=True),

        sa.Column('user_id', sa.Integer(), nullable=False),
        sa.Column('company_id', sa.Integer(), nullable=True),
        sa.Column('role_id', sa.Integer(), nullable=True),
        sa.Column('conversation_id', sa.String(), nullable=True),

        sa.Column('source_mime', sa.String(), nullable=True),
        sa.Column('source_bytes', sa.Integer(), nullable=True),
        sa.Column('sniffed_container', sa.String(), nullable=True),
        sa.Column('audio_seconds', sa.Float(), nullable=True),
        sa.Column('transport', sa.String(), nullable=True),

        sa.Column('preprocessed', sa.Boolean(), nullable=False, server_default=sa.false()),
        sa.Column('filter_chain', sa.String(), nullable=True),
        sa.Column('preprocess_ms', sa.Integer(), nullable=True),
        sa.Column('asr_ms', sa.Integer(), nullable=True),
        sa.Column('total_ms', sa.Integer(), nullable=True),

        sa.Column('language', sa.String(), nullable=True),
        sa.Column('char_count', sa.Integer(), nullable=True),
        sa.Column('segment_count', sa.Integer(), nullable=True),
        sa.Column('response_shape', sa.String(), nullable=True),
        sa.Column('text', sa.Text(), nullable=True),

        sa.Column('status', sa.String(), nullable=False),
        sa.Column('error_code', sa.String(), nullable=True),
        sa.Column('upstream_status', sa.Integer(), nullable=True),

        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.func.now()),
    )
    op.create_index('ix_voice_transcriptions_user_id', 'voice_transcriptions', ['user_id'])
    op.create_index('ix_voice_transcriptions_company_id', 'voice_transcriptions', ['company_id'])
    op.create_index('ix_voice_transcriptions_user_created', 'voice_transcriptions',
                    ['user_id', 'created_at'])


def downgrade() -> None:
    op.drop_index('ix_voice_transcriptions_user_created', table_name='voice_transcriptions')
    op.drop_index('ix_voice_transcriptions_company_id', table_name='voice_transcriptions')
    op.drop_index('ix_voice_transcriptions_user_id', table_name='voice_transcriptions')
    op.drop_table('voice_transcriptions')
