"""add stream event log table

Revision ID: a1b2c3d4e5f6
Revises: 9c178034aad7
Create Date: 2026-05-12 17:35:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa

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


def upgrade() -> None:
    op.create_table(
        'core_dingtalk_stream_event_log',
        sa.Column('event_type', sa.String(length=50), nullable=False, comment='事件类型'),
        sa.Column('target_type', sa.String(length=20), nullable=False, comment='目标类型: dept/user'),
        sa.Column('target_name', sa.String(length=100), nullable=True, comment='目标名称'),
        sa.Column('dingtalk_dept_id', sa.String(length=50), nullable=True, comment='钉钉部门ID'),
        sa.Column('dingtalk_userid', sa.String(length=100), nullable=True, comment='钉钉用户ID'),
        sa.Column('status', sa.String(length=20), nullable=True, comment='状态: success/failed'),
        sa.Column('error_detail', sa.Text(), nullable=True, comment='失败详情'),
        sa.Column('event_time', sa.DateTime(), nullable=True, comment='事件时间'),
        # 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_core_dingtalk_stream_event_log_event_type', 'core_dingtalk_stream_event_log', ['event_type'])
    op.create_index('ix_core_dingtalk_stream_event_log_target_type', 'core_dingtalk_stream_event_log', ['target_type'])
    op.create_index('ix_core_dingtalk_stream_event_log_status', 'core_dingtalk_stream_event_log', ['status'])
    op.create_index('ix_core_dingtalk_stream_event_log_is_deleted', 'core_dingtalk_stream_event_log', ['is_deleted'])
    op.create_index('ix_core_dingtalk_stream_event_log_sys_create_datetime', 'core_dingtalk_stream_event_log', ['sys_create_datetime'])


def downgrade() -> None:
    op.drop_index('ix_core_dingtalk_stream_event_log_sys_create_datetime', table_name='core_dingtalk_stream_event_log')
    op.drop_index('ix_core_dingtalk_stream_event_log_is_deleted', table_name='core_dingtalk_stream_event_log')
    op.drop_index('ix_core_dingtalk_stream_event_log_status', table_name='core_dingtalk_stream_event_log')
    op.drop_index('ix_core_dingtalk_stream_event_log_target_type', table_name='core_dingtalk_stream_event_log')
    op.drop_index('ix_core_dingtalk_stream_event_log_event_type', table_name='core_dingtalk_stream_event_log')
    op.drop_table('core_dingtalk_stream_event_log')
