"""add SSO plugin tables sso_user_profile and sso_config

Revision ID: b1c2d3e4f5a6
Revises: 369268b9af8b
Create Date: 2026-02-24

Phase: EXPAND
"""
from collections.abc import Sequence

import sqlalchemy as sa
import sqlmodel
from alembic import op

from langflow.utils import migration

# revision identifiers, used by Alembic.
revision: str = "b1c2d3e4f5a6"
down_revision: str | None = "369268b9af8b"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    conn = op.get_bind()
    if not migration.table_exists("sso_config", conn):
        op.create_table(
            "sso_config",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("provider", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("provider_name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("enabled", sa.Boolean(), nullable=False),
            sa.Column("enforce_sso", sa.Boolean(), nullable=False),
            sa.Column("client_id", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("client_secret_encrypted", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("discovery_url", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("redirect_uri", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("scopes", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("email_claim", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("username_claim", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("user_id_claim", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("token_endpoint", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("authorization_endpoint", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("jwks_uri", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("issuer", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), nullable=False),
            sa.Column("created_by", sa.Uuid(), nullable=True),
            sa.ForeignKeyConstraint(["created_by"], ["user.id"], ondelete="SET NULL"),
            sa.PrimaryKeyConstraint("id"),
        )
    if not migration.table_exists("sso_user_profile", conn):
        op.create_table(
            "sso_user_profile",
            sa.Column("id", sa.Uuid(), nullable=False),
            sa.Column("user_id", sa.Uuid(), nullable=False),
            sa.Column("sso_provider", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("sso_user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
            sa.Column("email", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
            sa.Column("sso_last_login_at", sa.DateTime(), nullable=True),
            sa.Column("created_at", sa.DateTime(), nullable=False),
            sa.Column("updated_at", sa.DateTime(), nullable=False),
            sa.ForeignKeyConstraint(["user_id"], ["user.id"], ondelete="CASCADE"),
            sa.PrimaryKeyConstraint("id"),
        )
        with op.batch_alter_table("sso_user_profile", schema=None) as batch_op:
            batch_op.create_index(batch_op.f("ix_sso_user_profile_email"), ["email"], unique=False)
            batch_op.create_index(
                batch_op.f("uq_sso_user_profile_provider_user"),
                ["sso_provider", "sso_user_id"],
                unique=True,
            )
            batch_op.create_index(batch_op.f("ix_sso_user_profile_user_id"), ["user_id"], unique=True)


def downgrade() -> None:
    conn = op.get_bind()
    if migration.table_exists("sso_user_profile", conn):
        with op.batch_alter_table("sso_user_profile", schema=None) as batch_op:
            batch_op.drop_index(batch_op.f("ix_sso_user_profile_user_id"))
            batch_op.drop_index(batch_op.f("uq_sso_user_profile_provider_user"))
            batch_op.drop_index(batch_op.f("ix_sso_user_profile_email"))
        op.drop_table("sso_user_profile")
    if migration.table_exists("sso_config", conn):
        op.drop_table("sso_config")
