"""add_newsletter_subscribers_table

Revision ID: 316d876b1b71
Revises: add_enterprise_homepage_fields
Create Date: 2025-12-05 15:33:09.120967

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '316d876b1b71'
down_revision = 'add_enterprise_homepage_fields'
branch_labels = None
depends_on = None


def upgrade() -> None:
    # Check if table exists
    from sqlalchemy import inspect
    conn = op.get_bind()
    inspector = inspect(conn)
    tables = inspector.get_table_names()
    
    if 'newsletter_subscribers' not in tables:
        # Create newsletter_subscribers table
        op.create_table(
            'newsletter_subscribers',
            sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
            sa.Column('email', sa.String(255), nullable=False),
            sa.Column('user_id', sa.Integer(), nullable=True),
            sa.Column('name', sa.String(255), nullable=True),
            sa.Column('subscribed_at', sa.DateTime(), nullable=False),
            sa.Column('updated_at', sa.DateTime(), nullable=False),
            sa.Column('is_active', sa.Boolean(), nullable=False, server_default='1'),
            sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
            sa.PrimaryKeyConstraint('id'),
            sa.UniqueConstraint('email')
        )
        op.create_index(op.f('ix_newsletter_subscribers_email'), 'newsletter_subscribers', ['email'], unique=False)
        op.create_index(op.f('ix_newsletter_subscribers_id'), 'newsletter_subscribers', ['id'], unique=False)
        op.create_index(op.f('ix_newsletter_subscribers_is_active'), 'newsletter_subscribers', ['is_active'], unique=False)
        op.create_index(op.f('ix_newsletter_subscribers_subscribed_at'), 'newsletter_subscribers', ['subscribed_at'], unique=False)
        op.create_index(op.f('ix_newsletter_subscribers_user_id'), 'newsletter_subscribers', ['user_id'], unique=False)


def downgrade() -> None:
    op.drop_index(op.f('ix_newsletter_subscribers_user_id'), table_name='newsletter_subscribers')
    op.drop_index(op.f('ix_newsletter_subscribers_subscribed_at'), table_name='newsletter_subscribers')
    op.drop_index(op.f('ix_newsletter_subscribers_is_active'), table_name='newsletter_subscribers')
    op.drop_index(op.f('ix_newsletter_subscribers_id'), table_name='newsletter_subscribers')
    op.drop_index(op.f('ix_newsletter_subscribers_email'), table_name='newsletter_subscribers')
    op.drop_table('newsletter_subscribers')

