"""add_streaming_diarization_turns

Revision ID: g1b2c3d4e5f6
Revises: f0a1b2c3d4e5
Create Date: 2025-12-19 14:00:00.000000

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "g1b2c3d4e5f6"
down_revision: str | Sequence[str] | None = "f0a1b2c3d4e5"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Create streaming_diarization_turns table for crash-resilient speaker turns."""
    op.create_table(
        "streaming_diarization_turns",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column(
            "meeting_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("noteflow.meetings.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("speaker", sa.String(50), nullable=False),
        sa.Column("start_time", sa.Float(), nullable=False),
        sa.Column("end_time", sa.Float(), nullable=False),
        sa.Column("confidence", sa.Float(), nullable=False, server_default="0.0"),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            nullable=False,
            server_default=sa.text("now()"),
        ),
        schema="noteflow",
    )

    # Create index for meeting_id lookups
    op.create_index(
        "ix_streaming_diarization_turns_meeting_id",
        "streaming_diarization_turns",
        ["meeting_id"],
        schema="noteflow",
    )


def downgrade() -> None:
    """Drop streaming_diarization_turns table."""
    op.drop_index(
        "ix_streaming_diarization_turns_meeting_id",
        table_name="streaming_diarization_turns",
        schema="noteflow",
    )
    op.drop_table("streaming_diarization_turns", schema="noteflow")
