"""products_add_cloud_binding

Revision ID: 006
Revises: 005
Create Date: 2026-05-16 00:00:00.000000

给 products 表加上"上游云接口绑定"三列：
- cloud_server_id  FK→cloud_servers.id  nullable，域名/SSL 等无需上游的产品留空
- api_type         字符串枚举   none / dcim_cloud_image / dcim_cloud_rid
- api_identifier   字符串       存上游 image id 或 rid，按 api_type 解释
"""
from typing import Sequence, Union

import sqlalchemy as sa
from alembic import op

revision: str = "006"
down_revision: Union[str, None] = "005"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column(
        "products",
        sa.Column("cloud_server_id", sa.Integer(), nullable=True),
    )
    op.add_column(
        "products",
        sa.Column(
            "api_type",
            sa.String(length=32),
            nullable=False,
            server_default="none",
        ),
    )
    op.add_column(
        "products",
        sa.Column("api_identifier", sa.String(length=128), nullable=True),
    )
    op.create_foreign_key(
        "fk_products_cloud_server_id",
        "products",
        "cloud_servers",
        ["cloud_server_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.create_index("ix_products_cloud_server_id", "products", ["cloud_server_id"])


def downgrade() -> None:
    op.drop_index("ix_products_cloud_server_id", table_name="products")
    op.drop_constraint("fk_products_cloud_server_id", "products", type_="foreignkey")
    op.drop_column("products", "api_identifier")
    op.drop_column("products", "api_type")
    op.drop_column("products", "cloud_server_id")
