"""add_config_profiles

Revision ID: 2026_05_24_add_config_profiles
Revises: f3d2dc90ba3a
Create Date: 2026-05-24 14:00:00.000000
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision: str = "2026_05_24_add_config_profiles"
down_revision: Union[str, Sequence[str], None] = "f3d2dc90ba3a"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
    # Create config_profiles table
    op.create_table(
        "config_profiles",
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("user_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("users.id", ondelete="CASCADE"), nullable=False),
        sa.Column("name", sa.String(255), nullable=False),
        sa.Column("description", sa.Text(), nullable=True),
        sa.Column("project_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("projects.id", ondelete="CASCADE"), nullable=True),
        sa.Column("tool_type_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("tool_types.id", ondelete="CASCADE"), nullable=True),
        sa.Column("env_vars", postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default="{}"),
        sa.Column("runtime_hints", postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default="{}"),
        sa.Column("mounts", postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default="[]"),
        sa.Column("files", postgresql.JSONB(astext_type=sa.Text()), nullable=False, server_default="{}"),
        sa.Column("is_default", sa.Boolean(), nullable=False, server_default="false"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("user_id", "name", name="uq_config_profiles_user_name"),
    )

    # Create indexes for config_profiles
    op.create_index("idx_config_profiles_user", "config_profiles", ["user_id"])
    op.create_index("idx_config_profiles_project", "config_profiles", ["project_id"])
    op.create_index("idx_config_profiles_tool_type", "config_profiles", ["tool_type_id"])

    # Create config_profile_includes table
    op.create_table(
        "config_profile_includes",
        sa.Column("id", postgresql.UUID(as_uuid=True), server_default=sa.text("gen_random_uuid()"), nullable=False),
        sa.Column("profile_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("config_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("included_profile_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("config_profiles.id", ondelete="CASCADE"), nullable=False),
        sa.Column("order_index", sa.Integer(), nullable=False, server_default="0"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.text("NOW()"), nullable=False),
        sa.PrimaryKeyConstraint("id"),
        sa.UniqueConstraint("profile_id", "included_profile_id", name="uq_config_profile_includes"),
    )

    # Create indexes for config_profile_includes
    op.create_index("idx_config_profile_includes_profile", "config_profile_includes", ["profile_id"])
    op.create_index("idx_config_profile_includes_included", "config_profile_includes", ["included_profile_id"])

    # Add selected_config_profile_id to tool_instances
    op.add_column(
        "tool_instances",
        sa.Column("selected_config_profile_id", postgresql.UUID(as_uuid=True), sa.ForeignKey("config_profiles.id", ondelete="SET NULL"), nullable=True),
    )
    op.create_index("idx_tool_instances_config_profile", "tool_instances", ["selected_config_profile_id"])


def downgrade() -> None:
    # Remove selected_config_profile_id from tool_instances
    op.drop_index("idx_tool_instances_config_profile", table_name="tool_instances")
    op.drop_column("tool_instances", "selected_config_profile_id")

    # Drop config_profile_includes table
    op.drop_index("idx_config_profile_includes_included", table_name="config_profile_includes")
    op.drop_index("idx_config_profile_includes_profile", table_name="config_profile_includes")
    op.drop_table("config_profile_includes")

    # Drop config_profiles table
    op.drop_index("idx_config_profiles_tool_type", table_name="config_profiles")
    op.drop_index("idx_config_profiles_project", table_name="config_profiles")
    op.drop_index("idx_config_profiles_user", table_name="config_profiles")
    op.drop_table("config_profiles")
