# -*- coding: utf-8 -*-
"""roc_idb_0001_initial

Revision ID: roc_idb_0001_initial
Revises:
Create Date: 2019-12-26 18:29:52.527835

"""

import sqlalchemy as sa
from poppy.pop.alembic.helpers import (
    create_table,
    create_schema,
    execute,
    user,
    drop_table,
)
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = "roc_idb_0001_initial"
down_revision = None
branch_labels = ("idb",)
depends_on = None


def upgrade():
    create_schema("idb")  # added by hand

    ## delete the types if they exist
    execute("DROP TYPE IF EXISTS idb.idb_source_type")

    # ### commands auto generated by Alembic - please adjust! ###
    create_table(
        "idb_release",
        sa.Column("id_idb_release", sa.INTEGER(), nullable=False),
        sa.Column("idb_version", sa.String(length=128), nullable=False),
        sa.Column(
            "idb_source",
            postgresql.ENUM(
                "PALISADE", "MIB", "SRDB", name="idb_source_type", schema="idb"
            ),
            nullable=False,
        ),
        sa.Column("mib_version", sa.String(), nullable=True),
        sa.Column("current", sa.BOOLEAN(), nullable=False),
        sa.Column("validity_min", postgresql.TIMESTAMP(), nullable=True),
        sa.Column("validity_max", postgresql.TIMESTAMP(), nullable=True),
        sa.PrimaryKeyConstraint("id_idb_release"),
        sa.UniqueConstraint("idb_version", "idb_source"),
        schema="idb",
    )
    create_table(
        "palisade_metadata",
        sa.Column("id_palisade_metadata", sa.INTEGER(), nullable=False),
        sa.Column("palisade_version", sa.String(length=32), nullable=False),
        sa.Column("srdb_id", sa.String(length=10), nullable=True),
        sa.Column("palisade_id", sa.String(length=128), nullable=True),
        sa.Column("packet_category", sa.String(length=128), nullable=True),
        sa.PrimaryKeyConstraint("id_palisade_metadata"),
        sa.UniqueConstraint("palisade_version", "srdb_id"),
        schema="idb",
    )
    create_table(
        "item_info",
        sa.Column("id_item_info", sa.INTEGER(), nullable=False),
        sa.Column("idb_release_id", sa.INTEGER(), nullable=False),
        sa.Column("srdb_id", sa.String(length=10), nullable=True),
        sa.Column("palisade_id", sa.String(length=128), nullable=True),
        sa.Column("item_descr", sa.String(), nullable=True),
        sa.ForeignKeyConstraint(
            ["idb_release_id"],
            ["idb.idb_release.id_idb_release"],
        ),
        sa.PrimaryKeyConstraint("id_item_info"),
        sa.UniqueConstraint("srdb_id", "idb_release_id"),
        schema="idb",
    )
    create_table(
        "packet_metadata",
        sa.Column("id_packet_metadata", sa.INTEGER(), nullable=False),
        sa.Column("idb_release_id", sa.INTEGER(), nullable=False),
        sa.Column("packet_category", sa.String(length=256), nullable=True),
        sa.ForeignKeyConstraint(
            ["idb_release_id"],
            ["idb.idb_release.id_idb_release"],
        ),
        sa.PrimaryKeyConstraint("id_packet_metadata"),
        schema="idb",
    )
    create_table(
        "packet_header",
        sa.Column("id_packet_header", sa.INTEGER(), nullable=False),
        sa.Column("item_info_id", sa.INTEGER(), nullable=False),
        sa.Column("packet_metadata_id", sa.INTEGER(), nullable=True),
        sa.Column("cat_eng", sa.String(length=128), nullable=False),
        sa.Column("cat", sa.BIGINT(), nullable=False),
        sa.Column("packet_type", sa.String(length=16), nullable=False),
        sa.Column("byte_size", sa.INTEGER(), nullable=False),
        sa.Column("pid_eng", sa.String(length=128), nullable=True),
        sa.Column("pid", sa.BIGINT(), nullable=True),
        sa.Column("spid", sa.BIGINT(), nullable=True),
        sa.Column("sid_eng", sa.String(length=128), nullable=True),
        sa.Column("sid", sa.BIGINT(), nullable=True),
        sa.Column("service_type_eng", sa.String(length=128), nullable=True),
        sa.Column("service_type", sa.BIGINT(), nullable=False),
        sa.Column("service_subtype_eng", sa.String(length=128), nullable=True),
        sa.Column("service_subtype", sa.BIGINT(), nullable=False),
        sa.ForeignKeyConstraint(
            ["item_info_id"],
            ["idb.item_info.id_item_info"],
        ),
        sa.ForeignKeyConstraint(
            ["packet_metadata_id"],
            ["idb.packet_metadata.id_packet_metadata"],
        ),
        sa.PrimaryKeyConstraint("id_packet_header"),
        schema="idb",
    )
    create_table(
        "param_info",
        sa.Column("id_param_info", sa.INTEGER(), nullable=False),
        sa.Column("item_info_id", sa.INTEGER(), nullable=False),
        sa.Column("par_bit_length", sa.SMALLINT(), nullable=False),
        sa.Column("par_type", sa.String(length=16), nullable=False),
        sa.Column("par_max", postgresql.DOUBLE_PRECISION(), nullable=True),
        sa.Column("par_min", postgresql.DOUBLE_PRECISION(), nullable=True),
        sa.Column("par_def", sa.String(length=64), nullable=True),
        sa.Column("cal_num", sa.String(length=16), nullable=True),
        sa.Column("cal_val", sa.BIGINT(), nullable=True),
        sa.Column("par_unit", sa.String(length=8), nullable=True),
        sa.Column("par_is_editable", sa.BOOLEAN(), nullable=True),
        sa.Column("transfer_function_id", sa.INTEGER(), nullable=True),
        sa.ForeignKeyConstraint(
            ["item_info_id"],
            ["idb.item_info.id_item_info"],
        ),
        sa.ForeignKeyConstraint(
            ["transfer_function_id"],
            ["idb.item_info.id_item_info"],
        ),
        sa.PrimaryKeyConstraint("id_param_info"),
        schema="idb",
    )
    create_table(
        "transfer_function",
        sa.Column("id_transfer_function", sa.INTEGER(), nullable=False),
        sa.Column("item_info_id", sa.INTEGER(), nullable=False),
        sa.Column("raw", sa.BIGINT(), nullable=False),
        sa.Column("eng", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["item_info_id"],
            ["idb.item_info.id_item_info"],
        ),
        sa.PrimaryKeyConstraint("id_transfer_function"),
        schema="idb",
    )
    create_table(
        "packet_map_info",
        sa.Column("id_packet_map_info", sa.INTEGER(), nullable=False),
        sa.Column("packet_header_id", sa.INTEGER(), nullable=False),
        sa.Column("param_info_id", sa.INTEGER(), nullable=False),
        sa.Column("block_size", sa.INTEGER(), nullable=False),
        sa.Column("group_size", sa.INTEGER(), nullable=False),
        sa.Column("loop_value", sa.INTEGER(), nullable=False),
        sa.Column("byte_position", sa.INTEGER(), nullable=False),
        sa.Column("bit_position", sa.INTEGER(), nullable=False),
        sa.ForeignKeyConstraint(
            ["packet_header_id"],
            ["idb.packet_header.id_packet_header"],
        ),
        sa.ForeignKeyConstraint(
            ["param_info_id"],
            ["idb.param_info.id_param_info"],
        ),
        sa.PrimaryKeyConstraint("id_packet_map_info"),
        schema="idb",
    )
    # ### end Alembic commands ###

    # added by hand
    # fix table permissions
    grant_user_seq = (
        "GRANT USAGE, SELECT ON ALL SEQUENCES IN " "SCHEMA idb TO {0}".format(user)
    )
    execute(grant_user_seq)


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    drop_table("packet_map_info", schema="idb")
    drop_table("transfer_function", schema="idb")
    drop_table("param_info", schema="idb")
    drop_table("packet_header", schema="idb")
    drop_table("packet_metadata", schema="idb")
    drop_table("item_info", schema="idb")
    drop_table("palisade_metadata", schema="idb")
    drop_table("idb_release", schema="idb")

    # added by hand
    execute("DROP TYPE IF EXISTS idb.idb_source_type")
    execute("DROP SCHEMA idb")

    # ### end Alembic commands ###
