"""add jd cps order fields

Revision ID: jd_cps_order_fields
Revises: 7db22acee504
Create Date: 2026-06-28 20:30:00.000000
"""
from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "jd_cps_order_fields"
down_revision = "7db22acee504"
branch_labels = None
depends_on = None


def upgrade() -> None:
    with op.batch_alter_table("cps_order") as batch_op:
        batch_op.add_column(
            sa.Column("platform", sa.String(length=20), nullable=False, server_default="meituan")
        )
        batch_op.add_column(sa.Column("external_order_id", sa.String(length=128), nullable=True))
        batch_op.add_column(sa.Column("external_row_id", sa.String(length=128), nullable=True))
        batch_op.add_column(sa.Column("estimated_commission_cents", sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column("actual_commission_cents", sa.Integer(), nullable=True))
        batch_op.add_column(sa.Column("jd_valid_code", sa.String(length=16), nullable=True))
        batch_op.add_column(sa.Column("settle_month", sa.String(length=16), nullable=True))
        batch_op.add_column(sa.Column("site_id", sa.String(length=128), nullable=True))
        batch_op.add_column(sa.Column("position_id", sa.String(length=128), nullable=True))
        batch_op.add_column(sa.Column("pid", sa.String(length=128), nullable=True))
        batch_op.add_column(sa.Column("sub_union_id", sa.String(length=128), nullable=True))
        batch_op.create_index("ix_cps_order_platform", ["platform"])
        batch_op.create_index("ix_cps_order_external_order_id", ["external_order_id"])
        batch_op.create_index("ix_cps_order_external_row_id", ["external_row_id"])
        batch_op.create_index("ix_cps_order_jd_valid_code", ["jd_valid_code"])


def downgrade() -> None:
    with op.batch_alter_table("cps_order") as batch_op:
        batch_op.drop_index("ix_cps_order_jd_valid_code")
        batch_op.drop_index("ix_cps_order_external_row_id")
        batch_op.drop_index("ix_cps_order_external_order_id")
        batch_op.drop_index("ix_cps_order_platform")
        batch_op.drop_column("sub_union_id")
        batch_op.drop_column("pid")
        batch_op.drop_column("position_id")
        batch_op.drop_column("site_id")
        batch_op.drop_column("settle_month")
        batch_op.drop_column("jd_valid_code")
        batch_op.drop_column("actual_commission_cents")
        batch_op.drop_column("estimated_commission_cents")
        batch_op.drop_column("external_row_id")
        batch_op.drop_column("external_order_id")
        batch_op.drop_column("platform")
