"""create rag settings

Revision ID: 9d3d34fcf6b4
Revises: 2264832c91c0
Create Date: 2025-11-04 15:13:38.589250

"""

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision = "9d3d34fcf6b4"
down_revision = "2264832c91c0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "rag_search_setting_configurations",
        sa.Column("id", sa.UUID(), nullable=False),
        sa.Column("task_id", sa.String(), nullable=False),
        sa.Column("rag_provider_id", sa.UUID(), nullable=True),
        sa.Column(
            "all_possible_tags",
            postgresql.JSON(astext_type=sa.Text()),
            nullable=True,
        ),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("latest_version_number", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.ForeignKeyConstraint(
            ["rag_provider_id"],
            ["rag_provider_configurations.id"],
            ondelete="SET NULL",
        ),
        sa.ForeignKeyConstraint(
            ["task_id"],
            ["tasks.id"],
        ),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(
        op.f("ix_rag_search_setting_configurations_rag_provider_id"),
        "rag_search_setting_configurations",
        ["rag_provider_id"],
        unique=False,
    )
    op.create_index(
        op.f("ix_rag_search_setting_configurations_task_id"),
        "rag_search_setting_configurations",
        ["task_id"],
        unique=False,
    )
    op.create_table(
        "rag_search_setting_configuration_versions",
        sa.Column("setting_configuration_id", sa.UUID(), nullable=False),
        sa.Column("version_number", sa.Integer(), nullable=False),
        sa.Column("settings", postgresql.JSON(astext_type=sa.Text()), nullable=False),
        sa.Column("tags", postgresql.JSON(astext_type=sa.Text()), nullable=True),
        sa.Column("created_at", sa.TIMESTAMP(), nullable=False),
        sa.Column("updated_at", sa.TIMESTAMP(), nullable=False),
        sa.ForeignKeyConstraint(
            ["setting_configuration_id"],
            ["rag_search_setting_configurations.id"],
        ),
        sa.PrimaryKeyConstraint("setting_configuration_id", "version_number"),
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_table("rag_search_setting_configuration_versions")
    op.drop_index(
        op.f("ix_rag_search_setting_configurations_task_id"),
        table_name="rag_search_setting_configurations",
    )
    op.drop_index(
        op.f("ix_rag_search_setting_configurations_rag_provider_id"),
        table_name="rag_search_setting_configurations",
    )
    op.drop_table("rag_search_setting_configurations")
    # ### end Alembic commands ###
