"""empty message

Revision ID: c51801ce4134
Revises: <PASSWORD>
Create Date: 2021-02-17 18:50:09.451228

"""

# revision identifiers, used by Alembic.
revision = "c51801ce4134"
down_revision = "da<PASSWORD>343771c"

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

from polylogyx.constants import DEFAULT_PLATFORMS


def upgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("config", sa.Column("name", sa.String(), nullable=True))
    op.add_column("config", sa.Column("is_default", sa.Boolean(), nullable=True))
    op.add_column(
        "config",
        sa.Column("conditions", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
    )
    op.add_column("config", sa.Column("description", sa.String(), nullable=True))
    op.add_column("node_config", sa.Column("config_id", sa.Integer(), nullable=True))
    op.add_column("node_config", sa.Column("node_id", sa.Integer(), nullable=True))
    op.alter_column(
        "node_config", "updated_at", existing_type=postgresql.TIMESTAMP(), nullable=True
    )
    op.drop_constraint("node_config_name_key", "node_config", type_="unique")
    op.create_foreign_key(
        "node_config_config_id_fkey",
        "node_config",
        "config",
        ["config_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.create_foreign_key(
        "node_config_node_id_fkey",
        "node_config",
        "node",
        ["node_id"],
        ["id"],
        ondelete="CASCADE",
    )
    op.drop_column("node_config", "apply_by_default")
    op.drop_column("node_config", "name")
    op.drop_column("node_config", "created_at")
    op.drop_column("node_config", "type")
    op.drop_column("node_config", "config")
    op.drop_column("node_config", "platform")

    # deleting the x86, deep configs
    op.execute("delete from default_query;")
    op.execute("delete from default_filters;")
    op.execute("delete from config where arch='x86' or type='2';")

    # setting the config names to 'Default' for those configs which are of shallow and default types
    op.execute(
        "update config set name='Default', description='Default Config of the platform' \
    where type='1' or type='0';"
    )
    op.execute("update config set is_default=true;")
    op.drop_column("config", "is_active")

    # assign configs to nodes
    bind = op.get_bind()
    session = orm.Session(bind=bind)

    config_id_mappings = {}
    configs = session.execute("select platform, id from config where is_default=true;")
    for config in configs:
        config_id_mappings[config[0]] = config[1]
    nodes = session.execute("select platform, id from node;")
    for node in nodes:
        platform = node[0]
        if platform not in DEFAULT_PLATFORMS:
            platform = "linux"
        session.execute("insert into node_config (node_id, config_id) values ({0}, {1});"
                        .format(node[1], config_id_mappings[platform]))
    session.commit()
    # ### end Alembic commands ###


def downgrade():
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column(
        "node_config",
        sa.Column("platform", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "node_config",
        sa.Column("config", sa.VARCHAR(), autoincrement=False, nullable=False),
    )
    op.add_column(
        "node_config",
        sa.Column("type", sa.VARCHAR(), autoincrement=False, nullable=True),
    )
    op.add_column(
        "node_config",
        sa.Column(
            "created_at", postgresql.TIMESTAMP(), autoincrement=False, nullable=True
        ),
    )
    op.add_column(
        "node_config",
        sa.Column("name", sa.VARCHAR(), autoincrement=False, nullable=False),
    )
    op.add_column(
        "node_config",
        sa.Column(
            "apply_by_default", sa.BOOLEAN(), autoincrement=False, nullable=False
        ),
    )
    op.drop_constraint("node_config_config_id_fkey", "node_config", type_="foreignkey")
    op.drop_constraint("node_config_node_id_fkey", "node_config", type_="foreignkey")
    op.create_unique_constraint("node_config_name_key", "node_config", ["name"])
    op.alter_column(
        "node_config",
        "updated_at",
        existing_type=postgresql.TIMESTAMP(),
        nullable=False,
    )
    op.drop_column("node_config", "node_id")
    op.drop_column("node_config", "config_id")
    op.drop_column("config", "name")
    op.drop_column("config", "is_default")
    op.drop_column("config", "description")
    op.drop_column("config", "conditions")
    op.add_column("config", sa.Column("is_active", sa.Boolean(), nullable=True))
    # ### end Alembic commands ###
