"""Add agent presets

Revision ID: d110a55643ba
Revises: 0aef095985d4
Create Date: 2025-11-04 17:06:03.137296

"""

from collections.abc import Sequence

import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

from alembic import op

# revision identifiers, used by Alembic.
revision: str = "d110a55643ba"
down_revision: str | None = "0aef095985d4"
branch_labels: str | Sequence[str] | None = None
depends_on: str | Sequence[str] | None = None


def upgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.create_table(
        "agent_preset",
        sa.Column(
            "created_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column(
            "updated_at",
            sa.TIMESTAMP(timezone=True),
            server_default=sa.text("now()"),
            nullable=False,
        ),
        sa.Column("surrogate_id", sa.Integer(), nullable=False),
        sa.Column("id", postgresql.UUID(as_uuid=True), nullable=False),
        sa.Column("owner_id", sa.UUID(), nullable=True),
        sa.Column("name", sa.String(), nullable=False),
        sa.Column("slug", sa.String(), nullable=False),
        sa.Column("description", sa.String(), nullable=True),
        sa.Column("instructions", sa.Text(), nullable=True),
        sa.Column("model_name", sa.String(), nullable=False),
        sa.Column("model_provider", sa.String(), nullable=False),
        sa.Column("base_url", sa.String(), nullable=True),
        sa.Column(
            "output_type", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("actions", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column("namespaces", postgresql.JSONB(astext_type=sa.Text()), nullable=True),
        sa.Column(
            "tool_approvals", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("mcp_server_url", sa.String(), nullable=True),
        sa.Column(
            "mcp_server_headers", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column(
            "model_settings", postgresql.JSONB(astext_type=sa.Text()), nullable=True
        ),
        sa.Column("retries", sa.Integer(), nullable=False),
        sa.ForeignKeyConstraint(
            ["owner_id"],
            ["workspace.id"],
            ondelete="CASCADE",
            name="agent_preset_owner_id_fkey",
        ),
        sa.PrimaryKeyConstraint("surrogate_id", name="agent_preset_pkey"),
        sa.UniqueConstraint("owner_id", "slug", name="uq_agent_preset_owner_slug"),
    )
    op.create_index(op.f("ix_agent_preset_id"), "agent_preset", ["id"], unique=True)
    op.create_index(
        op.f("ix_agent_preset_slug"), "agent_preset", ["slug"], unique=False
    )
    # ### end Alembic commands ###


def downgrade() -> None:
    # ### commands auto generated by Alembic - please adjust! ###
    op.drop_index(op.f("ix_agent_preset_slug"), table_name="agent_preset")
    op.drop_index(op.f("ix_agent_preset_id"), table_name="agent_preset")
    op.drop_table("agent_preset")
    # ### end Alembic commands ###
