"""Add category and display fields to agent_types table

Revision ID: 0007
Revises: 0006
Create Date: 2026-01-06

This migration adds:
- category: String(50) for grouping agents by role type
- icon: String(50) for Lucide icon identifier
- color: String(7) for hex color code
- sort_order: Integer for display ordering within categories
- typical_tasks: JSONB list of tasks this agent excels at
- collaboration_hints: JSONB list of agent slugs that work well together
"""

from collections.abc import Sequence

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

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


def upgrade() -> None:
    """Add category and display fields to agent_types table."""
    # Add new columns
    op.add_column(
        "agent_types",
        sa.Column("category", sa.String(length=50), nullable=True),
    )
    op.add_column(
        "agent_types",
        sa.Column("icon", sa.String(length=50), nullable=True, server_default="bot"),
    )
    op.add_column(
        "agent_types",
        sa.Column(
            "color", sa.String(length=7), nullable=True, server_default="#3B82F6"
        ),
    )
    op.add_column(
        "agent_types",
        sa.Column("sort_order", sa.Integer(), nullable=False, server_default="0"),
    )
    op.add_column(
        "agent_types",
        sa.Column(
            "typical_tasks",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=False,
            server_default="[]",
        ),
    )
    op.add_column(
        "agent_types",
        sa.Column(
            "collaboration_hints",
            postgresql.JSONB(astext_type=sa.Text()),
            nullable=False,
            server_default="[]",
        ),
    )

    # Add indexes for category and sort_order
    op.create_index("ix_agent_types_category", "agent_types", ["category"])
    op.create_index("ix_agent_types_sort_order", "agent_types", ["sort_order"])
    op.create_index(
        "ix_agent_types_category_sort", "agent_types", ["category", "sort_order"]
    )


def downgrade() -> None:
    """Remove category and display fields from agent_types table."""
    # Drop indexes
    op.drop_index("ix_agent_types_category_sort", table_name="agent_types")
    op.drop_index("ix_agent_types_sort_order", table_name="agent_types")
    op.drop_index("ix_agent_types_category", table_name="agent_types")

    # Drop columns
    op.drop_column("agent_types", "collaboration_hints")
    op.drop_column("agent_types", "typical_tasks")
    op.drop_column("agent_types", "sort_order")
    op.drop_column("agent_types", "color")
    op.drop_column("agent_types", "icon")
    op.drop_column("agent_types", "category")
