"""add_anonymous_page_views

Revision ID: 8aa6d3a9e501
Revises: 132de587a02d
Create Date: 2026-07-16 18:08:22.709405
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa


revision: str = '8aa6d3a9e501'
down_revision: Union[str, None] = '132de587a02d'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table('anonymous_page_views',
        sa.Column('id', sa.Uuid(), nullable=False),
        sa.Column('anonymous_id', sa.String(length=64), nullable=False),
        sa.Column('path', sa.String(length=500), nullable=False),
        sa.Column('page_title', sa.String(length=200), nullable=True),
        sa.Column('ip_address', sa.String(length=45), nullable=True),
        sa.Column('user_agent', sa.String(length=500), nullable=True),
        sa.Column('referer', sa.String(length=500), nullable=True),
        sa.Column('duration_seconds', sa.Integer(), nullable=True),
        sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_anonymous_page_views_anonymous_id'), 'anonymous_page_views', ['anonymous_id'], unique=False)
    op.create_index('ix_apv_anon_created', 'anonymous_page_views', ['anonymous_id', 'created_at'], unique=False)
    op.create_index('ix_apv_created', 'anonymous_page_views', ['created_at'], unique=False)


def downgrade() -> None:
    op.drop_index('ix_apv_created', table_name='anonymous_page_views')
    op.drop_index('ix_apv_anon_created', table_name='anonymous_page_views')
    op.drop_index(op.f('ix_anonymous_page_views_anonymous_id'), table_name='anonymous_page_views')
    op.drop_table('anonymous_page_views')
