"""Add automation_profiles table.

This migration creates the ``automation_profiles`` table for persisting
custom automation profiles. Built-in profiles are resolved in-memory
and are **not** stored in this table.

Revision ID: a6_001_automation_profiles
Revises: c1_001_tool_registry
Create Date: 2026-02-17 14:00:00

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    """Create automation_profiles table."""
    op.create_table(
        "automation_profiles",
        sa.Column("name", sa.Text(), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column(
            "schema_version",
            sa.Text(),
            nullable=False,
            server_default="1.0",
        ),
        sa.Column(
            "auto_strategize",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_execute",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_apply",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_decisions_strategize",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_decisions_execute",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_validation_fix",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_strategy_revision",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_reversion_from_apply",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_child_plans",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_retry_transient",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "auto_checkpoint_restore",
            sa.Float(),
            nullable=False,
            server_default=sa.text("0.0"),
        ),
        sa.Column(
            "require_sandbox",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("1"),
        ),
        sa.Column(
            "require_checkpoints",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("1"),
        ),
        sa.Column(
            "allow_unsafe_tools",
            sa.Boolean(),
            nullable=False,
            server_default=sa.text("0"),
        ),
        sa.Column("created_at", sa.Text(), nullable=False),
        sa.Column("updated_at", sa.Text(), nullable=False),
        sa.PrimaryKeyConstraint("name"),
    )
    op.create_index(
        "ix_automation_profiles_name",
        "automation_profiles",
        ["name"],
        unique=True,
    )


def downgrade() -> None:
    """Drop automation_profiles table."""
    op.drop_index(
        "ix_automation_profiles_name",
        table_name="automation_profiles",
    )
    op.drop_table("automation_profiles")
