"""coupon daily completion table(今日跑完整轮领券 → 首页置灰源)

Revision ID: coupon_daily_completion
Revises: f8d3b1e60a27
Create Date: 2026-06-10 00:00:00.000000

"""

from collections.abc import Sequence

import sqlalchemy as sa

from alembic import op

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


def upgrade() -> None:
    op.create_table(
        "coupon_daily_completion",
        sa.Column("id", sa.Integer(), autoincrement=True, nullable=False),
        sa.Column("device_id", sa.String(length=64), nullable=False),
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("complete_date", sa.Date(), nullable=False),
        sa.Column("trace_id", sa.String(length=64), 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("device_id", "complete_date", name="uq_coupon_completion_device_date"),
    )
    with op.batch_alter_table("coupon_daily_completion", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_coupon_daily_completion_user_id"), ["user_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_coupon_daily_completion_trace_id"), ["trace_id"], unique=False
        )


def downgrade() -> None:
    with op.batch_alter_table("coupon_daily_completion", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_coupon_daily_completion_trace_id"))
        batch_op.drop_index(batch_op.f("ix_coupon_daily_completion_user_id"))
    op.drop_table("coupon_daily_completion")
