"""tasks.cost_usd / usage_events.cost_usd rename → cost_cny (×7.2 backfill).

Revision ID: 0007
Revises: 0006
Create Date: 2026-05-20

按 PROGRESS / DESIGN:接入豆包(seedream/seedance)按张/按 token 计费,价格单位是人民币,
不再走 litellm USD cost map → 全表统一币种 CNY,免 chat USD / media CNY 分类汇总。

CLAUDE.md "开发期不写兼容":直接 rename 列 + ×7.2 一次性折算开发期 chat 数据
(汇率近似,误差可接受;后续新写入 record_chat_usage 内部把 litellm USD ×7.2 落 CNY)。
"""
from typing import Sequence, Union

from alembic import op


revision: str = "0007"
down_revision: Union[str, None] = "0006"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


_USD_TO_CNY = 7.2


def upgrade() -> None:
    # 先 rename(保留已有数据值,只换列名),再 ×7.2 折算
    op.alter_column("tasks", "cost_usd", new_column_name="cost_cny")
    op.alter_column("usage_events", "cost_usd", new_column_name="cost_cny")

    op.execute(f"UPDATE tasks SET cost_cny = cost_cny * {_USD_TO_CNY}")
    op.execute(f"UPDATE usage_events SET cost_cny = cost_cny * {_USD_TO_CNY}")


def downgrade() -> None:
    op.execute(f"UPDATE usage_events SET cost_cny = cost_cny / {_USD_TO_CNY}")
    op.execute(f"UPDATE tasks SET cost_cny = cost_cny / {_USD_TO_CNY}")
    op.alter_column("usage_events", "cost_cny", new_column_name="cost_usd")
    op.alter_column("tasks", "cost_cny", new_column_name="cost_usd")
