"""add_step_library_sync_fields

Revision ID: fb1481317ff6
Revises: a1b2c3d4e5f6
Create Date: 2026-02-25 03:19:52.600292

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = 'fb1481317ff6'
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.add_column('step_library', sa.Column('source_tree_id', sa.UUID(), nullable=True))
    op.add_column('step_library', sa.Column('source_node_id', sa.String(255), nullable=True))
    op.add_column('step_library', sa.Column('is_flow_synced', sa.Boolean(), nullable=False, server_default='false'))
    op.add_column('step_library', sa.Column('last_synced_at', sa.DateTime(timezone=True), nullable=True))
    op.create_foreign_key(
        'fk_step_library_source_tree',
        'step_library', 'trees',
        ['source_tree_id'], ['id'],
        ondelete='SET NULL'
    )
    op.create_unique_constraint(
        'uq_step_library_source_node',
        'step_library',
        ['source_tree_id', 'source_node_id']
    )
    op.create_index('ix_step_library_source_tree_id', 'step_library', ['source_tree_id'])


def downgrade() -> None:
    op.drop_index('ix_step_library_source_tree_id', 'step_library')
    op.drop_constraint('uq_step_library_source_node', 'step_library', type_='unique')
    op.drop_constraint('fk_step_library_source_tree', 'step_library', type_='foreignkey')
    op.drop_column('step_library', 'last_synced_at')
    op.drop_column('step_library', 'is_flow_synced')
    op.drop_column('step_library', 'source_node_id')
    op.drop_column('step_library', 'source_tree_id')
