"""create fission_notes table

Revision ID: 018
Revises: 017
Create Date: 2026-06-18

裂变重写（对齐上线版 split.js）：一次LLM出N套完整笔记包，每套落一行。
每套含 标题/正文/标签/封面/imagePlan/裂变维度，带评分+生图结果。
"""
from alembic import op
import sqlalchemy as sa

revision = "018"
down_revision = "017"
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        "fission_notes",
        sa.Column("id", sa.BigInteger, primary_key=True, autoincrement=True),
        sa.Column(
            "fission_id", sa.BigInteger,
            sa.ForeignKey("fission_tasks.id", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column(
            "workspace_id", sa.BigInteger,
            sa.ForeignKey("workspaces.id", ondelete="CASCADE"),
            nullable=False, comment="多租户隔离",
        ),
        sa.Column("seq", sa.Integer, nullable=False, server_default="0",
                  comment="第几套（0起）"),
        sa.Column("note_json", sa.Text, nullable=True, comment="完整笔记包JSON"),
        sa.Column("images_json", sa.Text, nullable=True, comment="生图结果JSON"),
        sa.Column("score", sa.Integer, nullable=False, server_default="0"),
        sa.Column("passed", sa.Integer, nullable=False, server_default="0",
                  comment="是否过线(>=80)"),
        sa.Column("needs_optimization", sa.Integer, nullable=False,
                  server_default="0", comment="不达标降级草稿标记"),
        sa.Column("dimension", sa.String(64), nullable=True,
                  comment="裂变维度: 换角度/换痛点/换人群"),
        sa.Column("status", sa.String(20), nullable=False,
                  server_default="pending",
                  comment="状态: pending/scored/image_done/failed"),
        sa.Column("created_at", sa.DateTime, server_default=sa.func.now(),
                  nullable=False),
    )
    op.create_index("idx_fission_notes_fission_id", "fission_notes", ["fission_id"])
    op.create_index("idx_fission_notes_workspace_id", "fission_notes", ["workspace_id"])


def downgrade():
    op.drop_index("idx_fission_notes_workspace_id", table_name="fission_notes")
    op.drop_index("idx_fission_notes_fission_id", table_name="fission_notes")
    op.drop_table("fission_notes")
