"""add model configuration center

Revision ID: 8e91c70a5d3b
Revises: df320ecde84f
Create Date: 2026-07-17
"""

from typing import Sequence, Union

import sqlalchemy as sa
import sqlmodel
from alembic import op

revision: str = "8e91c70a5d3b"
down_revision: Union[str, Sequence[str], None] = "df320ecde84f"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    op.create_table(
        "model_configs",
        sa.Column("id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("name", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("provider", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("capability", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("endpoint_url", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("model_name", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("api_key_encrypted", sqlmodel.sql.sqltypes.AutoString(), nullable=True),
        sa.Column("enabled", sa.Boolean(), nullable=False),
        sa.Column("is_default", sa.Boolean(), nullable=False),
        sa.Column("description", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("created_at", sa.DateTime(), nullable=True),
        sa.Column("updated_at", sa.DateTime(), nullable=True),
        sa.PrimaryKeyConstraint("id"),
    )
    op.create_index(op.f("ix_model_configs_capability"), "model_configs", ["capability"], unique=False)
    op.create_index(op.f("ix_model_configs_name"), "model_configs", ["name"], unique=True)
    op.create_table(
        "scenario_model_bindings",
        sa.Column("scenario_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("purpose", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.Column("model_config_id", sqlmodel.sql.sqltypes.AutoString(), nullable=False),
        sa.ForeignKeyConstraint(["model_config_id"], ["model_configs.id"]),
        sa.ForeignKeyConstraint(["scenario_id"], ["scenarios.id"]),
        sa.PrimaryKeyConstraint("scenario_id", "purpose"),
    )
    op.create_index(
        op.f("ix_scenario_model_bindings_model_config_id"),
        "scenario_model_bindings",
        ["model_config_id"],
        unique=False,
    )


def downgrade() -> None:
    op.drop_index(
        op.f("ix_scenario_model_bindings_model_config_id"),
        table_name="scenario_model_bindings",
    )
    op.drop_table("scenario_model_bindings")
    op.drop_index(op.f("ix_model_configs_name"), table_name="model_configs")
    op.drop_index(op.f("ix_model_configs_capability"), table_name="model_configs")
    op.drop_table("model_configs")
