"""add_missing_columns

Revision ID: df9117888e82
Revises: 6289854264cb
Create Date: 2026-01-15 09:06:31.308505

"""
from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = 'df9117888e82'
down_revision: Union[str, Sequence[str], None] = '6289854264cb'
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! ###
    # Create custom_integrations table
    op.create_table('custom_integrations',
    sa.Column('id', sa.UUID(as_uuid=False), server_default=sa.text('gen_random_uuid()'), nullable=False),
    sa.Column('organization_id', sa.String(length=255), nullable=False),
    sa.Column('name', sa.String(length=255), nullable=False),
    sa.Column('integration_type', sa.String(length=100), nullable=False),
    sa.Column('description', sa.Text(), nullable=True),
    sa.Column('status', sa.Enum('ACTIVE', 'INACTIVE', 'DELETED', name='customintegrationstatus'), server_default='ACTIVE', nullable=False),
    sa.Column('config', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'{}'::jsonb"), nullable=False),
    sa.Column('tags', postgresql.JSONB(astext_type=sa.Text()), server_default=sa.text("'[]'::jsonb"), nullable=True),
    sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('updated_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
    sa.Column('created_by', sa.String(length=255), nullable=True),
    sa.CheckConstraint("integration_type != ''", name='ck_custom_integration_type_not_empty'),
    sa.CheckConstraint("name != ''", name='ck_custom_integration_name_not_empty'),
    sa.PrimaryKeyConstraint('id')
    )
    op.create_index('idx_custom_integrations_org_name', 'custom_integrations', ['organization_id', 'name'], unique=True)
    op.create_index('idx_custom_integrations_org_type', 'custom_integrations', ['organization_id', 'integration_type'], unique=False)
    op.create_index('idx_custom_integrations_status', 'custom_integrations', ['status'], unique=False)
    op.create_index(op.f('ix_custom_integrations_organization_id'), 'custom_integrations', ['organization_id'], unique=False)

    # Add plan_json to executions and remove plan_execution_id FK
    op.add_column('executions', sa.Column('plan_json', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.drop_index(op.f('idx_executions_plan_execution_id'), table_name='executions')
    op.drop_constraint(op.f('fk_executions_plan_execution'), 'executions', type_='foreignkey')
    op.drop_column('executions', 'plan_execution_id')

    # Add new columns to plan_executions
    op.add_column('plan_executions', sa.Column('plan_generation_id', sa.String(length=255), nullable=True))
    op.add_column('plan_executions', sa.Column('waiting_tasks', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
    op.create_index(op.f('ix_plan_executions_plan_generation_id'), 'plan_executions', ['plan_generation_id'], unique=False)

    # Note: Skipping index modifications for traces/spans to avoid FK cascade issues
    # The existing indexes are functionally equivalent
    # ### end Alembic commands ###


def downgrade() -> None:
    """Downgrade schema."""
    # ### commands auto generated by Alembic - please adjust! ###
    # Restore plan_executions columns
    op.drop_index(op.f('ix_plan_executions_plan_generation_id'), table_name='plan_executions')
    op.drop_column('plan_executions', 'waiting_tasks')
    op.drop_column('plan_executions', 'plan_generation_id')

    # Restore executions.plan_execution_id and remove plan_json
    op.add_column('executions', sa.Column('plan_execution_id', sa.UUID(), autoincrement=False, nullable=True))
    op.create_foreign_key(op.f('fk_executions_plan_execution'), 'executions', 'plan_executions', ['plan_execution_id'], ['id'], ondelete='CASCADE')
    op.create_index(op.f('idx_executions_plan_execution_id'), 'executions', ['plan_execution_id'], unique=False)
    op.drop_column('executions', 'plan_json')

    # Drop custom_integrations table
    op.drop_index(op.f('ix_custom_integrations_organization_id'), table_name='custom_integrations')
    op.drop_index('idx_custom_integrations_status', table_name='custom_integrations')
    op.drop_index('idx_custom_integrations_org_type', table_name='custom_integrations')
    op.drop_index('idx_custom_integrations_org_name', table_name='custom_integrations')
    op.drop_table('custom_integrations')
    # ### end Alembic commands ###
