"""add notification tables

Revision ID: ed197421f25a
Revises: 53d6d0b55f11
Create Date: 2026-01-29 13:12:33.275063

"""
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 = 'ed197421f25a'
down_revision: Union[str, Sequence[str], None] = '53d6d0b55f11'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('notification_target',
    sa.Column('provider', sa.String(length=50), nullable=False),
    sa.Column('target_key', sa.String(length=100), nullable=False),
    sa.Column('display_name', sa.String(length=255), nullable=True),
    sa.Column('webhook_url', sa.Text(), nullable=False),
    sa.Column('is_enabled', sa.Boolean(), nullable=False),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.PrimaryKeyConstraint('id', name=op.f('notification_target_pkey')),
    sa.UniqueConstraint('provider', 'target_key', name='uq_notification_target_provider_key')
    )
    op.create_index(op.f('notification_target_id_idx'), 'notification_target', ['id'], unique=False)
    op.create_table('notification_outbox',
    sa.Column('provider', sa.String(length=50), nullable=False),
    sa.Column('target_key', sa.String(length=100), nullable=False),
    sa.Column('event_type', sa.String(length=100), nullable=False),
    sa.Column('severity', sa.String(length=20), nullable=True),
    sa.Column('dedupe_key', sa.String(length=255), nullable=False),
    sa.Column('correlation_id', sa.String(length=100), nullable=True),
    sa.Column('entity_type', sa.String(length=50), nullable=True),
    sa.Column('entity_id', sa.String(length=100), nullable=True),
    sa.Column('payload', postgresql.JSONB(astext_type=sa.Text()), nullable=False),
    sa.Column('status', sa.String(length=20), nullable=False),
    sa.Column('priority', sa.Integer(), nullable=False),
    sa.Column('attempts', sa.Integer(), nullable=False),
    sa.Column('max_attempts', sa.Integer(), nullable=False),
    sa.Column('next_attempt_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('last_error', sa.Text(), nullable=True),
    sa.Column('sent_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('expires_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('locked_by', sa.String(length=100), nullable=True),
    sa.Column('locked_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('deleted_at', sa.DateTime(timezone=True), nullable=True),
    sa.ForeignKeyConstraint(['provider', 'target_key'], ['notification_target.provider', 'notification_target.target_key'], name='fk_notification_outbox_target'),
    sa.PrimaryKeyConstraint('id', name=op.f('notification_outbox_pkey')),
    sa.UniqueConstraint('dedupe_key', name=op.f('notification_outbox_dedupe_key_key'))
    )
    op.create_index('ix_notification_outbox_entity', 'notification_outbox', ['entity_type', 'entity_id'], unique=False)
    op.create_index('ix_notification_outbox_ready', 'notification_outbox', ['status', 'priority', 'next_attempt_at'], unique=False)
    op.create_index(op.f('notification_outbox_correlation_id_idx'), 'notification_outbox', ['correlation_id'], unique=False)
    op.create_index(op.f('notification_outbox_expires_at_idx'), 'notification_outbox', ['expires_at'], unique=False)
    op.create_index(op.f('notification_outbox_id_idx'), 'notification_outbox', ['id'], unique=False)
    op.create_index(op.f('notification_outbox_status_idx'), 'notification_outbox', ['status'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('notification_outbox_status_idx'), table_name='notification_outbox')
    op.drop_index(op.f('notification_outbox_id_idx'), table_name='notification_outbox')
    op.drop_index(op.f('notification_outbox_expires_at_idx'), table_name='notification_outbox')
    op.drop_index(op.f('notification_outbox_correlation_id_idx'), table_name='notification_outbox')
    op.drop_index('ix_notification_outbox_ready', table_name='notification_outbox')
    op.drop_index('ix_notification_outbox_entity', table_name='notification_outbox')
    op.drop_table('notification_outbox')
    op.drop_index(op.f('notification_target_id_idx'), table_name='notification_target')
    op.drop_table('notification_target')
    # ### end Alembic commands ###
