"""202601051352

Revision ID: 6064f41faac6
Revises: a6452760a140
Create Date: 2026-01-05 13:52:25.739146

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = '6064f41faac6'
down_revision: Union[str, None] = 'a6452760a140'
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.add_column('data_config', sa.Column('decay_constant', sa.Float(), nullable=True, comment='ACT-R衰减常数d，默认0.5'))
    op.add_column('data_config', sa.Column('forgetting_threshold', sa.Float(), nullable=True, comment='遗忘阈值，默认0.3'))
    op.add_column('data_config', sa.Column('forgetting_interval_hours', sa.Integer(), nullable=True, comment='遗忘周期间隔（小时），默认24'))
    op.add_column('data_config', sa.Column('enable_llm_summary', sa.Boolean(), nullable=True, comment='是否使用LLM生成摘要，默认True'))
    op.add_column('data_config', sa.Column('max_merge_batch_size', sa.Integer(), nullable=True, comment='单次最大融合节点对数，默认100'))
    op.add_column('data_config', sa.Column('max_history_length', sa.Integer(), nullable=True, comment='访问历史最大长度，默认100'))
    op.add_column('data_config', sa.Column('min_days_since_access', sa.Integer(), nullable=True, comment='最小未访问天数，默认30'))
    op.add_column('end_users', sa.Column('personality_traits', sa.Text(), nullable=True, comment='性格特点'))
    op.add_column('end_users', sa.Column('core_values', sa.Text(), nullable=True, comment='核心价值观'))
    op.add_column('end_users', sa.Column('one_sentence_summary', sa.Text(), nullable=True, comment='一句话总结'))
    op.add_column('end_users', sa.Column('behavior_pattern', sa.Text(), nullable=True, comment='行为模式'))
    op.add_column('end_users', sa.Column('key_findings', sa.Text(), nullable=True, comment='关键发现'))
    op.add_column('end_users', sa.Column('growth_trajectory', sa.Text(), nullable=True, comment='成长轨迹'))
    
    # Convert BIGINT (milliseconds) to DateTime using raw SQL with USING clause
    op.execute("""
        ALTER TABLE end_users 
        ALTER COLUMN hire_date TYPE timestamp without time zone 
        USING to_timestamp(hire_date / 1000.0)
    """)
    op.execute("COMMENT ON COLUMN end_users.hire_date IS '入职日期'")
    
    op.execute("""
        ALTER TABLE end_users 
        ALTER COLUMN updatetime_profile TYPE timestamp without time zone 
        USING to_timestamp(updatetime_profile / 1000.0)
    """)
    op.execute("COMMENT ON COLUMN end_users.updatetime_profile IS '核心档案信息最后更新时间'")
    
    op.alter_column('end_users', 'user_summary',
               existing_type=sa.TEXT(),
               comment='缓存的用户摘要（基本介绍）',
               existing_comment='缓存的用户摘要',
               existing_nullable=True)
    op.alter_column('end_users', 'memory_insight',
               existing_type=sa.TEXT(),
               comment='缓存的记忆洞察报告（总体概述）',
               existing_comment='缓存的记忆洞察报告',
               existing_nullable=True)
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.alter_column('end_users', 'memory_insight',
               existing_type=sa.TEXT(),
               comment='缓存的记忆洞察报告',
               existing_comment='缓存的记忆洞察报告（总体概述）',
               existing_nullable=True)
    op.alter_column('end_users', 'user_summary',
               existing_type=sa.TEXT(),
               comment='缓存的用户摘要',
               existing_comment='缓存的用户摘要（基本介绍）',
               existing_nullable=True)
    
    # Convert DateTime back to BIGINT (milliseconds) using raw SQL
    op.execute("""
        ALTER TABLE end_users 
        ALTER COLUMN updatetime_profile TYPE bigint 
        USING (EXTRACT(EPOCH FROM updatetime_profile) * 1000)::bigint
    """)
    op.execute("COMMENT ON COLUMN end_users.updatetime_profile IS '核心档案信息最后更新时间（时间戳，毫秒）'")
    
    op.execute("""
        ALTER TABLE end_users 
        ALTER COLUMN hire_date TYPE bigint 
        USING (EXTRACT(EPOCH FROM hire_date) * 1000)::bigint
    """)
    op.execute("COMMENT ON COLUMN end_users.hire_date IS '入职日期（时间戳，毫秒）'")
    
    op.drop_column('end_users', 'growth_trajectory')
    op.drop_column('end_users', 'key_findings')
    op.drop_column('end_users', 'behavior_pattern')
    op.drop_column('end_users', 'one_sentence_summary')
    op.drop_column('end_users', 'core_values')
    op.drop_column('end_users', 'personality_traits')
    op.drop_column('data_config', 'min_days_since_access')
    op.drop_column('data_config', 'max_history_length')
    op.drop_column('data_config', 'max_merge_batch_size')
    op.drop_column('data_config', 'enable_llm_summary')
    op.drop_column('data_config', 'forgetting_interval_hours')
    op.drop_column('data_config', 'forgetting_threshold')
    op.drop_column('data_config', 'decay_constant')
    # ### end Alembic commands ###
