"""add auth schema

Revision ID: 552e7a8dec07
Revises: 5b9dd35eb10b
Create Date: 2024-10-02 14:48:49.354845

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
import sqlmodel  # noqa F401


# revision identifiers, used by Alembic.
revision: str = "552e7a8dec07"
down_revision: Union[str, None] = "5b9dd35eb10b"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "User",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", sa.TEXT(), nullable=False),
        sa.Column("name", sa.TEXT(), nullable=True),
        sa.Column("email", sa.TEXT(), nullable=False),
        sa.Column("email_verified", sa.TIMESTAMP(), nullable=True),
        sa.Column("image", sa.TEXT(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
    )
    op.create_table(
        "VerificationToken",
        sa.Column("identifier", sa.TEXT(), nullable=False),
        sa.Column("token", sa.TEXT(), nullable=False),
        sa.Column("expires", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("identifier", "token"),
        sa.UniqueConstraint("identifier", "token"),
    )
    op.create_table(
        "Account",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("userId", sa.TEXT(), nullable=False),
        sa.Column("type", sa.TEXT(), nullable=False),
        sa.Column("provider", sa.TEXT(), nullable=False),
        sa.Column("providerAccountId", sa.TEXT(), nullable=False),
        sa.Column("refresh_token", sa.TEXT(), nullable=True),
        sa.Column("access_token", sa.TEXT(), nullable=True),
        sa.Column("expires_at", sa.Integer(), nullable=True),
        sa.Column("token_type", sa.TEXT(), nullable=True),
        sa.Column("scope", sa.TEXT(), nullable=True),
        sa.Column("id_token", sa.TEXT(), nullable=True),
        sa.Column("session_state", sa.TEXT(), nullable=True),
        sa.ForeignKeyConstraint(["userId"], ["User.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("provider", "providerAccountId"),
        sa.UniqueConstraint("provider", "providerAccountId"),
    )
    op.create_table(
        "Authenticator",
        sa.Column("credentialID", sa.TEXT(), nullable=False),
        sa.Column("userId", sa.TEXT(), nullable=False),
        sa.Column("providerAccountId", sa.TEXT(), nullable=False),
        sa.Column("credentialPublicKey", sa.TEXT(), nullable=False),
        sa.Column("counter", sa.Integer(), nullable=False),
        sa.Column("credentialDeviceType", sa.TEXT(), nullable=False),
        sa.Column("credentialBackedUp", sa.BOOLEAN(), nullable=False),
        sa.Column("transports", sa.TEXT(), nullable=True),
        sa.ForeignKeyConstraint(["userId"], ["User.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("credentialID", "userId"),
        sa.UniqueConstraint("credentialID"),
    )
    op.create_table(
        "Session",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", sa.TEXT(), nullable=False),
        sa.Column("sessionToken", sa.TEXT(), nullable=False),
        sa.Column("userId", sa.TEXT(), nullable=False),
        sa.Column("expires", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(["userId"], ["User.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("sessionToken"),
    )
    op.create_table(
        "api_keys",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("id", sa.TEXT(), nullable=False),
        sa.Column("user_id", sa.TEXT(), nullable=False),
        sa.Column("name", sa.TEXT(), nullable=False),
        sa.Column("key", sa.TEXT(), nullable=False),
        sa.ForeignKeyConstraint(["user_id"], ["User.id"], ondelete="CASCADE"),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_api_keys_key"), "api_keys", ["key"], unique=False)
    op.create_index(op.f("ix_api_keys_user_id"), "api_keys", ["user_id"], unique=False)

    DEFAULT_USER_ID = "38815447-e272-4299-94c0-29a2d30435f9"
    op.execute(
        f"INSERT INTO \"User\" (id, email) VALUES ('{DEFAULT_USER_ID}', 'random@email.com')",
    )
    TEST_USER_ID = "a2f038f1-e35b-4509-bcc4-c08bd0e481a6"
    op.execute(
        f"INSERT INTO \"User\" (id, email) VALUES ('{TEST_USER_ID}', 'test@admyral.ai')",
    )

    op.execute(f"UPDATE secrets SET user_id = '{DEFAULT_USER_ID}'")
    op.create_foreign_key(
        None, "secrets", "User", ["user_id"], ["id"], ondelete="CASCADE"
    )
    op.drop_column("workflow_runs", "user_id")

    op.execute(f"UPDATE workflows SET user_id = '{DEFAULT_USER_ID}'")
    op.create_foreign_key(
        None, "workflows", "User", ["user_id"], ["id"], ondelete="CASCADE"
    )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_constraint(None, "workflows", type_="foreignkey")
    op.add_column(
        "workflow_runs",
        sa.Column("user_id", sa.TEXT(), autoincrement=False, nullable=False),
    )
    op.drop_constraint(None, "secrets", type_="foreignkey")
    op.drop_index(op.f("ix_api_keys_user_id"), table_name="api_keys")
    op.drop_index(op.f("ix_api_keys_key"), table_name="api_keys")
    op.drop_table("api_keys")
    op.drop_table("Session")
    op.drop_table("Authenticator")
    op.drop_table("Account")
    op.drop_table("VerificationToken")
    op.drop_table("User")
    # ### end Alembic commands ###
