"""Add auth_methods and oauth_type to source model

Revision ID: 813f84a976fd
Revises: 3e29a6bf2817
Create Date: 2025-09-14 14:42:23.881004

"""

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

# revision identifiers, used by Alembic.
revision = "813f84a976fd"
down_revision = "3e29a6bf2817"
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    # First drop the foreign key constraint
    op.drop_constraint(
        "source_connection_white_label_id_fkey", "source_connection", type_="foreignkey"
    )
    # Then drop the column
    op.drop_column("source_connection", "white_label_id")
    # Now we can safely drop the white_label table
    op.drop_table("white_label")

    # Add new columns to source table
    op.add_column("source", sa.Column("auth_methods", sa.JSON(), nullable=True))
    op.add_column("source", sa.Column("oauth_type", sa.String(), nullable=True))
    # Drop the old auth_type column
    op.drop_column("source", "auth_type")
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "source_connection",
        sa.Column("white_label_id", sa.UUID(), autoincrement=False, nullable=True),
    )
    op.create_foreign_key(
        op.f("source_connection_white_label_id_fkey"),
        "source_connection",
        "white_label",
        ["white_label_id"],
        ["id"],
        ondelete="SET NULL",
    )
    op.add_column(
        "source",
        sa.Column(
            "auth_type",
            postgresql.ENUM(
                "oauth2",
                "oauth2_with_refresh",
                "oauth2_with_refresh_rotating",
                "trello_auth",
                "api_key",
                "native_functionality",
                "config_class",
                "none",
                name="authtype",
            ),
            autoincrement=False,
            nullable=True,
        ),
    )
    op.drop_column("source", "oauth_type")
    op.drop_column("source", "auth_methods")
    op.create_table(
        "white_label",
        sa.Column("name", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("source_short_name", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("redirect_url", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("client_id", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("client_secret", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.Column("organization_id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("id", sa.UUID(), autoincrement=False, nullable=False),
        sa.Column("created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("modified_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=False),
        sa.Column("created_by_email", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("modified_by_email", sa.VARCHAR(), autoincrement=False, nullable=True),
        sa.Column("allowed_origins", sa.VARCHAR(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["organization_id"],
            ["organization.id"],
            name=op.f("white_label_organization_id_fkey"),
            ondelete="CASCADE",
        ),
        sa.ForeignKeyConstraint(
            ["source_short_name"],
            ["source.short_name"],
            name=op.f("white_label_source_short_name_fkey"),
        ),
        sa.PrimaryKeyConstraint("id", name=op.f("white_label_pkey")),
    )
    # ### end Alembic commands ###
