"""add intelligent eval cron pool tables

Revision ID: b72debf55c3b
Revises: d4e7f9a1b2c3
Create Date: 2026-08-12 01:45:36.671065

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel


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


def upgrade() -> None:
    """Upgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table('intelligent_eval_config_snapshots',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('eval_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('snapshot_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('goal', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('seeds', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('intent', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('role_description', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('time_window_hours', sa.Integer(), nullable=False),
    sa.Column('plan', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('created_by', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.ForeignKeyConstraint(['eval_id'], ['intelligent_evals.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('intelligent_eval_config_snapshots', schema=None) as batch_op:
        batch_op.create_index('idx_config_snapshots_eval_created', ['eval_id', 'created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_intelligent_eval_config_snapshots_eval_id'), ['eval_id'], unique=False)

    op.create_table('intelligent_eval_decision_logs',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('eval_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('decision_type', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('reason', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('context', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('cron_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['eval_id'], ['intelligent_evals.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('intelligent_eval_decision_logs', schema=None) as batch_op:
        batch_op.create_index('idx_decision_logs_eval_created', ['eval_id', 'created_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_intelligent_eval_decision_logs_eval_id'), ['eval_id'], unique=False)

    op.create_table('intelligent_eval_task_queue',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('eval_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('status', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('priority', sa.Integer(), nullable=False),
    sa.Column('reason', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('assigned_cron_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('assigned_at', sa.DateTime(), nullable=True),
    sa.Column('completed_at', sa.DateTime(), nullable=True),
    sa.Column('error', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['eval_id'], ['intelligent_evals.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('intelligent_eval_task_queue', schema=None) as batch_op:
        batch_op.create_index('idx_task_queue_eval_status', ['eval_id', 'status'], unique=False)
        batch_op.create_index('idx_task_queue_status_priority', ['status', 'priority'], unique=False)
        batch_op.create_index(batch_op.f('ix_intelligent_eval_task_queue_eval_id'), ['eval_id'], unique=False)
        batch_op.create_index(batch_op.f('ix_intelligent_eval_task_queue_priority'), ['priority'], unique=False)
        batch_op.create_index(batch_op.f('ix_intelligent_eval_task_queue_status'), ['status'], unique=False)

    op.create_table('openclaw_cron_pool',
    sa.Column('id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('openclaw_cron_id', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('status', sqlmodel.sql.sqltypes.AutoString(), nullable=False),
    sa.Column('current_eval_id', sqlmodel.sql.sqltypes.AutoString(), nullable=True),
    sa.Column('last_active_at', sa.DateTime(), nullable=False),
    sa.Column('last_task_at', sa.DateTime(), nullable=True),
    sa.Column('total_tasks_completed', sa.Integer(), nullable=False),
    sa.Column('total_tasks_failed', sa.Integer(), nullable=False),
    sa.Column('created_at', sa.DateTime(), nullable=True),
    sa.Column('updated_at', sa.DateTime(), nullable=True),
    sa.ForeignKeyConstraint(['current_eval_id'], ['intelligent_evals.id'], ),
    sa.PrimaryKeyConstraint('id')
    )
    with op.batch_alter_table('openclaw_cron_pool', schema=None) as batch_op:
        batch_op.create_index('idx_cron_pool_status_last_active', ['status', 'last_active_at'], unique=False)
        batch_op.create_index(batch_op.f('ix_openclaw_cron_pool_openclaw_cron_id'), ['openclaw_cron_id'], unique=True)
        batch_op.create_index(batch_op.f('ix_openclaw_cron_pool_status'), ['status'], unique=False)

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('openclaw_cron_pool', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_openclaw_cron_pool_status'))
        batch_op.drop_index(batch_op.f('ix_openclaw_cron_pool_openclaw_cron_id'))
        batch_op.drop_index('idx_cron_pool_status_last_active')

    op.drop_table('openclaw_cron_pool')
    with op.batch_alter_table('intelligent_eval_task_queue', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_intelligent_eval_task_queue_status'))
        batch_op.drop_index(batch_op.f('ix_intelligent_eval_task_queue_priority'))
        batch_op.drop_index(batch_op.f('ix_intelligent_eval_task_queue_eval_id'))
        batch_op.drop_index('idx_task_queue_status_priority')
        batch_op.drop_index('idx_task_queue_eval_status')

    op.drop_table('intelligent_eval_task_queue')
    with op.batch_alter_table('intelligent_eval_decision_logs', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_intelligent_eval_decision_logs_eval_id'))
        batch_op.drop_index('idx_decision_logs_eval_created')

    op.drop_table('intelligent_eval_decision_logs')
    with op.batch_alter_table('intelligent_eval_config_snapshots', schema=None) as batch_op:
        batch_op.drop_index(batch_op.f('ix_intelligent_eval_config_snapshots_eval_id'))
        batch_op.drop_index('idx_config_snapshots_eval_created')

    op.drop_table('intelligent_eval_config_snapshots')
    # ### end Alembic commands ###
