"""migrations-0.0.1

Revision ID: 79ec864b571e
Revises: 
Create Date: 2024-02-25 12:20:11.674442

"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "79ec864b571e"
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(
        "datasource",
        sa.Column("id", sa.BigInteger(), nullable=False),
        sa.Column("uuid", sa.Unicode(length=255), nullable=False),
        sa.Column(
            "type",
            sa.Enum(
                "MINIO",
                "POSTGRES",
                "WINDOWS_SHARE",
                "SQL_SERVER",
                "GOOGLE_DRIVE",
                "SHAREPOINT",
                "MYSQL",
                name="datasource_type",
            ),
            nullable=False,
        ),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column(
            "status",
            sa.Enum("ONLINE", "OFFLINE", name="datasource_status"),
            nullable=False,
        ),
        sa.Column(
            "connection", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
        sa.UniqueConstraint("uuid"),
    )
    op.create_table(
        "document",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("uuid", sa.Unicode(length=255), nullable=False),
        sa.Column("base_uri", sa.Unicode(length=255), nullable=False),
        sa.Column("filename", sa.Unicode(length=255), nullable=False),
        sa.Column(
            "rag_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=False
        ),
        sa.Column(
            "store_metadata", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("uuid", "base_uri", name="uq_document_uuid_base_url"),
    )
    op.create_table(
        "model",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column(
            "module",
            sa.Enum("anomaly", "insights", "data", "qanda", name="model_module"),
            nullable=False,
        ),
        sa.Column("datasource", sa.Unicode(length=255), nullable=False),
        sa.Column("dataobject", sa.Unicode(length=255), nullable=False),
        sa.Column("target", sa.Unicode(length=255), nullable=True),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("attributes", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("module", "name", name="uq_model_module_name"),
    )
    op.create_index("idx_model_name", "model", ["module", "name"], unique=False)
    op.create_table(
        "role",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("uuid", sa.Unicode(length=255), nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column("create_date", sa.DateTime(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name", name="uq_role_name"),
        sa.UniqueConstraint("uuid"),
    )
    op.create_table(
        "setting",
        sa.Column("id", sa.Unicode(length=255), nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column("module", sa.Unicode(length=255), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("attributes", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "users",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("uuid", sa.Unicode(length=255), nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=False),
        sa.Column("root", sa.Boolean(), nullable=False),
        sa.Column("email", sa.Unicode(length=255), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("password", sa.LargeBinary(), nullable=False),
        sa.Column("create_date", sa.DateTime(), nullable=False),
        sa.Column("attributes", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("email", name="uq_user_email"),
        sa.UniqueConstraint("uuid"),
    )
    op.create_table(
        "model_rule",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("name", sa.Unicode(length=255), nullable=True),
        sa.Column("model", sa.BigInteger(), nullable=False),
        sa.Column("description", sa.Unicode(length=255), nullable=True),
        sa.Column("created_by", sa.Unicode(length=255), nullable=True),
        sa.Column("create_date", sa.DateTime(), nullable=False),
        sa.Column("value", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["model"], ["model.id"], name="fk_model_rule_model", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("name"),
    )
    op.create_table(
        "prediction",
        sa.Column("id", sa.BigInteger(), autoincrement=True, nullable=False),
        sa.Column("input", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("model", sa.BigInteger(), nullable=True),
        sa.Column("create_date", sa.DateTime(), nullable=False),
        sa.Column("data", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
        sa.Column("uid", sa.Unicode(length=4096), nullable=True),
        sa.Column("module", sa.Unicode(length=4096), nullable=True),
        sa.ForeignKeyConstraint(
            ["model"], ["model.id"], name="fk_rule_model", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("uid"),
    )
    op.create_table(
        "role_action",
        sa.Column("id", sa.BigInteger(), nullable=False),
        sa.Column("role", sa.BigInteger(), nullable=False),
        sa.Column(
            "action",
            sa.Enum("READ", "DELETE", "CREATE", "UPDATE", name="role_action_action"),
            nullable=False,
        ),
        sa.Column(
            "resource_type",
            sa.Enum(
                "MODULES",
                "USERS",
                "DATASOURCES",
                "ROLES",
                "PREDICTIONS",
                "SETTINGS",
                name="role_action_resource_type",
            ),
            nullable=False,
        ),
        sa.Column("resource", sa.Unicode(length=1024), nullable=True),
        sa.ForeignKeyConstraint(
            ["role"], ["role.id"], name="fk_role_action_role", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint(
            "role",
            "action",
            "resource_type",
            "resource",
            name="uq_role_action_resource_type_resource",
        ),
    )
    op.create_table(
        "user_role",
        sa.Column("id", sa.BigInteger(), nullable=False),
        sa.Column("uuid", sa.Unicode(length=255), nullable=False),
        sa.Column("role", sa.BigInteger(), nullable=False),
        sa.Column("user", sa.BigInteger(), nullable=False),
        sa.Column("create_date", sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(
            ["role"], ["role.id"], name="fk_user_role_role", ondelete="CASCADE"
        ),
        sa.ForeignKeyConstraint(
            ["user"], ["users.id"], name="fk_user_role_user", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user", "role", name="uq_user_role_user_role"),
        sa.UniqueConstraint("uuid"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("user_role")
    op.drop_table("role_action")
    op.drop_table("prediction")
    op.drop_table("model_rule")
    op.drop_table("users")
    op.drop_table("setting")
    op.drop_table("role")
    op.drop_index("idx_model_name", table_name="model")
    op.drop_table("model")
    op.drop_table("document")
    op.drop_table("datasource")
    # ### end Alembic commands ###
