"""cps activity 落地页图: cps_activity.image_url + 回填存量淘宝活动

新建淘宝活动时运营提供落地页主视觉图(上传或选已有),落地页按活动展示对应图。
存量淘宝活动回填为此前写死的那张图,无缝衔接。

Revision ID: cps_activity_image
Revises: cps_v2_platforms
Create Date: 2026-06-17 12:00:00.000000
"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

from app.core.config import settings


revision: str = "cps_activity_image"
down_revision: Union[str, Sequence[str], None] = "cps_v2_platforms"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.add_column("cps_activity", sa.Column("image_url", sa.String(512), nullable=True))
    # 回填存量淘宝活动 = 此前写死的那张图(绝对 URL,基于落地页域名 CPS_REDIRECT_BASE;
    # dev 未配则相对路径)。新活动由运营自传/选,不回填。
    base = settings.CPS_REDIRECT_BASE.rstrip("/")
    default_img = f"{base}/media/taobao_landing.jpg" if base else "/media/taobao_landing.jpg"
    op.execute(
        sa.text(
            "UPDATE cps_activity SET image_url = :img WHERE platform = 'taobao'"
        ).bindparams(img=default_img)
    )


def downgrade() -> None:
    op.drop_column("cps_activity", "image_url")
