"""feat: add new order and learn models

Revision ID: c18b39622587
Revises: 0cbd676b770e
Create Date: 2025-08-16 05:45:57.531750

"""

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import mysql

# revision identifiers, used by Alembic.
revision = "c18b39622587"
down_revision = "0cbd676b770e"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "learn_generated_blocks",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "generated_block_bid",
            sa.String(length=36),
            nullable=False,
            comment="Learn block log business identifier",
        ),
        sa.Column(
            "progress_record_bid",
            sa.String(length=36),
            nullable=False,
            comment="Learn outline item business identifier",
        ),
        sa.Column(
            "user_bid",
            sa.String(length=36),
            nullable=False,
            comment="User business identifier",
        ),
        sa.Column(
            "block_bid",
            sa.String(length=36),
            nullable=False,
            comment="Block business identifier",
        ),
        sa.Column(
            "outline_item_bid",
            sa.String(length=36),
            nullable=False,
            comment="Outline business identifier",
        ),
        sa.Column(
            "shifu_bid",
            sa.String(length=36),
            nullable=False,
            comment="Shifu business identifier",
        ),
        sa.Column("type", sa.Integer(), nullable=False, comment="Block content type"),
        sa.Column("role", sa.Integer(), nullable=False, comment="Block role"),
        sa.Column(
            "generated_content",
            sa.Text(),
            nullable=False,
            comment="Block generate content",
        ),
        sa.Column(
            "position", sa.Integer(), nullable=False, comment="Block position index"
        ),
        sa.Column(
            "block_content_conf",
            sa.Text(),
            nullable=False,
            comment="Block content config(used for re-generate)",
        ),
        sa.Column(
            "liked",
            sa.Integer(),
            nullable=False,
            comment="Interaction type: -1=disliked, 0=not available, 1=liked",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column(
            "status",
            sa.Integer(),
            nullable=False,
            comment="Status of the record: 1=active, 0=history",
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False, comment="Creation time"),
        sa.Column("updated_at", sa.DateTime(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        comment="Learn generated blocks",
    )
    with op.batch_alter_table("learn_generated_blocks", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_block_bid"),
            ["block_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_generated_block_bid"),
            ["generated_block_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_outline_item_bid"),
            ["outline_item_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_progress_record_bid"),
            ["progress_record_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_shifu_bid"),
            ["shifu_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_generated_blocks_user_bid"), ["user_bid"], unique=False
        )

    op.create_table(
        "learn_progress_records",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "progress_record_bid",
            sa.String(length=36),
            nullable=False,
            comment="Learn outline item business identifier",
        ),
        sa.Column(
            "shifu_bid",
            sa.String(length=36),
            nullable=False,
            comment="Shifu business identifier",
        ),
        sa.Column(
            "outline_item_bid",
            sa.String(length=36),
            nullable=False,
            comment="Outline business identifier",
        ),
        sa.Column(
            "user_bid",
            sa.String(length=36),
            nullable=False,
            comment="User business identifier",
        ),
        sa.Column(
            "outline_item_updated",
            sa.Integer(),
            nullable=False,
            comment="Outline is updated",
        ),
        sa.Column(
            "status",
            sa.SmallInteger(),
            nullable=False,
            comment="Status: 601=not started, 602=in progress, 603=completed, 604=refund, 605=locked, 606=unavailable, 607=branch, 608=reset",
        ),
        sa.Column(
            "block_position",
            sa.Integer(),
            nullable=False,
            comment="Block position index of the outlineitem",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False, comment="Creation time"),
        sa.Column("updated_at", sa.DateTime(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        comment="Learn progress records",
    )
    with op.batch_alter_table("learn_progress_records", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_learn_progress_records_outline_item_bid"),
            ["outline_item_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_progress_records_progress_record_bid"),
            ["progress_record_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_progress_records_shifu_bid"),
            ["shifu_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_learn_progress_records_status"), ["status"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_learn_progress_records_user_bid"), ["user_bid"], unique=False
        )

    op.create_table(
        "order_orders",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "order_bid",
            sa.String(length=36),
            nullable=False,
            comment="Order business identifier",
        ),
        sa.Column(
            "shifu_bid",
            sa.String(length=36),
            nullable=False,
            comment="Shifu business identifier",
        ),
        sa.Column(
            "user_bid",
            sa.String(length=36),
            nullable=False,
            comment="User business identifier",
        ),
        sa.Column(
            "payable_price",
            sa.Numeric(precision=10, scale=2),
            nullable=False,
            comment="Shifu original price",
        ),
        sa.Column(
            "paid_price",
            sa.Numeric(precision=10, scale=2),
            nullable=False,
            comment="Paid price",
        ),
        sa.Column(
            "status",
            sa.SmallInteger(),
            nullable=False,
            comment="Status of the order: 501=init, 502=paid, 503=refunded, 504=unpaid, 505=timeout",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False, comment="Creation time"),
        sa.Column("updated_at", sa.DateTime(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        comment="Order orders",
    )
    with op.batch_alter_table("order_orders", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_order_orders_order_bid"), ["order_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_orders_shifu_bid"), ["shifu_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_orders_user_bid"), ["user_bid"], unique=False
        )

    op.create_table(
        "order_pingxx_orders",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "pingxx_order_bid",
            sa.String(length=36),
            nullable=False,
            comment="Pingxx order business identifier",
        ),
        sa.Column(
            "user_bid",
            sa.String(length=36),
            nullable=False,
            comment="User business identifier",
        ),
        sa.Column(
            "shifu_bid",
            sa.String(length=36),
            nullable=False,
            comment="Shifu business identifier",
        ),
        sa.Column(
            "order_bid",
            sa.String(length=36),
            nullable=False,
            comment="Order business identifier",
        ),
        sa.Column(
            "transaction_no",
            sa.String(length=36),
            nullable=False,
            comment="Pingxx transaction number",
        ),
        sa.Column(
            "app_id",
            sa.String(length=36),
            nullable=False,
            comment="Pingxx app identifier",
        ),
        sa.Column(
            "channel", sa.String(length=36), nullable=False, comment="Payment channel"
        ),
        sa.Column("amount", mysql.BIGINT(), nullable=False, comment="Payment amount"),
        sa.Column("currency", sa.String(length=36), nullable=False, comment="Currency"),
        sa.Column(
            "subject", sa.String(length=255), nullable=False, comment="Payment subject"
        ),
        sa.Column(
            "body", sa.String(length=255), nullable=False, comment="Payment body"
        ),
        sa.Column(
            "client_ip", sa.String(length=255), nullable=False, comment="Client IP"
        ),
        sa.Column("extra", sa.Text(), nullable=False, comment="Extra information"),
        sa.Column(
            "status",
            sa.SmallInteger(),
            nullable=False,
            comment="Status of the order: 0=unpaid, 1=paid, 2=refunded, 3=closed, 4=failed",
        ),
        sa.Column(
            "charge_id",
            sa.String(length=255),
            nullable=False,
            comment="Charge identifier",
        ),
        sa.Column("paid_at", sa.DateTime(), nullable=False, comment="Payment time"),
        sa.Column("refunded_at", sa.DateTime(), nullable=False, comment="Refund time"),
        sa.Column("closed_at", sa.DateTime(), nullable=False, comment="Close time"),
        sa.Column("failed_at", sa.DateTime(), nullable=False, comment="Failed time"),
        sa.Column(
            "refund_id",
            sa.String(length=255),
            nullable=False,
            comment="Refund identifier",
        ),
        sa.Column(
            "failure_code",
            sa.String(length=255),
            nullable=False,
            comment="Failure code",
        ),
        sa.Column(
            "failure_msg",
            sa.String(length=255),
            nullable=False,
            comment="Failure message",
        ),
        sa.Column(
            "charge_object",
            sa.Text(),
            nullable=False,
            comment="Pingxx raw charge object",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column("created_at", sa.DateTime(), nullable=False, comment="Creation time"),
        sa.Column("updated_at", sa.DateTime(), nullable=False, comment="Update time"),
        sa.PrimaryKeyConstraint("id"),
        comment="Order pingxx orders",
    )
    with op.batch_alter_table("order_pingxx_orders", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_app_id"), ["app_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_charge_id"), ["charge_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_order_bid"), ["order_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_pingxx_order_bid"),
            ["pingxx_order_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_refund_id"), ["refund_id"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_shifu_bid"), ["shifu_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_transaction_no"),
            ["transaction_no"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_order_pingxx_orders_user_bid"), ["user_bid"], unique=False
        )

    op.create_table(
        "promo_coupon_usages",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "coupon_usage_bid",
            sa.String(length=36),
            nullable=False,
            comment="Coupon usage business identifier",
        ),
        sa.Column(
            "coupon_bid",
            sa.String(length=36),
            nullable=False,
            comment="Coupon business identifier",
        ),
        sa.Column("name", sa.String(length=255), nullable=False, comment="Coupon name"),
        sa.Column(
            "user_bid",
            sa.String(length=36),
            nullable=False,
            comment="User business identifier",
        ),
        sa.Column(
            "shifu_bid",
            sa.String(length=36),
            nullable=False,
            comment="Shifu business identifier",
        ),
        sa.Column(
            "order_bid",
            sa.String(length=36),
            nullable=False,
            comment="Order business identifier",
        ),
        sa.Column("code", sa.String(length=36), nullable=False, comment="Coupon code"),
        sa.Column(
            "discount_type",
            sa.SmallInteger(),
            nullable=False,
            comment="Coupon Type: 701=fixed, 702=percent",
        ),
        sa.Column(
            "value",
            sa.Numeric(precision=10, scale=2),
            nullable=False,
            comment="Coupon value: would be calculated to amount by coupon type",
        ),
        sa.Column(
            "status",
            sa.SmallInteger(),
            nullable=False,
            comment="Status of the record: 901=inactive, 902=active, 903=used, 904=timeout",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column(
            "created_at",
            sa.DateTime(),
            server_default=sa.text("now()"),
            nullable=False,
            comment="Creation timestamp",
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(),
            server_default=sa.text("now()"),
            nullable=False,
            comment="Update timestamp",
        ),
        sa.PrimaryKeyConstraint("id"),
        comment="Promo coupon usages",
    )
    with op.batch_alter_table("promo_coupon_usages", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_promo_coupon_usages_coupon_bid"),
            ["coupon_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_promo_coupon_usages_coupon_usage_bid"),
            ["coupon_usage_bid"],
            unique=False,
        )
        batch_op.create_index(
            batch_op.f("ix_promo_coupon_usages_order_bid"), ["order_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_promo_coupon_usages_shifu_bid"), ["shifu_bid"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_promo_coupon_usages_user_bid"), ["user_bid"], unique=False
        )

    op.create_table(
        "promo_coupons",
        sa.Column("id", mysql.BIGINT(), autoincrement=True, nullable=False),
        sa.Column(
            "coupon_bid",
            sa.String(length=36),
            nullable=False,
            comment="Coupon business identifier",
        ),
        sa.Column("code", sa.String(length=36), nullable=False, comment="Coupon code"),
        sa.Column(
            "discount_type",
            sa.SmallInteger(),
            nullable=False,
            comment="Coupon type: 701=fixed, 702=percent",
        ),
        sa.Column(
            "usage_type",
            sa.SmallInteger(),
            nullable=False,
            comment="Coupon apply type: 801=one coupon code for multiple times, 802=one coupon code for one time",
        ),
        sa.Column(
            "value",
            sa.Numeric(precision=10, scale=2),
            nullable=False,
            comment="Coupon value: would be calculated to amount by coupon type",
        ),
        sa.Column("start", sa.DateTime(), nullable=False, comment="Coupon start time"),
        sa.Column("end", sa.DateTime(), nullable=False, comment="Coupon end time"),
        sa.Column(
            "channel", sa.String(length=36), nullable=False, comment="Coupon channel"
        ),
        sa.Column(
            "filter",
            sa.Text(),
            nullable=False,
            comment="Coupon filter: would be used to filter user and shifu",
        ),
        sa.Column(
            "total_count", mysql.BIGINT(), nullable=False, comment="Coupon total count"
        ),
        sa.Column(
            "used_count", mysql.BIGINT(), nullable=False, comment="Coupon used count"
        ),
        sa.Column(
            "status",
            sa.SmallInteger(),
            nullable=False,
            comment="Status of the discount: 0=inactive, 1=active",
        ),
        sa.Column(
            "deleted",
            sa.SmallInteger(),
            nullable=False,
            comment="Deletion flag: 0=active, 1=deleted",
        ),
        sa.Column(
            "created_at",
            sa.DateTime(),
            server_default=sa.text("now()"),
            nullable=False,
            comment="Creation timestamp",
        ),
        sa.Column(
            "created_user_bid",
            sa.String(length=36),
            nullable=False,
            comment="Creator user business identifier",
        ),
        sa.Column(
            "updated_at",
            sa.DateTime(),
            server_default=sa.text("now()"),
            nullable=False,
            comment="Update timestamp",
        ),
        sa.PrimaryKeyConstraint("id"),
        comment="Promo coupons",
    )
    with op.batch_alter_table("promo_coupons", schema=None) as batch_op:
        batch_op.create_index(
            batch_op.f("ix_promo_coupons_code"), ["code"], unique=False
        )
        batch_op.create_index(
            batch_op.f("ix_promo_coupons_coupon_bid"), ["coupon_bid"], unique=False
        )

    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table("promo_coupons", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_promo_coupons_coupon_bid"))
        batch_op.drop_index(batch_op.f("ix_promo_coupons_code"))

    op.drop_table("promo_coupons")
    with op.batch_alter_table("promo_coupon_usages", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_promo_coupon_usages_user_bid"))
        batch_op.drop_index(batch_op.f("ix_promo_coupon_usages_shifu_bid"))
        batch_op.drop_index(batch_op.f("ix_promo_coupon_usages_order_bid"))
        batch_op.drop_index(batch_op.f("ix_promo_coupon_usages_coupon_usage_bid"))
        batch_op.drop_index(batch_op.f("ix_promo_coupon_usages_coupon_bid"))

    op.drop_table("promo_coupon_usages")
    with op.batch_alter_table("order_pingxx_orders", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_user_bid"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_transaction_no"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_shifu_bid"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_refund_id"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_pingxx_order_bid"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_order_bid"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_charge_id"))
        batch_op.drop_index(batch_op.f("ix_order_pingxx_orders_app_id"))

    op.drop_table("order_pingxx_orders")
    with op.batch_alter_table("order_orders", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_order_orders_user_bid"))
        batch_op.drop_index(batch_op.f("ix_order_orders_shifu_bid"))
        batch_op.drop_index(batch_op.f("ix_order_orders_order_bid"))

    op.drop_table("order_orders")
    with op.batch_alter_table("learn_progress_records", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_learn_progress_records_user_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_progress_records_status"))
        batch_op.drop_index(batch_op.f("ix_learn_progress_records_shifu_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_progress_records_progress_record_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_progress_records_outline_item_bid"))

    op.drop_table("learn_progress_records")
    with op.batch_alter_table("learn_generated_blocks", schema=None) as batch_op:
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_user_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_shifu_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_progress_record_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_outline_item_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_generated_block_bid"))
        batch_op.drop_index(batch_op.f("ix_learn_generated_blocks_block_bid"))

    op.drop_table("learn_generated_blocks")
    # ### end Alembic commands ###
