"""Add processed webhook events table for deduplication

Revision ID: webhook_events_001
Revises: 
Create Date: 2026-01-14 12:00:00.000000

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'webhook_events_001'
down_revision = 'ab0d186d8497'  # Points to the existing head
branch_labels = None
depends_on = None


def upgrade():
    """Add processed_webhook_events table for idempotency"""
    op.create_table(
        'processed_webhook_events',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('stripe_event_id', sa.String(length=255), nullable=False),
        sa.Column('event_type', sa.String(length=100), nullable=True),
        sa.Column('processed_at', sa.DateTime(), server_default=sa.text('now()'), nullable=True),
        sa.PrimaryKeyConstraint('id'),
        sa.UniqueConstraint('stripe_event_id')
    )
    op.create_index(
        op.f('ix_processed_webhook_events_stripe_event_id'), 
        'processed_webhook_events', 
        ['stripe_event_id'], 
        unique=True
    )


def downgrade():
    """Remove processed_webhook_events table"""
    op.drop_index(
        op.f('ix_processed_webhook_events_stripe_event_id'), 
        table_name='processed_webhook_events'
    )
    op.drop_table('processed_webhook_events')
