"""add batch_logs table

Revision ID: 0004
Revises: 0003
Create Date: 2026-02-18 00:00:00.000000

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


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


def upgrade() -> None:
    op.create_table(
        'batch_logs',
        sa.Column('id', sa.Integer(), nullable=False),
        sa.Column('batch_name', sa.String(length=100), nullable=False, comment='批次名稱'),
        sa.Column('status', sa.String(length=20), nullable=False, comment='執行狀態: success/failed/warning'),
        sa.Column('message', sa.Text(), nullable=True, comment='執行訊息或錯誤詳情'),
        sa.Column('started_at', sa.DateTime(), nullable=False, comment='開始時間'),
        sa.Column('finished_at', sa.DateTime(), nullable=True, comment='完成時間'),
        sa.Column('duration_seconds', sa.Integer(), nullable=True, comment='執行時間 (秒)'),
        sa.PrimaryKeyConstraint('id')
    )
    op.create_index('ix_batch_logs_id', 'batch_logs', ['id'], unique=False)
    op.create_index('ix_batch_logs_batch_name', 'batch_logs', ['batch_name'], unique=False)
    op.create_index('ix_batch_logs_started_at', 'batch_logs', ['started_at'], unique=False)


def downgrade() -> None:
    op.drop_index('ix_batch_logs_started_at', table_name='batch_logs')
    op.drop_index('ix_batch_logs_batch_name', table_name='batch_logs')
    op.drop_index('ix_batch_logs_id', table_name='batch_logs')
    op.drop_table('batch_logs')
