"""add dingtalk todo record table

Revision ID: b2c3d4e5f6g7
Revises: a1b2c3d4e5f6
Create Date: 2026-05-13 19:50:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision: str = 'b2c3d4e5f6g7'
down_revision: Union[str, None] = 'a1b2c3d4e5f6'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        'core_dingtalk_todo_record',
        sa.Column('source_type', sa.String(length=50), nullable=False, comment='来源类型: workflow_task/workflow_instance/message'),
        sa.Column('source_id', sa.String(length=50), nullable=False, comment='业务ID'),
        sa.Column('user_id', sa.String(length=50), nullable=False, comment='系统用户ID'),
        sa.Column('dingtalk_union_id', sa.String(length=100), nullable=False, comment='钉钉unionId'),
        sa.Column('dingtalk_task_id', sa.String(length=100), nullable=False, comment='钉钉待办taskId'),
        sa.Column('status', sa.String(length=20), nullable=True, server_default='pending', comment='状态: pending/done/deleted'),
        # BaseModel columns
        sa.Column('id', sa.String(length=21), nullable=False, comment='主键ID(NanoId)'),
        sa.Column('sort', sa.Integer(), nullable=True, comment='排序'),
        sa.Column('is_deleted', sa.Boolean(), nullable=True, comment='是否删除'),
        sa.Column('sys_create_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='创建时间'),
        sa.Column('sys_update_datetime', sa.DateTime(), server_default=sa.text('now()'), nullable=True, comment='更新时间'),
        sa.Column('sys_creator_id', sa.String(length=21), nullable=True, comment='创建人ID'),
        sa.Column('sys_modifier_id', sa.String(length=21), nullable=True, comment='修改人ID'),
        sa.Column('sys_dept_id', sa.String(length=21), nullable=True, comment='部门ID'),
        sa.PrimaryKeyConstraint('id'),
    )
    op.create_index('ix_dingtalk_todo_source', 'core_dingtalk_todo_record', ['source_type', 'source_id'])
    op.create_index('ix_dingtalk_todo_source_type', 'core_dingtalk_todo_record', ['source_type'])
    op.create_index('ix_dingtalk_todo_source_id', 'core_dingtalk_todo_record', ['source_id'])
    op.create_index('ix_dingtalk_todo_user_id', 'core_dingtalk_todo_record', ['user_id'])
    op.create_index('ix_dingtalk_todo_status', 'core_dingtalk_todo_record', ['status'])
    op.create_index('ix_dingtalk_todo_is_deleted', 'core_dingtalk_todo_record', ['is_deleted'])


def downgrade() -> None:
    op.drop_index('ix_dingtalk_todo_is_deleted', table_name='core_dingtalk_todo_record')
    op.drop_index('ix_dingtalk_todo_status', table_name='core_dingtalk_todo_record')
    op.drop_index('ix_dingtalk_todo_user_id', table_name='core_dingtalk_todo_record')
    op.drop_index('ix_dingtalk_todo_source_id', table_name='core_dingtalk_todo_record')
    op.drop_index('ix_dingtalk_todo_source_type', table_name='core_dingtalk_todo_record')
    op.drop_index('ix_dingtalk_todo_source', table_name='core_dingtalk_todo_record')
    op.drop_table('core_dingtalk_todo_record')
