"""add workspace oauth providers

Revision ID: 70144f614d3d
Revises: fba6c5cd4c77
Create Date: 2025-11-05 17:09:39.337627

"""

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 = "70144f614d3d"
down_revision: str | None = "fba6c5cd4c77"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "oauth_provider",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=False),
        sa.Column("provider_id", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column(
            "grant_type",
            postgresql.ENUM(
                "AUTHORIZATION_CODE",
                "CLIENT_CREDENTIALS",
                name="oauthgranttype",
                create_type=False,
            ),
            nullable=False,
        ),
        sa.Column("authorization_endpoint", sa.Text(), nullable=False),
        sa.Column("token_endpoint", sa.Text(), nullable=False),
        sa.Column(
            "scopes",
            postgresql.JSONB(astext_type=sa.Text()),
            server_default=sa.text("'[]'::jsonb"),
            nullable=False,
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="oauth_provider_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="oauth_provider_pkey"),
        sa.UniqueConstraint(
            "owner_id",
            "provider_id",
            "grant_type",
            name="uq_oauth_provider_owner_provider_grant_type",
        ),
    )
    op.create_index(op.f("ix_oauth_provider_id"), "oauth_provider", ["id"], unique=True)
    op.create_index(
        op.f("ix_oauth_provider_provider_id"),
        "oauth_provider",
        ["provider_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_oauth_provider_provider_id"), table_name="oauth_provider")
    op.drop_index(op.f("ix_oauth_provider_id"), table_name="oauth_provider")
    op.drop_table("oauth_provider")
    # ### end Alembic commands ###
