"""add_annotations_table

Revision ID: b5c3e8a2d1f0
Revises: 6a9d9f408f40
Create Date: 2025-12-17 10: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 = "b5c3e8a2d1f0"
down_revision: str | Sequence[str] | None = "6a9d9f408f40"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    """Create annotations table for user-created annotations during recording."""
    op.create_table(
        "annotations",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column(
            "annotation_id",
            postgresql.UUID(as_uuid=True),
            nullable=False,
            unique=True,
        ),
        sa.Column(
            "meeting_id",
            postgresql.UUID(as_uuid=True),
            sa.ForeignKey("noteflow.meetings.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("annotation_type", sa.String(50), nullable=False),
        sa.Column("text", sa.Text(), nullable=False),
        sa.Column("start_time", sa.Float(), nullable=False),
        sa.Column("end_time", sa.Float(), nullable=False),
        sa.Column(
            "segment_ids",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=False,
            server_default="[]",
        ),
        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_annotations_meeting_id",
        "annotations",
        ["meeting_id"],
        schema="noteflow",
    )

    # Create index for time-based queries
    op.create_index(
        "ix_annotations_time_range",
        "annotations",
        ["meeting_id", "start_time", "end_time"],
        schema="noteflow",
    )


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