"""Seed the connections-networking feature pack: business_connection (owner_managed, not singleton).

LinkedIn-style connect/accept between two business_profile records. Cloud's ownership
primitives (rls_enabled, owner_managed) each assume a single owner, but a connection
involves two businesses and the recipient needs to act (accept/decline) on something
they don't own. Modeled as two independent owner_managed rows instead of one shared
mutable row -- each side creates their own row referencing the other, so no new Cloud
capability is needed beyond owner_managed itself (with owner_singleton left off, since
one business has many connections, not one).

Revision ID: 114
Revises: 113
"""
from datetime import datetime, timezone
from uuid import uuid4

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

revision = "114"
down_revision = "113"
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),
)

_BUSINESS_CONNECTION_ENTITY = {
    "entity_key": "business_connection",
    "name": "Business Connection",
    "plural_name": "Business Connections",
    # owner_managed, deliberately NOT owner_singleton -- a business has many connections.
    "owner_managed": True,
    "schema": {
        "fields": [
            {
                "key": "my_business_record_id", "name": "My Business", "type": "relation",
                "relation_entity_key": "business_profile", "required": True, "indexed": True,
            },
            {
                "key": "counterparty_business_record_id", "name": "Counterparty Business", "type": "relation",
                "relation_entity_key": "business_profile", "required": True, "indexed": True,
            },
            {
                "key": "status", "name": "Status", "type": "select", "indexed": True, "required": True,
                "options": ["pending", "accepted", "declined"],
            },
            {"key": "message", "name": "Message", "type": "text"},
        ],
    },
}

_COMPOSITION = {
    "function_slugs": [],
    "entities": [_BUSINESS_CONNECTION_ENTITY],
    "connector_actions": [],
    "requires": [],
    # First real use of requires_packs (previously declared but unused by any seeded
    # pack) -- business_connection's relation fields point at business_profile, so
    # installing this without business-directory already installed would create an
    # entity referencing a schema that doesn't exist yet.
    "requires_packs": ["business-directory"],
}

_INPUT_SCHEMA = {"type": "object", "properties": {}}


def upgrade() -> None:
    now = datetime.now(timezone.utc)
    op.bulk_insert(
        feature_packs,
        [
            {
                "id": uuid4(),
                "slug": "connections-networking",
                "name": "Connections & Networking",
                "description": (
                    "LinkedIn-style connect/accept/decline between two business-directory "
                    "profiles. Each side owns only the row it created; a connection's status "
                    "is read from whichever rows exist between the two businesses."
                ),
                "version": "1.0.0",
                "tags": ["networking", "connections", "social"],
                "status": "active",
                "composition": _COMPOSITION,
                "input_schema": _INPUT_SCHEMA,
                "access_policy": {"minimum_plan": "free"},
                "created_at": now,
                "updated_at": now,
            },
        ],
    )


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