"""create notifs table

Revision ID: add_notifs_table
Revises: add_events_table
Create Date: 2024-03-19 10:00:00.000000

"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy import inspect

# revision identifiers, used by Alembic.
revision = 'add_notifs_table'
down_revision = 'add_events_table'
branch_labels = None
depends_on = None

def upgrade():
    conn = op.get_bind()
    inspector = inspect(conn)
    tables = inspector.get_table_names()
    
    if 'notifs' not in tables:
        op.create_table(
            'notifs',
            sa.Column('id', sa.Integer, primary_key=True),
            sa.Column('notif_type', sa.String(50), nullable=False),
            sa.Column('user_id', sa.Integer, sa.ForeignKey('user.id'), nullable=False),
            sa.Column('sender_id', sa.Integer, sa.ForeignKey('user.id'), nullable=True),
            sa.Column('timestamp', sa.DateTime, nullable=False),
            sa.Column('read', sa.Boolean, nullable=False, default=False),
            sa.Column('details', sa.JSON),
        )
        op.create_index('idx_notifs_notif_type', 'notifs', ['notif_type'])
        op.create_index('idx_notifs_timestamp', 'notifs', ['timestamp'])
        op.create_index('idx_notifs_user_id', 'notifs', ['user_id'])
        op.create_index('idx_notifs_sender_id', 'notifs', ['sender_id'])
        op.create_index('idx_notifs_read', 'notifs', ['read'])

def downgrade():
    op.drop_index('idx_notifs_notif_type', table_name='notifs')
    op.drop_index('idx_notifs_timestamp', table_name='notifs')
    op.drop_index('idx_notifs_user_id', table_name='notifs')
    op.drop_index('idx_notifs_sender_id', table_name='notifs')
    op.drop_index('idx_notifs_read', table_name='notifs')
    op.drop_table('notifs') 