"""Start db with v2 models

Revision ID: e04161e97672
Revises:
Create Date: 2023-06-23 11:35:16.117520

"""

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "inferences",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("passed", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_inferences_id"), "inferences", ["id"], unique=False)
    op.create_table(
        "rules",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("type", sa.String(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("prompt_enabled", sa.Boolean(), nullable=False),
        sa.Column("response_enabled", sa.Boolean(), nullable=False),
        sa.Column("scoring_method", sa.String(), nullable=False),
        sa.Column("archived", sa.Boolean(), nullable=False),
        sa.Column("scope", sa.String(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_rules_id"), "rules", ["id"], unique=False)
    op.create_table(
        "tasks",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_tasks_id"), "tasks", ["id"], unique=False)
    op.create_table(
        "inference_prompts",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("inference_id", sa.String(), nullable=False),
        sa.Column("passed", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("data_path", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["inference_id"],
            ["inferences.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_inference_prompts_id"),
        "inference_prompts",
        ["id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_inference_prompts_inference_id"),
        "inference_prompts",
        ["inference_id"],
        unique=True,
    )
    op.create_table(
        "inference_responses",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("inference_id", sa.String(), nullable=False),
        sa.Column("passed", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("data_path", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["inference_id"],
            ["inferences.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_inference_responses_id"),
        "inference_responses",
        ["id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_inference_responses_inference_id"),
        "inference_responses",
        ["inference_id"],
        unique=True,
    )
    op.create_table(
        "rule_data",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("rule_id", sa.String(), nullable=False),
        sa.Column("data_type", sa.String(), nullable=False),
        sa.Column("data", sa.String(), nullable=False),
        sa.ForeignKeyConstraint(
            ["rule_id"],
            ["rules.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_rule_data_rule_id"),
        "rule_data",
        ["rule_id"],
        unique=False,
    )
    op.create_table(
        "tasks_to_rules",
        sa.Column("task_id", sa.String(), nullable=False),
        sa.Column("rule_id", sa.String(), nullable=False),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(
            ["rule_id"],
            ["rules.id"],
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("task_id", "rule_id"),
    )
    op.create_index(
        op.f("ix_tasks_to_rules_rule_id"),
        "tasks_to_rules",
        ["rule_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_tasks_to_rules_task_id"),
        "tasks_to_rules",
        ["task_id"],
        unique=False,
    )
    op.create_table(
        "prompt_rule_results",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("inference_prompt_id", sa.String(), nullable=False),
        sa.Column("rule_id", sa.String(), nullable=False),
        sa.Column("rule_passed", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.ForeignKeyConstraint(
            ["inference_prompt_id"],
            ["inference_prompts.id"],
        ),
        sa.ForeignKeyConstraint(
            ["rule_id"],
            ["rules.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("inference_prompt_id", "rule_id"),
    )
    op.create_index(
        op.f("ix_prompt_rule_results_id"),
        "prompt_rule_results",
        ["id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_prompt_rule_results_inference_prompt_id"),
        "prompt_rule_results",
        ["inference_prompt_id"],
        unique=False,
    )
    op.create_table(
        "response_rule_results",
        sa.Column("id", sa.String(), nullable=False),
        sa.Column("inference_response_id", sa.String(), nullable=False),
        sa.Column("rule_id", sa.String(), nullable=False),
        sa.Column("rule_passed", sa.Boolean(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.ForeignKeyConstraint(
            ["inference_response_id"],
            ["inference_responses.id"],
        ),
        sa.ForeignKeyConstraint(
            ["rule_id"],
            ["rules.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("inference_response_id", "rule_id"),
    )
    op.create_index(
        op.f("ix_response_rule_results_id"),
        "response_rule_results",
        ["id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_response_rule_results_inference_response_id"),
        "response_rule_results",
        ["inference_response_id"],
        unique=False,
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(
        op.f("ix_response_rule_results_inference_response_id"),
        table_name="response_rule_results",
    )
    op.drop_index(
        op.f("ix_response_rule_results_id"),
        table_name="response_rule_results",
    )
    op.drop_table("response_rule_results")
    op.drop_index(
        op.f("ix_prompt_rule_results_inference_prompt_id"),
        table_name="prompt_rule_results",
    )
    op.drop_index(op.f("ix_prompt_rule_results_id"), table_name="prompt_rule_results")
    op.drop_table("prompt_rule_results")
    op.drop_index(op.f("ix_tasks_to_rules_task_id"), table_name="tasks_to_rules")
    op.drop_index(op.f("ix_tasks_to_rules_rule_id"), table_name="tasks_to_rules")
    op.drop_table("tasks_to_rules")
    op.drop_index(op.f("ix_rule_data_rule_id"), table_name="rule_data")
    op.drop_table("rule_data")
    op.drop_index(
        op.f("ix_inference_responses_inference_id"),
        table_name="inference_responses",
    )
    op.drop_index(op.f("ix_inference_responses_id"), table_name="inference_responses")
    op.drop_table("inference_responses")
    op.drop_index(
        op.f("ix_inference_prompts_inference_id"),
        table_name="inference_prompts",
    )
    op.drop_index(op.f("ix_inference_prompts_id"), table_name="inference_prompts")
    op.drop_table("inference_prompts")
    op.drop_index(op.f("ix_tasks_id"), table_name="tasks")
    op.drop_table("tasks")
    op.drop_index(op.f("ix_rules_id"), table_name="rules")
    op.drop_table("rules")
    op.drop_index(op.f("ix_inferences_id"), table_name="inferences")
    op.drop_table("inferences")
    # ### end Alembic commands ###
