"""Add user table

Revision ID: 3093c7336477
Revises:
Create Date: 2024-11-20 05:57:01.684118

"""

from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

# revision identifiers, used by Alembic.
revision: str = "3093c7336477"
down_revision: Union[str, None] = None
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(
        "permission",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("updated_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_permission_name"), "permission", ["name"], unique=False)
    op.create_index(
        op.f("ix_permission_created_at"), "permission", ["created_at"], unique=False
    )
    op.create_index(
        op.f("ix_permission_created_by"), "permission", ["created_by"], unique=False
    )
    op.create_index(
        op.f("ix_permission_updated_at"), "permission", ["updated_at"], unique=False
    )
    op.create_index(
        op.f("ix_permission_updated_by"), "permission", ["updated_by"], unique=False
    )

    op.create_table(
        "role",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("updated_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_role_name"), "role", ["name"], unique=False)
    op.create_index(op.f("ix_role_created_at"), "role", ["created_at"], unique=False)
    op.create_index(op.f("ix_role_created_by"), "role", ["created_by"], unique=False)
    op.create_index(op.f("ix_role_updated_at"), "role", ["updated_at"], unique=False)
    op.create_index(op.f("ix_role_updated_by"), "role", ["updated_by"], unique=False)

    op.create_table(
        "rolepermission",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("role_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("permission_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_rolepermission_permission_id"),
        "rolepermission",
        ["permission_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_rolepermission_role_id"), "rolepermission", ["role_id"], unique=False
    )

    op.create_table(
        "user",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("username", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("password", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=False),
        sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.Column("updated_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_user_username"), "user", ["username"], unique=False)
    op.create_index(op.f("ix_user_created_at"), "user", ["created_at"], unique=False)
    op.create_index(op.f("ix_user_created_by"), "user", ["created_by"], unique=False)
    op.create_index(op.f("ix_user_updated_at"), "user", ["updated_at"], unique=False)
    op.create_index(op.f("ix_user_updated_by"), "user", ["updated_by"], unique=False)

    op.create_table(
        "userrole",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("role_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("created_by", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_userrole_role_id"), "userrole", ["role_id"], unique=False)
    op.create_index(op.f("ix_userrole_user_id"), "userrole", ["user_id"], unique=False)

    op.create_table(
        "session",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("user_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("device", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("os", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("browser", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_session_user_id"), "session", ["user_id"], unique=False)
    # ### end Alembic commands ###


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

    op.drop_index(op.f("ix_userrole_user_id"), table_name="userrole")
    op.drop_index(op.f("ix_userrole_role_id"), table_name="userrole")
    op.drop_table("userrole")

    op.drop_index(op.f("ix_user_username"), table_name="user")
    op.drop_index(op.f("ix_user_updated_by"), table_name="user")
    op.drop_index(op.f("ix_user_updated_at"), table_name="user")
    op.drop_index(op.f("ix_user_created_by"), table_name="user")
    op.drop_index(op.f("ix_user_created_at"), table_name="user")
    op.drop_table("user")

    op.drop_index(op.f("ix_rolepermission_role_id"), table_name="rolepermission")
    op.drop_index(op.f("ix_rolepermission_permission_id"), table_name="rolepermission")
    op.drop_table("rolepermission")

    op.drop_index(op.f("ix_role_name"), table_name="role")
    op.drop_index(op.f("ix_role_updated_by"), table_name="role")
    op.drop_index(op.f("ix_role_updated_at"), table_name="role")
    op.drop_index(op.f("ix_role_created_by"), table_name="role")
    op.drop_index(op.f("ix_role_created_at"), table_name="role")
    op.drop_table("role")

    op.drop_index(op.f("ix_permission_updated_by"), table_name="permission")
    op.drop_index(op.f("ix_permission_updated_at"), table_name="permission")
    op.drop_index(op.f("ix_permission_created_by"), table_name="permission")
    op.drop_index(op.f("ix_permission_created_at"), table_name="permission")
    op.drop_index(op.f("ix_permission_name"), table_name="permission")
    op.drop_table("permission")
    # ### end Alembic commands ###
