"""cps 微信用户表 cps_wx_user + cps_click 加 openid

CPS 落地页微信网页授权拿 openid/昵称头像,做用户级群统计。

Revision ID: cps_wx_user
Revises: comparison_debug_fields
Create Date: 2026-06-19 12:00:00.000000
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


revision: str = "cps_wx_user"
down_revision: Union[str, Sequence[str], None] = "comparison_debug_fields"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "cps_wx_user",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("openid", sa.String(64), nullable=False),
        sa.Column("unionid", sa.String(64), nullable=True),
        sa.Column("nickname", sa.String(128), nullable=True),
        sa.Column("headimgurl", sa.String(512), nullable=True),
        sa.Column("first_code", sa.String(16), nullable=True),
        sa.Column("first_group_id", sa.Integer(), nullable=True),
        sa.Column("first_seen", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("last_seen", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("ix_cps_wx_user_openid", "cps_wx_user", ["openid"], unique=True)
    op.create_index("ix_cps_wx_user_unionid", "cps_wx_user", ["unionid"])
    op.create_index("ix_cps_wx_user_first_group_id", "cps_wx_user", ["first_group_id"])

    op.add_column("cps_click", sa.Column("openid", sa.String(64), nullable=True))
    op.create_index("ix_cps_click_openid", "cps_click", ["openid"])


def downgrade() -> None:
    op.drop_index("ix_cps_click_openid", table_name="cps_click")
    op.drop_column("cps_click", "openid")
    op.drop_index("ix_cps_wx_user_first_group_id", table_name="cps_wx_user")
    op.drop_index("ix_cps_wx_user_unionid", table_name="cps_wx_user")
    op.drop_index("ix_cps_wx_user_openid", table_name="cps_wx_user")
    op.drop_table("cps_wx_user")
