"""coupon session table(领券任务全程流水 → admin「领券数据」看板数据源)

一次领券一行(trace_id 唯一):客户端 POST /api/v1/coupon/session 两段上报 —— 发起建行
(status=started)、收尾(completed/failed/abandoned)按 trace_id 更新同一行。记全程耗时
elapsed_ms + 各平台耗时 platform_elapsed + 机型/ROM,供 admin 算发起/完成数、耗时分位、机型维度。

Revision ID: coupon_session_table
Revises: feedback_submit_env
Create Date: 2026-06-30 00:00:00.000000

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "coupon_session_table"
down_revision: str | Sequence[str] | None = "feedback_submit_env"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None

# PG 用 JSONB,SQLite(本地/测试)退化为通用 JSON(同 model 的 _JSON variant)。
_JSON = sa.JSON().with_variant(postgresql.JSONB(), "postgresql")


def upgrade() -> None:
    op.create_table(
        "coupon_session",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("trace_id", sa.String(length=64), nullable=False),
        sa.Column("device_id", sa.String(length=64), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("status", sa.String(length=16), nullable=False),
        sa.Column("app_env", sa.String(length=16), nullable=True),
        sa.Column("platforms", _JSON, nullable=True),
        sa.Column("device_model", sa.String(length=128), nullable=True),
        sa.Column("rom", sa.String(length=64), nullable=True),
        sa.Column("started_at", sa.DateTime(timezone=True), nullable=False),
        sa.Column("started_date", sa.Date(), nullable=False),
        sa.Column("finished_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("elapsed_ms", sa.Integer(), nullable=True),
        sa.Column("platform_elapsed", _JSON, nullable=True),
        sa.Column("claimed_count", sa.Integer(), nullable=True),
        sa.Column(
            "created_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(timezone=True),
            server_default=sa.text("(CURRENT_TIMESTAMP)"),
            nullable=False,
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("trace_id", name="uq_coupon_session_trace"),
    )
    with op.batch_alter_table("coupon_session", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_coupon_session_user_id"), ["user_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_coupon_session_app_env"), ["app_env"], unique=False
        )
        # admin 主聚合/筛选:按上海自然日 + 环境。
        batch_op.create_index(
            "ix_coupon_session_date_env", ["started_date", "app_env"], unique=False
        )


def downgrade() -> None:
    with op.batch_alter_table("coupon_session", schema=None) as batch_op:
        batch_op.drop_index("ix_coupon_session_date_env")
        batch_op.drop_index(batch_op.f("ix_coupon_session_app_env"))
        batch_op.drop_index(batch_op.f("ix_coupon_session_user_id"))
    op.drop_table("coupon_session")
