"""013 text_candidates 表加 edited 字段（改稿进飞轮）

Revision ID: 013
Revises: 012
Create Date: 2026-06-16

改稿=用户真改了文案的字=最强真实飞轮信号（倩倩姐2026-06-16拍板，权重+5）。
edited 标记该候选是否被用户改过，配合 SignalType.TEXT_EDIT 写入飞轮偏好事件。
历史行默认 false，避免 NULL。
"""
from alembic import op
import sqlalchemy as sa

revision = "013"
down_revision = "012"
branch_labels = None
depends_on = None


def upgrade():
    op.add_column(
        "text_candidates",
        sa.Column(
            "edited",
            sa.Boolean(),
            nullable=False,
            server_default=sa.false(),
            comment="是否被用户改稿过=最强飞轮信号(text_edit)",
        ),
    )
    # preference_events.signal_type 是 MySQL ENUM,必须把 text_edit 加进枚举值,
    # 否则写 text_edit 信号会 Data truncated 报错(端到端实测抓出)。
    op.execute(
        "ALTER TABLE preference_events MODIFY COLUMN signal_type "
        "ENUM('text_select','image_select','approve','reject_with_reason',"
        "'regenerate','text_edit') NOT NULL"
    )


def downgrade():
    op.execute(
        "ALTER TABLE preference_events MODIFY COLUMN signal_type "
        "ENUM('text_select','image_select','approve','reject_with_reason',"
        "'regenerate') NOT NULL"
    )
    op.drop_column("text_candidates", "edited")
