"""add_gdpr_consent_tables

Revision ID: 510cf1184e0b
Revises: 13327ad46a4d
Create Date: 2025-10-15 21:55:40.584671+02:00

"""
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 = '510cf1184e0b'
down_revision: Union[str, None] = '13327ad46a4d'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('user_consents',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('terms_accepted', sa.Boolean(), nullable=False),
    sa.Column('privacy_accepted', sa.Boolean(), nullable=False),
    sa.Column('marketing_consent', sa.Boolean(), nullable=False),
    sa.Column('analytics_consent', sa.Boolean(), nullable=False),
    sa.Column('consent_version', sa.String(length=20), nullable=False),
    sa.Column('consent_method', sa.String(length=50), nullable=False),
    sa.Column('ip_address', sa.String(length=45), nullable=True),
    sa.Column('user_agent', sa.Text(), nullable=True),
    sa.Column('terms_text_hash', sa.String(length=64), nullable=True),
    sa.Column('privacy_text_hash', sa.String(length=64), nullable=True),
    sa.Column('consented_at', sa.DateTime(timezone=True), nullable=False),
    sa.Column('withdrawn_at', sa.DateTime(timezone=True), nullable=True),
    sa.Column('extra_data', postgresql.JSON(astext_type=sa.Text()), nullable=True),
    sa.ForeignKeyConstraint(['user_id'], ['users.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_user_consent_consented_at', 'user_consents', ['consented_at'], unique=False)
    op.create_index('idx_user_consent_user_id', 'user_consents', ['user_id'], unique=False)
    op.create_index(op.f('ix_user_consents_user_id'), 'user_consents', ['user_id'], unique=False)
    op.create_table('consent_history',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('user_id', sa.UUID(), nullable=False),
    sa.Column('consent_id', sa.UUID(), nullable=True),
    sa.Column('action', sa.String(length=50), nullable=False),
    sa.Column('consent_snapshot', postgresql.JSON(astext_type=sa.Text()), nullable=False),
    sa.Column('ip_address', sa.String(length=45), nullable=True),
    sa.Column('user_agent', sa.Text(), nullable=True),
    sa.Column('consent_method', sa.String(length=50), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), nullable=False),
    sa.ForeignKeyConstraint(['consent_id'], ['user_consents.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_consent_history_action', 'consent_history', ['action'], unique=False)
    op.create_index('idx_consent_history_created_at', 'consent_history', ['created_at'], unique=False)
    op.create_index('idx_consent_history_user_id', 'consent_history', ['user_id'], unique=False)
    op.create_index(op.f('ix_consent_history_created_at'), 'consent_history', ['created_at'], unique=False)
    op.create_index(op.f('ix_consent_history_user_id'), 'consent_history', ['user_id'], unique=False)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f('ix_consent_history_user_id'), table_name='consent_history')
    op.drop_index(op.f('ix_consent_history_created_at'), table_name='consent_history')
    op.drop_index('idx_consent_history_user_id', table_name='consent_history')
    op.drop_index('idx_consent_history_created_at', table_name='consent_history')
    op.drop_index('idx_consent_history_action', table_name='consent_history')
    op.drop_table('consent_history')
    op.drop_index(op.f('ix_user_consents_user_id'), table_name='user_consents')
    op.drop_index('idx_user_consent_user_id', table_name='user_consents')
    op.drop_index('idx_user_consent_consented_at', table_name='user_consents')
    op.drop_table('user_consents')
    # ### end Alembic commands ###
