"""replace provider_config with endpoint columns

Revision ID: 0aef095985d4
Revises: b376e6d16619
Create Date: 2025-11-03 18:44:21.747097

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "0aef095985d4"
down_revision: str | None = "b376e6d16619"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # Delete all existing OAuth integrations as they won't have endpoint configuration
    # Users have been notified to reconfigure their OAuth integrations after this migration
    op.execute("DELETE FROM oauth_integration;")

    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "oauth_integration",
        sa.Column("authorization_endpoint", sa.Text(), nullable=True),
    )
    op.add_column(
        "oauth_integration", sa.Column("token_endpoint", sa.Text(), nullable=True)
    )
    op.drop_column("oauth_integration", "provider_config")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "oauth_integration",
        sa.Column(
            "provider_config",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
    )
    op.drop_column("oauth_integration", "token_endpoint")
    op.drop_column("oauth_integration", "authorization_endpoint")
    # ### end Alembic commands ###
