"""Seed the social-feed feature pack: business_post, post_reaction, post_comment.

A LinkedIn-style business feed. All three entities are owner_managed (public read,
owner-only write) and deliberately NOT owner_singleton (one business posts, reacts, and
comments many times) -- the same pattern connections-networking established. A "share"
is modeled as a new business_post referencing the original (shared_from_post_record_id)
rather than a fourth entity; a "like"/"save" is modeled as one post_reaction entity with
a reaction_type rather than two near-identical ones. No new Cloud capability needed:
listing a post's comments/reactions is a plain records/query filtered by an indexed
relation field, and counts use the existing record.aggregate primitive -- both already
generic regardless of owner_managed status.

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

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

revision = "115"
down_revision = "114"
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_POST_ENTITY = {
    "entity_key": "business_post",
    "name": "Business Post",
    "plural_name": "Business Posts",
    "owner_managed": True,
    "schema": {
        "fields": [
            {
                "key": "author_business_record_id", "name": "Author Business", "type": "relation",
                "relation_entity_key": "business_profile", "required": True, "indexed": True,
            },
            {"key": "content", "name": "Content", "type": "text", "required": True},
            {
                "key": "post_type", "name": "Post Type", "type": "select", "indexed": True,
                "options": [
                    "update", "product_launch", "partnership_opportunity", "requirement",
                    "announcement", "insight", "hiring", "event",
                ],
            },
            {"key": "link_url", "name": "Link URL", "type": "text"},
            {
                "key": "shared_from_post_record_id", "name": "Shared From", "type": "relation",
                "relation_entity_key": "business_post", "indexed": True,
            },
        ],
    },
}

_POST_REACTION_ENTITY = {
    "entity_key": "post_reaction",
    "name": "Post Reaction",
    "plural_name": "Post Reactions",
    "owner_managed": True,
    "schema": {
        "fields": [
            {
                "key": "post_record_id", "name": "Post", "type": "relation",
                "relation_entity_key": "business_post", "required": True, "indexed": True,
            },
            {
                "key": "business_record_id", "name": "Reacting Business", "type": "relation",
                "relation_entity_key": "business_profile", "required": True, "indexed": True,
            },
            {
                "key": "reaction_type", "name": "Reaction Type", "type": "select", "indexed": True, "required": True,
                "options": ["like", "save"],
            },
        ],
    },
}

_POST_COMMENT_ENTITY = {
    "entity_key": "post_comment",
    "name": "Post Comment",
    "plural_name": "Post Comments",
    "owner_managed": True,
    "schema": {
        "fields": [
            {
                "key": "post_record_id", "name": "Post", "type": "relation",
                "relation_entity_key": "business_post", "required": True, "indexed": True,
            },
            {
                "key": "author_business_record_id", "name": "Author Business", "type": "relation",
                "relation_entity_key": "business_profile", "required": True, "indexed": True,
            },
            {"key": "body", "name": "Body", "type": "text", "required": True},
        ],
    },
}

_COMPOSITION = {
    "function_slugs": [],
    "entities": [_BUSINESS_POST_ENTITY, _POST_REACTION_ENTITY, _POST_COMMENT_ENTITY],
    "connector_actions": [],
    "requires": [],
    "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": "social-feed",
                "name": "Social Feed",
                "description": (
                    "A professional business feed: posts, reshares, likes/saves, and "
                    "comments between business-directory profiles."
                ),
                "version": "1.0.0",
                "tags": ["feed", "posts", "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 = 'social-feed'")
