"""create_cookie_consents_table

Revision ID: 76c7521bc046
Revises: add_user_soft_delete_fields
Create Date: 2025-12-05 22:49:00.646995

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '76c7521bc046'
down_revision = 'add_user_soft_delete_fields'
branch_labels = None
depends_on = None


def upgrade():
    # Create cookie_consents table
    op.create_table(
        'cookie_consents',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('consent_type', sa.String(length=20), nullable=False),
        sa.Column('consent_date', sa.DateTime(), nullable=False),
        sa.Column('ip_address', sa.String(length=45), nullable=True),
        sa.Column('user_agent', sa.Text(), nullable=True),
        sa.Column('browser_fingerprint', sa.String(length=64), nullable=True),
        sa.Column('session_id', sa.String(length=255), nullable=True),
        sa.Column('referrer_url', sa.String(length=500), nullable=True),
        sa.Column('page_url', sa.String(length=500), nullable=True),
        sa.Column('user_id', sa.Integer(), nullable=True),
        sa.Column('consent_version', sa.String(length=10), nullable=False, server_default='1.0'),
        sa.Column('country_code', sa.String(length=2), nullable=True),
        sa.Column('updated_at', sa.DateTime(), nullable=True),
        sa.Column('is_revoked', sa.Boolean(), nullable=False, server_default='0'),
        sa.Column('revoked_at', sa.DateTime(), nullable=True),
        sa.Column('created_at', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='SET NULL'),
        sa.PrimaryKeyConstraint('id')
    )
    
    # Create indexes for performance
    op.create_index('ix_cookie_consents_consent_date', 'cookie_consents', ['consent_date'])
    op.create_index('ix_cookie_consents_browser_fingerprint', 'cookie_consents', ['browser_fingerprint'])
    op.create_index('ix_cookie_consents_session_id', 'cookie_consents', ['session_id'])
    op.create_index('ix_cookie_consents_user_id', 'cookie_consents', ['user_id'])
    op.create_index('ix_cookie_consents_is_revoked', 'cookie_consents', ['is_revoked'])


def downgrade():
    # Drop indexes
    op.drop_index('ix_cookie_consents_is_revoked', 'cookie_consents')
    op.drop_index('ix_cookie_consents_user_id', 'cookie_consents')
    op.drop_index('ix_cookie_consents_session_id', 'cookie_consents')
    op.drop_index('ix_cookie_consents_browser_fingerprint', 'cookie_consents')
    op.drop_index('ix_cookie_consents_consent_date', 'cookie_consents')
    
    # Drop table
    op.drop_table('cookie_consents')
