"""add trusted application sso integration

Revision ID: 0029_sso_integration
Revises: 0028_chat_concurrency
"""

from __future__ import annotations

from alembic import op
import sqlalchemy as sa


revision = "0029_sso_integration"
down_revision = "0028_chat_concurrency"
branch_labels = None
depends_on = None

PRIMARY_KEY_TYPE = sa.BigInteger().with_variant(sa.Integer(), "sqlite")


def upgrade() -> None:
    op.create_table(
        "sys_sso_client",
        sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
        sa.Column("app_id", sa.String(length=80), nullable=False),
        sa.Column("name", sa.String(length=100), nullable=False),
        sa.Column("client_secret", sa.Text(), nullable=False),
        sa.Column("redirect_uris", sa.Text(), nullable=False),
        sa.Column("status", sa.Integer(), nullable=False, server_default="1"),
        sa.Column("created_by", sa.BigInteger(), nullable=True),
        sa.Column("last_used_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("ix_sys_sso_client_app_id", "sys_sso_client", ["app_id"], unique=True)
    op.create_index("ix_sys_sso_client_status", "sys_sso_client", ["status"])

    op.create_table(
        "sys_user_external_identity",
        sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
        sa.Column("client_id", sa.BigInteger(), nullable=False),
        sa.Column("user_id", sa.BigInteger(), nullable=False),
        sa.Column("external_user_id", sa.String(length=120), nullable=False),
        sa.Column("phone_snapshot", sa.String(length=20), nullable=True),
        sa.Column("display_name_snapshot", sa.String(length=100), nullable=True),
        sa.Column("last_login_at", sa.DateTime(), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
        sa.ForeignKeyConstraint(["client_id"], ["sys_sso_client.id"]),
        sa.ForeignKeyConstraint(["user_id"], ["sys_user.id"]),
        sa.UniqueConstraint("client_id", "external_user_id", name="uq_sso_identity_client_external"),
        sa.UniqueConstraint("client_id", "user_id", name="uq_sso_identity_client_user"),
    )
    op.create_index("ix_sys_user_external_identity_client_id", "sys_user_external_identity", ["client_id"])
    op.create_index("ix_sso_identity_user", "sys_user_external_identity", ["user_id"])

    op.create_table(
        "sys_sso_login_audit",
        sa.Column("id", PRIMARY_KEY_TYPE, primary_key=True, autoincrement=True),
        sa.Column("client_id", sa.BigInteger(), nullable=True),
        sa.Column("user_id", sa.BigInteger(), nullable=True),
        sa.Column("external_user_id", sa.String(length=120), nullable=True),
        sa.Column("action", sa.String(length=30), nullable=False),
        sa.Column("status", sa.String(length=20), nullable=False),
        sa.Column("error_message", sa.String(length=500), nullable=True),
        sa.Column("ip", sa.String(length=50), nullable=True),
        sa.Column("created_at", sa.DateTime(), server_default=sa.func.now(), nullable=False),
    )
    op.create_index("ix_sso_audit_client_created", "sys_sso_login_audit", ["client_id", "created_at"])
    op.create_index("ix_sso_audit_user_created", "sys_sso_login_audit", ["user_id", "created_at"])
    op.create_index("ix_sso_audit_status_created", "sys_sso_login_audit", ["status", "created_at"])


def downgrade() -> None:
    op.drop_table("sys_sso_login_audit")
    op.drop_table("sys_user_external_identity")
    op.drop_table("sys_sso_client")
