"""Seed assignment-tracking: staff-managed assignment of any platform record to a
staff member, with an enforced status graph. Phase 11B1.

Composition is stamped at insert time via stamp_composition(), the same
function 134/135 use to restamp existing rows -- because install_pack() reads
feature_packs.composition directly with no runtime stamping call site
(confirmed: zero production call sites for stamp_composition outside those two
migrations). Seeding an unstamped blob here would silently ship this pack
without schema.workflow, schema.domain_events, or field_policy flags ever
reaching Cloud. See docs/audits/NOTIFICATIONS_IN_APP_11C1.md's "correction"
section for how this was found and fixed across 136/137/138.

Revision ID: 136
Revises: 135
"""
from datetime import datetime, timezone
from uuid import uuid4

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

revision = "136"
down_revision = "135"
branch_labels = None
depends_on = None

feature_packs = sa.table(
    "feature_packs",
    sa.column("id", postgresql.UUID(as_uuid=True)),
    sa.column("slug", sa.String),
    sa.column("name", sa.String),
    sa.column("description", sa.Text),
    sa.column("version", sa.String),
    sa.column("tags", postgresql.JSONB),
    sa.column("status", sa.String),
    sa.column("composition", postgresql.JSONB),
    sa.column("input_schema", postgresql.JSONB),
    sa.column("access_policy", postgresql.JSONB),
)

_ASSIGNMENT_ENTITY = {
    "entity_key": "assignment",
    "name": "Assignment",
    "plural_name": "Assignments",
    "schema": {
        "fields": [
            {
                "key": "subject_entity_key", "name": "Subject Entity", "type": "text",
                "required": True, "indexed": True,
            },
            {
                "key": "subject_record_id", "name": "Subject Record", "type": "text",
                "required": True, "indexed": True,
            },
            {
                "key": "assigned_to", "name": "Assigned To", "type": "text",
                "required": True, "indexed": True,
            },
            {"key": "assigned_by", "name": "Assigned By", "type": "text"},
            {
                "key": "status",
                "name": "Status",
                "type": "select",
                "required": True,
                "indexed": True,
                "options": ["active", "completed", "cancelled"],
            },
        ],
    },
}


def upgrade() -> None:
    from src.domain.feature_packs.normalize import stamp_composition

    now = datetime.now(timezone.utc)
    raw_composition = {
        "function_slugs": [],
        "entities": [_ASSIGNMENT_ENTITY],
        "connector_actions": [],
        "requires": ["workflow.transition"],
    }
    op.bulk_insert(
        feature_packs,
        [
            {
                "id": uuid4(),
                "slug": "assignment-tracking",
                "name": "Assignment Tracking",
                "description": (
                    "Staff-managed assignment of any platform record (lead, case, "
                    "ticket, ...) to a staff member, referenced by opaque "
                    "(entity_key, record_id) -- not a per-vertical assigned_to field, "
                    "and not a second workflow engine: the assignment's own status "
                    "graph is enforced by the existing workflow.transition primitive."
                ),
                "version": "1.0.0",
                "tags": ["workflow", "assignment", "operations"],
                "status": "active",
                "composition": stamp_composition("assignment-tracking", raw_composition),
                "input_schema": {"type": "object", "properties": {}},
                "access_policy": {"minimum_plan": "free"},
                "created_at": now,
                "updated_at": now,
            },
        ],
    )


def downgrade() -> None:
    op.execute("DELETE FROM feature_packs WHERE slug = 'assignment-tracking'")
