"""Add OAuthIntegration

Revision ID: d1693ed72940
Revises: 71c8649f752f
Create Date: 2025-06-24 18:55:55.870982

"""

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 = "d1693ed72940"
down_revision: str | None = "71c8649f752f"
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_integration",
        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=True),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), nullable=True),
        sa.Column("provider_id", sa.String(), nullable=False),
        sa.Column("encrypted_access_token", sa.LargeBinary(), nullable=False),
        sa.Column("encrypted_refresh_token", sa.LargeBinary(), nullable=True),
        sa.Column("encrypted_client_id", sa.LargeBinary(), nullable=True),
        sa.Column("encrypted_client_secret", sa.LargeBinary(), nullable=True),
        sa.Column("use_workspace_credentials", sa.Boolean(), nullable=False),
        sa.Column("token_type", sa.String(), nullable=False),
        sa.Column("expires_at", sa.TIMESTAMP(timezone=True), nullable=True),
        sa.Column("scope", sa.String(), nullable=True),
        sa.Column(
            "provider_config", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="oauth_integration_owner_id_fkey",
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
            ondelete="CASCADE",
            name="oauth_integration_user_id_fkey",
        ),
        sa.PrimaryKeyConstraint("id", name="oauth_integration_pkey"),
        sa.UniqueConstraint(
            "owner_id", "provider_id", name="uq_oauth_integration_owner_provider"
        ),
    )
    op.create_index(
        op.f("ix_oauth_integration_id"), "oauth_integration", ["id"], unique=True
    )
    op.create_index(
        op.f("ix_oauth_integration_provider_id"),
        "oauth_integration",
        ["provider_id"],
        unique=False,
    )
    # ### end Alembic commands ###


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