"""init

Revision ID: 1ded42b82f3b
Revises: 
Create Date: 2021-10-10 18:55:41.093942

"""
import sqlalchemy as sa
import sqlmodel.sql.sqltypes
from alembic import op

from app.security import get_password_hash

# revision identifiers, used by Alembic.
revision = "1ded42b82f3b"
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    permissions_table = op.create_table(
        "permission",
        sa.Column("name", sa.String(), nullable=True),
        sa.Column("id", sa.Integer(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_index(op.f("ix_permission_id"), "permission", ["id"], unique=False)
    users_table = op.create_table(
        "user",
        sa.Column("email", sa.String(), nullable=True),
        sa.Column("id", sa.Integer(), nullable=True),
        sa.Column("password", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("is_active", sa.Boolean(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email"),
    )
    op.create_index(op.f("ix_user_id"), "user", ["id"], unique=False)
    op.create_index(op.f("ix_user_is_active"), "user", ["is_active"], unique=False)
    op.create_index(op.f("ix_user_password"), "user", ["password"], unique=False)
    user_permissions_table = op.create_table(
        "userpermissionlink",
        sa.Column("user_id", sa.Integer(), nullable=True),
        sa.Column("permission_id", sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(
            ["permission_id"],
            ["permission.id"],
        ),
        sa.ForeignKeyConstraint(
            ["user_id"],
            ["user.id"],
        ),
        sa.PrimaryKeyConstraint("user_id", "permission_id"),
    )
    op.create_index(
        op.f("ix_userpermissionlink_permission_id"),
        "userpermissionlink",
        ["permission_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_userpermissionlink_user_id"), "userpermissionlink", ["user_id"], unique=False
    )
    # ### end Alembic commands ###

    # TODO remove for release
    if permissions_table is not None:
        op.bulk_insert(permissions_table, [{"id": 1, "name": "admin_permission"}])

    if users_table is not None:
        op.bulk_insert(
            users_table,
            [{"email": "admin", "password": get_password_hash("123"), "is_active": True}],
        )
    if user_permissions_table is not None:
        op.bulk_insert(user_permissions_table, [{"user_id": 1, "permission_id": 1}])


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_userpermissionlink_user_id"), table_name="userpermissionlink")
    op.drop_index(op.f("ix_userpermissionlink_permission_id"), table_name="userpermissionlink")
    op.drop_table("userpermissionlink")
    op.drop_index(op.f("ix_user_password"), table_name="user")
    op.drop_index(op.f("ix_user_is_active"), table_name="user")
    op.drop_index(op.f("ix_user_id"), table_name="user")
    op.drop_table("user")
    op.drop_index(op.f("ix_permission_id"), table_name="permission")
    op.drop_table("permission")
    # ### end Alembic commands ###
