"""Add skill registry tables (skills, skill_items).

This migration creates the two core skill registry tables needed for
the Skill Registry Persistence layer:

- ``skills``: Registered skill definitions with namespaced names as
  primary keys, description, version, metadata JSON, and timestamps.
- ``skill_items``: Individual skill items (tool refs, includes, inline
  tools, MCP sources, agent sources) linked to skills with stable
  ordering and JSON config.

Revision ID: c0_001_skill_registry
Revises: 71cd40eb661f
Create Date: 2026-02-19 12:00:00

"""

from collections.abc import Sequence

import sqlalchemy as sa
from alembic import op

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


def upgrade() -> None:
    # -- skills table --
    op.create_table(
        "skills",
        sa.Column("name", sa.String(255), primary_key=True),
        sa.Column("namespace", sa.String(100), nullable=False),
        sa.Column("short_name", sa.String(150), nullable=False),
        sa.Column("description", sa.Text(), nullable=False),
        sa.Column("version", sa.String(50), nullable=True),
        sa.Column("metadata_json", sa.Text(), nullable=True),
        sa.Column("created_at", sa.String(30), nullable=False),
        sa.Column("updated_at", sa.String(30), nullable=False),
    )
    op.create_index("ix_skills_namespace", "skills", ["namespace"])

    # -- skill_items table --
    op.create_table(
        "skill_items",
        sa.Column("id", sa.Integer(), primary_key=True, autoincrement=True),
        sa.Column(
            "skill_name",
            sa.String(255),
            sa.ForeignKey("skills.name", ondelete="CASCADE"),
            nullable=False,
        ),
        sa.Column("item_type", sa.String(30), nullable=False),
        sa.Column("item_name", sa.String(500), nullable=False),
        sa.Column("item_config", sa.Text(), nullable=True),
        sa.Column("item_order", sa.Integer(), nullable=False),
        sa.Column("created_at", sa.String(30), nullable=False),
    )
    op.create_index("ix_skill_items_skill_name", "skill_items", ["skill_name"])
    op.create_index("ix_skill_items_item_type", "skill_items", ["item_type"])


def downgrade() -> None:
    op.drop_index("ix_skill_items_item_type", table_name="skill_items")
    op.drop_index("ix_skill_items_skill_name", table_name="skill_items")
    op.drop_table("skill_items")
    op.drop_index("ix_skills_namespace", table_name="skills")
    op.drop_table("skills")
