"""standardize rule output

Revision ID: d298a8a6e360
Revises: ba03ca11c6ab
Create Date: 2023-09-08 16:40:43.851789

"""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = "d298a8a6e360"
down_revision = "ba03ca11c6ab"
branch_labels = None
depends_on = None


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

    op.add_column("inference_prompts", sa.Column("result", sa.String()))
    op.execute(
        "UPDATE inference_prompts SET result = (case when passed = true then 'Pass' else 'Fail' end);",
    )
    op.alter_column("inference_prompts", "result", nullable=False)
    op.drop_column("inference_prompts", "passed")

    op.add_column("inference_responses", sa.Column("result", sa.String()))
    op.execute(
        "UPDATE inference_responses SET result = (case when passed = true then 'Pass' else 'Fail' end);",
    )
    op.alter_column("inference_responses", "result", nullable=False)
    op.drop_column("inference_responses", "passed")

    op.add_column("inferences", sa.Column("result", sa.String()))
    op.execute(
        "UPDATE inferences SET result = (case when passed = true then 'Pass' else 'Fail' end);",
    )
    op.alter_column("inferences", "result", nullable=False)
    op.drop_column("inferences", "passed")

    op.add_column("prompt_rule_results", sa.Column("rule_result", sa.String()))
    op.execute(
        "UPDATE prompt_rule_results SET rule_result = (case when rule_passed = true then 'Pass' else 'Fail' end);",
    )
    op.alter_column("prompt_rule_results", "rule_result", nullable=False)
    op.drop_column("prompt_rule_results", "rule_passed")

    op.add_column(
        "prompt_rule_results",
        sa.Column("rule_details", sa.String(), nullable=True),
    )

    op.add_column("response_rule_results", sa.Column("rule_result", sa.String()))
    op.execute(
        "UPDATE response_rule_results SET rule_result = (case when rule_passed = true then 'Pass' else 'Fail' end);",
    )
    op.alter_column("response_rule_results", "rule_result", nullable=False)
    op.drop_column("response_rule_results", "rule_passed")

    op.add_column(
        "response_rule_results",
        sa.Column("rule_details", sa.String(), nullable=True),
    )

    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.add_column("response_rule_results", sa.Column("rule_passed", sa.BOOLEAN()))
    op.execute(
        "UPDATE response_rule_results SET rule_passed = (case when rule_result = 'Pass' then true else false end);",
    )
    op.alter_column("response_rule_results", "rule_passed", nullable=False)
    op.drop_column("response_rule_results", "rule_result")

    op.drop_column("response_rule_results", "rule_details")

    op.add_column(
        "prompt_rule_results",
        sa.Column("rule_passed", sa.BOOLEAN()),
    )
    op.execute(
        "UPDATE prompt_rule_results SET rule_passed = (case when rule_result = 'Pass' then true else false end);",
    )
    op.alter_column("prompt_rule_results", "rule_passed", nullable=False)
    op.drop_column("prompt_rule_results", "rule_result")

    op.drop_column("prompt_rule_results", "rule_details")

    op.add_column(
        "inferences",
        sa.Column("passed", sa.BOOLEAN()),
    )
    op.execute(
        "UPDATE inferences SET passed = (case when result = 'Pass' then true else false end);",
    )
    op.alter_column("inferences", "passed", nullable=False)
    op.drop_column("inferences", "result")

    op.add_column(
        "inference_responses",
        sa.Column("passed", sa.BOOLEAN()),
    )
    op.execute(
        "UPDATE inference_responses SET passed = (case when result = 'Pass' then true else false end);",
    )
    op.alter_column("inference_responses", "passed", nullable=False)
    op.drop_column("inference_responses", "result")

    op.add_column(
        "inference_prompts",
        sa.Column("passed", sa.BOOLEAN()),
    )
    op.execute(
        "UPDATE inference_prompts SET passed = (case when result = 'Pass' then true else false end);",
    )
    op.alter_column("inference_prompts", "passed", nullable=False)
    op.drop_column("inference_prompts", "result")
    # ### end Alembic commands ###
