"""Initial alerts table

Revision ID: 001
Revises:
Create Date: 2025-09-18 23:17:00

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

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


def upgrade() -> None:
    # Create enum types
    alert_status_enum = postgresql.ENUM('active', 'resolved', 'acknowledged', 'ignored', name='alertstatus')
    alert_severity_enum = postgresql.ENUM('low', 'medium', 'high', 'urgent', name='alertseverity')

    alert_status_enum.create(op.get_bind())
    alert_severity_enum.create(op.get_bind())

    # Create alerts table
    op.create_table('alerts',
        sa.Column('id', postgresql.UUID(as_uuid=True), primary_key=True),
        sa.Column('tenant_id', postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column('item_type', sa.String(length=50), nullable=False),
        sa.Column('alert_type', sa.String(length=100), nullable=False),
        sa.Column('severity', alert_severity_enum, nullable=False),
        sa.Column('status', alert_status_enum, nullable=False),
        sa.Column('service', sa.String(length=100), nullable=False),
        sa.Column('title', sa.String(length=255), nullable=False),
        sa.Column('message', sa.Text(), nullable=False),
        sa.Column('actions', sa.JSON(), nullable=True),
        sa.Column('metadata', sa.JSON(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.Column('updated_at', sa.DateTime(), nullable=False),
        sa.Column('resolved_at', sa.DateTime(), nullable=True),
    )

    # Create indexes
    op.create_index('ix_alerts_tenant_id', 'alerts', ['tenant_id'])
    op.create_index('ix_alerts_severity', 'alerts', ['severity'])
    op.create_index('ix_alerts_status', 'alerts', ['status'])
    op.create_index('ix_alerts_created_at', 'alerts', ['created_at'])


def downgrade() -> None:
    # Drop indexes
    op.drop_index('ix_alerts_created_at', 'alerts')
    op.drop_index('ix_alerts_status', 'alerts')
    op.drop_index('ix_alerts_severity', 'alerts')
    op.drop_index('ix_alerts_tenant_id', 'alerts')

    # Drop table
    op.drop_table('alerts')

    # Drop enum types
    op.execute('DROP TYPE alertseverity')
    op.execute('DROP TYPE alertstatus')