"""create instance disk addon billing table

Revision ID: 023_instance_disk_addons
Revises: 022_featured_label
Create Date: 2026-06-13
"""

from alembic import op
import sqlalchemy as sa

revision = "023_instance_disk_addons"
down_revision = "022_featured_label"
branch_labels = None
depends_on = None


def upgrade() -> None:
    bind = op.get_bind()
    tables = set(sa.inspect(bind).get_table_names())
    if "instance_disk_addons" in tables:
        return

    op.create_table(
        "instance_disk_addons",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("user_id", sa.BigInteger(), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("instance_id", sa.Integer(), sa.ForeignKey("instances.id", ondelete="CASCADE"), nullable=False),
        sa.Column("order_id", sa.Integer(), sa.ForeignKey("orders.id", ondelete="RESTRICT"), nullable=False),
        sa.Column("upstream_disk_id", sa.Integer(), nullable=True),
        sa.Column("upstream_task_id", sa.Integer(), nullable=True),
        sa.Column("size_gb", sa.Integer(), nullable=False),
        sa.Column("store_id", sa.Integer(), nullable=False),
        sa.Column("driver", sa.String(16), nullable=True),
        sa.Column("cache", sa.String(32), nullable=True),
        sa.Column("io", sa.String(16), nullable=True),
        sa.Column("iops_min", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("iops_max", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("cycle", sa.String(16), nullable=False),
        sa.Column("current_charge_amount", sa.Integer(), nullable=False, comment="本次扣费，单位分"),
        sa.Column("recurring_amount", sa.Integer(), nullable=False, comment="后续每周期费用，单位分"),
        sa.Column("charged_until", sa.DateTime(timezone=True), nullable=True),
        sa.Column(
            "status",
            sa.Enum("pending", "active", "failed", "refunded", "deleted", name="instancediskaddonstatus"),
            nullable=False,
            server_default="pending",
        ),
        sa.Column("payload_snapshot", sa.JSON(), nullable=True),
        sa.Column("remark", sa.Text(), nullable=True),
        sa.Column("activated_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("refunded_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("deleted_at", sa.DateTime(timezone=True), nullable=True),
    )
    op.create_index("ix_instance_disk_addons_user_id", "instance_disk_addons", ["user_id"])
    op.create_index("ix_instance_disk_addons_instance_id", "instance_disk_addons", ["instance_id"])
    op.create_index("ix_instance_disk_addons_order_id", "instance_disk_addons", ["order_id"])
    op.create_index("ix_instance_disk_addons_upstream_disk_id", "instance_disk_addons", ["upstream_disk_id"])
    op.create_index("ix_instance_disk_addons_upstream_task_id", "instance_disk_addons", ["upstream_task_id"])
    op.create_index("ix_instance_disk_addons_status", "instance_disk_addons", ["status"])


def downgrade() -> None:
    bind = op.get_bind()
    tables = set(sa.inspect(bind).get_table_names())
    if "instance_disk_addons" not in tables:
        return

    op.drop_index("ix_instance_disk_addons_status", table_name="instance_disk_addons")
    op.drop_index("ix_instance_disk_addons_upstream_task_id", table_name="instance_disk_addons")
    op.drop_index("ix_instance_disk_addons_upstream_disk_id", table_name="instance_disk_addons")
    op.drop_index("ix_instance_disk_addons_order_id", table_name="instance_disk_addons")
    op.drop_index("ix_instance_disk_addons_instance_id", table_name="instance_disk_addons")
    op.drop_index("ix_instance_disk_addons_user_id", table_name="instance_disk_addons")
    op.drop_table("instance_disk_addons")
