"""add_alert_interactions

Revision ID: a1b2c3d4e5f6
Revises: 5ad7a76c1b10
Create Date: 2025-10-19 14:30:00.000000+02:00

"""
from typing import Sequence, Union

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

# revision identifiers, used by Alembic.
revision: str = 'a1b2c3d4e5f6'
down_revision: Union[str, None] = '5ad7a76c1b10'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create alert_interactions table
    op.create_table('alert_interactions',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('tenant_id', sa.UUID(), nullable=False),
    sa.Column('alert_id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('interaction_type', sa.String(length=50), nullable=False),
    sa.Column('interacted_at', sa.DateTime(), nullable=False),
    sa.Column('response_time_seconds', sa.Integer(), nullable=True),
    sa.Column('interaction_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=False),
    sa.ForeignKeyConstraint(['alert_id'], ['alerts.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )

    # Create indexes for efficient queries
    op.create_index('idx_alert_interactions_tenant_alert', 'alert_interactions', ['tenant_id', 'alert_id'], unique=False)
    op.create_index('idx_alert_interactions_user', 'alert_interactions', ['user_id'], unique=False)
    op.create_index('idx_alert_interactions_time', 'alert_interactions', ['interacted_at'], unique=False)
    op.create_index('idx_alert_interactions_type', 'alert_interactions', ['interaction_type'], unique=False)
    op.create_index('idx_alert_interactions_tenant_time', 'alert_interactions', ['tenant_id', 'interacted_at'], unique=False)


def downgrade() -> None:
    op.drop_index('idx_alert_interactions_tenant_time', table_name='alert_interactions')
    op.drop_index('idx_alert_interactions_type', table_name='alert_interactions')
    op.drop_index('idx_alert_interactions_time', table_name='alert_interactions')
    op.drop_index('idx_alert_interactions_user', table_name='alert_interactions')
    op.drop_index('idx_alert_interactions_tenant_alert', table_name='alert_interactions')
    op.drop_table('alert_interactions')
