"""补齐产品计划配置与产品操作日志。"""

import sqlalchemy as sa
from alembic import op

from zbt.infrastructure.mysql.core_models import CoreBase

revision = "core_0004"
down_revision = "core_0003"
branch_labels = None
depends_on = None


def upgrade() -> None:
    bind = op.get_bind()
    inspector = sa.inspect(bind)
    existing_columns = {
        str(column["name"]) for column in inspector.get_columns("plans")
    }
    columns = (
        sa.Column(
            "premium_cents",
            sa.BigInteger(),
            nullable=False,
            server_default="0",
        ),
        sa.Column(
            "coverage_amount_cents",
            sa.BigInteger(),
            nullable=False,
            server_default="0",
        ),
        sa.Column("min_age", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("max_age", sa.Integer(), nullable=False, server_default="100"),
    )
    for column in columns:
        if column.name not in existing_columns:
            op.add_column("plans", column)
    if not inspector.has_table("product_change_logs"):
        CoreBase.metadata.tables["product_change_logs"].create(bind=bind)


def downgrade() -> None:
    CoreBase.metadata.tables["product_change_logs"].drop(bind=op.get_bind())
    op.drop_column("plans", "max_age")
    op.drop_column("plans", "min_age")
    op.drop_column("plans", "coverage_amount_cents")
    op.drop_column("plans", "premium_cents")
