"""add capability_catalog and capability_requests tables

Revision ID: i6d7e8f9a0b1
Revises: h5c6d7e8f9a0
Create Date: 2026-03-19
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

revision = "i6d7e8f9a0b1"
down_revision = "h5c6d7e8f9a0"
branch_labels = None
depends_on = None


def upgrade() -> None:
    op.create_table(
        "capability_catalog",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True,
                  server_default=sa.text("gen_random_uuid()")),
        sa.Column("domain_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("domains.id", ondelete="RESTRICT"),
                  nullable=False, index=True),
        sa.Column("capability_type", sa.String(50), nullable=False),
        sa.Column("title", sa.String(255), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("keywords", sa.ARRAY(sa.String), nullable=False, server_default="{}"),
        sa.Column("status", sa.String(20), nullable=False, server_default="active"),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.UniqueConstraint("domain_id", "capability_type", "title", name="uq_catalog_domain_type_title"),
    )

    op.create_table(
        "capability_requests",
        sa.Column("id", postgresql.UUID(as_uuid=True), primary_key=True,
                  server_default=sa.text("gen_random_uuid()")),
        sa.Column("title", sa.String(500), nullable=False),
        sa.Column("description", sa.Text, nullable=True),
        sa.Column("capability_type", sa.String(50), nullable=False),
        sa.Column("priority", sa.String(20), nullable=False, server_default="medium"),
        sa.Column("status", sa.String(20), nullable=False, server_default="requested"),
        sa.Column("requesting_domain_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("domains.id", ondelete="RESTRICT"),
                  nullable=False, index=True),
        sa.Column("requesting_workstream_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("workstreams.id", ondelete="SET NULL"),
                  nullable=True),
        sa.Column("requesting_agent", sa.String(100), nullable=False),
        sa.Column("fulfilling_domain_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("domains.id", ondelete="SET NULL"),
                  nullable=True, index=True),
        sa.Column("fulfilling_workstream_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("workstreams.id", ondelete="SET NULL"),
                  nullable=True),
        sa.Column("fulfilling_agent", sa.String(100), nullable=True),
        sa.Column("blocking_task_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("tasks.id", ondelete="SET NULL"),
                  nullable=True),
        sa.Column("catalog_entry_id", postgresql.UUID(as_uuid=True),
                  sa.ForeignKey("capability_catalog.id", ondelete="SET NULL"),
                  nullable=True),
        sa.Column("resolution_note", sa.Text, nullable=True),
        sa.Column("accepted_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("completed_at", sa.DateTime(timezone=True), nullable=True),
        sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
        sa.Column("updated_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
    )


def downgrade() -> None:
    op.drop_table("capability_requests")
    op.drop_table("capability_catalog")
