"""empty message

Revision ID: fa7f7ef37e5e
Revises: 644500e01519
Create Date: 2026-04-25 10:27:10.700973

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from db.models.tasks.shared import TaskUsage
from db.models.utils import DataClassJSON
from db.models.utils import PydanticJSON

# revision identifiers, used by Alembic.
revision: str = 'fa7f7ef37e5e'
down_revision: Union[str, Sequence[str], None] = '644500e01519'
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('schedules',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('name', sa.String(), nullable=False),
    sa.Column('task', sa.String(), nullable=False),
    sa.Column('is_enabled', sa.Boolean(), nullable=False),
    sa.Column('config', PydanticJSON(None), nullable=False),
    sa.Column('agent_id', sa.Integer(), nullable=True),
    sa.Column('_workspace_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['_workspace_id'], ['workspaces.id'], ),
    sa.ForeignKeyConstraint(['agent_id'], ['agents.id'], ondelete='SET NULL'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_table('run_records',
    sa.Column('id', sa.Integer(), nullable=False),
    sa.Column('run_at', sa.Integer(), nullable=False),
    sa.Column('usage', DataClassJSON(TaskUsage), nullable=False),
    sa.Column('messages', PydanticJSON(None), nullable=False),
    sa.Column('schedule_id', sa.Integer(), nullable=False),
    sa.ForeignKeyConstraint(['schedule_id'], ['schedules.id'], ondelete='CASCADE'),
    sa.PrimaryKeyConstraint('id')
    )

    with op.batch_alter_table('task_resources', schema=None) as batch_op:
        batch_op.add_column(sa.Column('owner_type', sa.String(), nullable=True))
        batch_op.add_column(sa.Column('owner_id', sa.Integer(), nullable=True))

    op.execute("UPDATE task_resources SET owner_type = 'tasks', owner_id = _task_id")

    with op.batch_alter_table('task_resources', schema=None) as batch_op:
        batch_op.alter_column('owner_type', nullable=False)
        batch_op.alter_column('owner_id', nullable=False)
        batch_op.create_index('ix_task_resources_owner', ['owner_type', 'owner_id'], unique=False)
        batch_op.drop_constraint(batch_op.f('fk_task_resources__task_id_tasks'), type_='foreignkey')
        batch_op.drop_column('_task_id')

    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    with op.batch_alter_table('task_resources', schema=None) as batch_op:
        batch_op.add_column(sa.Column('_task_id', sa.INTEGER(), nullable=True))

    op.execute("UPDATE task_resources SET _task_id = owner_id WHERE owner_type = 'tasks'")

    with op.batch_alter_table('task_resources', schema=None) as batch_op:
        batch_op.alter_column('_task_id', nullable=False)
        batch_op.create_foreign_key(batch_op.f('fk_task_resources__task_id_tasks'), 'tasks', ['_task_id'], ['id'], ondelete='CASCADE')
        batch_op.drop_index('ix_task_resources_owner')
        batch_op.drop_column('owner_id')
        batch_op.drop_column('owner_type')

    op.drop_table('run_records')
    op.drop_table('schedules')
    # ### end Alembic commands ###
