"""remove model,role entities

Revision ID: 3d6d802ca102
Revises: 0fc14df78ac3
Create Date: 2024-06-06 17:26:10.798750

"""

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 = "3d6d802ca102"
down_revision: Union[str, None] = "0fc14df78ac3"
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.drop_index("idx_model_name", table_name="model")
    op.drop_table("model_rule")
    op.drop_constraint("fk_rule_model", "prediction", type_="foreignkey")
    op.drop_table("model")
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###

    op.create_table(
        "model",
        sa.Column("id", sa.BIGINT(), autoincrement=True, nullable=False),
        sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=False),
        sa.Column(
            "module",
            postgresql.ENUM(
                "anomaly", "insights", "data", "qanda", name="model_module"
            ),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column(
            "datasource", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column(
            "dataobject", sa.VARCHAR(length=255), autoincrement=False, nullable=False
        ),
        sa.Column("target", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("enabled", sa.BOOLEAN(), autoincrement=False, nullable=False),
        sa.Column(
            "attributes",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=True,
        ),
        sa.PrimaryKeyConstraint("id", name="model_pkey"),
        sa.UniqueConstraint("module", "name", name="uq_model_module_name"),
    )

    op.create_table(
        "model_rule",
        sa.Column("id", sa.BIGINT(), autoincrement=True, nullable=False),
        sa.Column("name", sa.VARCHAR(length=255), autoincrement=False, nullable=True),
        sa.Column("model", sa.BIGINT(), autoincrement=False, nullable=False),
        sa.Column(
            "description", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column(
            "created_by", sa.VARCHAR(length=255), autoincrement=False, nullable=True
        ),
        sa.Column(
            "create_date", postgresql.TIMESTAMP(), autoincrement=False, nullable=False
        ),
        sa.Column(
            "value",
            postgresql.JSONB(astext_type=sa.Text()),
            autoincrement=False,
            nullable=False,
        ),
        sa.Column("enabled", sa.BOOLEAN(), autoincrement=False, nullable=False),
        sa.ForeignKeyConstraint(
            ["model"], ["model.id"], name="fk_model_rule_model", ondelete="CASCADE"
        ),
        sa.PrimaryKeyConstraint("id", name="model_rule_pkey"),
        sa.UniqueConstraint("name", name="model_rule_name_key"),
    )
    op.create_index("idx_model_name", "model", ["module", "name"], unique=False)
    op.create_foreign_key(
        "fk_rule_model", "prediction", "model", ["model"], ["id"], ondelete="CASCADE"
    )
    # ### end Alembic commands ###
