"""add analytics_event table

Revision ID: 1699fc2c069f
Revises: bcfcaf07152b
Create Date: 2026-06-26 16:35:16.133975

"""
from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision: str = '1699fc2c069f'
down_revision: str | Sequence[str] | None = 'bcfcaf07152b'
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('analytics_event',
    sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
    sa.Column('event', sa.String(length=64), nullable=False),
    sa.Column('props', sa.JSON(), nullable=True),
    sa.Column('device_id', sa.String(length=64), nullable=False),
    sa.Column('user_id', sa.Integer(), nullable=True),
    sa.Column('session_id', sa.String(length=64), nullable=True),
    sa.Column('client_ts', sa.BigInteger(), nullable=False),
    sa.Column('sent_at', sa.BigInteger(), nullable=True),
    sa.Column('page', sa.String(length=64), nullable=True),
    sa.Column('client_ip', sa.String(length=64), nullable=True),
    sa.Column('oem', sa.String(length=32), nullable=True),
    sa.Column('os', sa.String(length=32), nullable=True),
    sa.Column('model', sa.String(length=64), nullable=True),
    sa.Column('app_ver', sa.String(length=32), nullable=True),
    sa.Column('network', sa.String(length=16), nullable=True),
    sa.Column('channel', sa.String(length=32), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('(CURRENT_TIMESTAMP)'), nullable=False),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('analytics_event', schema=None) as batch_op:
        batch_op.create_index(batch_op.f('ix_analytics_event_created_at'), ['created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_event_device_id'), ['device_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_event_event'), ['event'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_event_session_id'), ['session_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_analytics_event_user_id'), ['user_id'], unique=False)

    # 注:autogenerate 顺带检出 ad_ecpm/cps_*/invite_fingerprint 的历史索引漂移,
    # 与本次「新增埋点表」无关,已手动移除,避免本迁移误改他人表。
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('analytics_event', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_analytics_event_user_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_event_session_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_event_event'))
        batch_op.drop_index(batch_op.f('ix_analytics_event_device_id'))
        batch_op.drop_index(batch_op.f('ix_analytics_event_created_at'))

    op.drop_table('analytics_event')
    # ### end Alembic commands ###
