"""20251118112827

Revision ID: 2df9cdfc5087
Revises: 9971d42f0e8c
Create Date: 2025-11-18 11:28:27.981687

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = '2df9cdfc5087'
down_revision: Union[str, None] = '9971d42f0e8c'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('conversations',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('app_id', sa.UUID(), nullable=False, comment='应用ID'),
    sa.Column('user_id', sa.String(), nullable=True, comment='用户ID（外部系统）'),
    sa.Column('title', sa.String(length=255), nullable=True, comment='会话标题'),
    sa.Column('summary', sa.Text(), nullable=True, comment='会话摘要'),
    sa.Column('is_draft', sa.Boolean(), nullable=False, comment='是否为草稿会话'),
    sa.Column('config_snapshot', sa.JSON(), nullable=True, comment='配置快照（Agent配置、模型配置等）'),
    sa.Column('message_count', sa.Integer(), nullable=True, comment='消息数量'),
    sa.Column('is_active', sa.Boolean(), nullable=False, comment='是否活跃'),
    sa.Column('created_at', sa.DateTime(), nullable=True, comment='创建时间'),
    sa.Column('updated_at', sa.DateTime(), nullable=True, comment='更新时间'),
    sa.ForeignKeyConstraint(['app_id'], ['apps.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_conversations_id'), 'conversations', ['id'], unique=False)
    op.create_table('messages',
    sa.Column('id', sa.UUID(), nullable=False),
    sa.Column('conversation_id', sa.UUID(), nullable=False, comment='会话ID'),
    sa.Column('role', sa.String(length=20), nullable=False, comment='角色: user/assistant/system'),
    sa.Column('content', sa.Text(), nullable=False, comment='消息内容'),
    sa.Column('meta_data', sa.JSON(), nullable=True, comment='消息元数据（如模型、token使用等）'),
    sa.Column('created_at', sa.DateTime(), nullable=True, comment='创建时间'),
    sa.ForeignKeyConstraint(['conversation_id'], ['conversations.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index(op.f('ix_messages_id'), 'messages', ['id'], unique=False)
    op.drop_table('data_config')
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('data_config',
    sa.Column('config_id', sa.BIGINT(), sa.Identity(always=True, start=1, increment=1, minvalue=1, maxvalue=9223372036854775807, cycle=False, cache=1), autoincrement=True, nullable=False),
    sa.Column('config_name', sa.VARCHAR(length=255), server_default=sa.text("'配置文件'::character varying"), autoincrement=False, nullable=False),
    sa.Column('group_id', sa.VARCHAR(length=255), server_default=sa.text("'group_id'::character varying"), autoincrement=False, nullable=False),
    sa.Column('user_id', sa.VARCHAR(length=255), server_default=sa.text("'user_id'::character varying"), autoincrement=False, nullable=False),
    sa.Column('apply_id', sa.VARCHAR(length=255), server_default=sa.text("'apply_id'::character varying"), autoincrement=False, nullable=False),
    sa.Column('enable_llm_dedup_blockwise', sa.BOOLEAN(), server_default=sa.text('true'), autoincrement=False, nullable=False),
    sa.Column('enable_llm_disambiguation', sa.BOOLEAN(), server_default=sa.text('true'), autoincrement=False, nullable=False),
    sa.Column('deep_retrieval', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False),
    sa.Column('t_type_strict', sa.NUMERIC(), server_default=sa.text('0.5'), autoincrement=False, nullable=False),
    sa.Column('t_name_strict', sa.NUMERIC(), server_default=sa.text('0.8'), autoincrement=False, nullable=False),
    sa.Column('t_overall', sa.NUMERIC(), server_default=sa.text('0.56'), autoincrement=False, nullable=False),
    sa.Column('chunker_strategy', postgresql.ENUM('RecursiveChunker', 'TokenChunker', 'SemanticChunker', 'NeuralChunker', 'HybridChunker', 'LLMChunker', 'SentenceChunker', 'LateChunker', name='chunker_strategy_enum'), server_default=sa.text("'RecursiveChunker'::chunker_strategy_enum"), autoincrement=False, nullable=False, comment='选择分块策略：    \r\n    "RecursiveChunker",\r\n    "TokenChunker",\r\n    "SemanticChunker",\r\n    "NeuralChunker",\r\n    "HybridChunker",\r\n    "LLMChunker",\r\n    "SentenceChunker",\r\n    "LateChunker"'),
    sa.Column('statement_granularity', sa.VARCHAR(length=255), server_default=sa.text("'2'::character varying"), autoincrement=False, nullable=True),
    sa.Column('include_dialogue_context', sa.BOOLEAN(), server_default=sa.text('true'), autoincrement=False, nullable=True),
    sa.Column('max_context', sa.VARCHAR(length=255), server_default=sa.text("'200'::character varying"), autoincrement=False, nullable=True),
    sa.Column('λ_time', sa.NUMERIC(), server_default=sa.text('0.3'), autoincrement=False, nullable=True),
    sa.Column('λ_mem', sa.NUMERIC(), server_default=sa.text('0.4'), autoincrement=False, nullable=True),
    sa.Column('offset', sa.NUMERIC(precision=255, scale=0), server_default=sa.text('0.56'), autoincrement=False, nullable=True),
    sa.Column('state', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=False),
    sa.Column('created_at', postgresql.TIMESTAMP(timezone=True, precision=6), server_default=sa.text('CURRENT_TIMESTAMP'), autoincrement=False, nullable=False),
    sa.Column('updated_at', postgresql.TIMESTAMP(timezone=True, precision=6), server_default=sa.text('CURRENT_TIMESTAMP'), autoincrement=False, nullable=False),
    sa.Column('config_desc', sa.TEXT(), server_default=sa.text("'配置文件描述'::text"), autoincrement=False, nullable=False),
    sa.Column('workspace_id', sa.VARCHAR(length=255), server_default=sa.text("'workspace_id'::character varying"), autoincrement=False, nullable=False),
    sa.Column('enable_self_reflexion', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=True),
    sa.Column('iteration_period', sa.VARCHAR(length=50), server_default=sa.text("'三小时'::character varying"), autoincrement=False, nullable=True),
    sa.Column('baseline', sa.VARCHAR(length=50), server_default=sa.text("'时间'::character varying"), autoincrement=False, nullable=True),
    sa.Column('reflexion_range', sa.VARCHAR(length=25), server_default=sa.text("'部分'::character varying"), autoincrement=False, nullable=True),
    sa.Column('pruning_enabled', sa.BOOLEAN(), server_default=sa.text('false'), autoincrement=False, nullable=True),
    sa.Column('pruning_scene', sa.VARCHAR(length=255), server_default=sa.text("'education'::character varying"), autoincrement=False, nullable=True),
    sa.Column('pruning_threshold', sa.REAL(), server_default=sa.text('0.3'), autoincrement=False, nullable=True)
    )
    op.drop_index(op.f('ix_messages_id'), table_name='messages')
    op.drop_table('messages')
    op.drop_index(op.f('ix_conversations_id'), table_name='conversations')
    op.drop_table('conversations')
    # ### end Alembic commands ###
