"""content-presets: typed content.collection entity presets so Codegen stops
reinventing testimonial/FAQ/service/team/location/pricing/gallery/event
schemas per generated project. Staff-authored via admin record.create/edit,
publicly readable through content.collection (same posture as product-catalog:
staff_managed ownership, public GET). Provides/security/planner come from the
catalog_v2.py overlay via stamp_composition, same as content-ingest (187).

Revision ID: 189
Revises: 188
"""
from uuid import uuid4

from alembic import op
from sqlalchemy.dialects.postgresql import JSONB, UUID, insert
import sqlalchemy as sa

revision = "189"
down_revision = "188"
branch_labels = None
depends_on = None

feature_packs = sa.table("feature_packs",
    sa.column("id", 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", JSONB),
    sa.column("status", sa.String),
    sa.column("composition", JSONB),
    sa.column("input_schema", JSONB),
    sa.column("access_policy", JSONB),
)


def content_presets_composition() -> dict:
    from src.domain.feature_packs.normalize import stamp_composition

    return stamp_composition("content-presets", {
        "function_slugs": [], "connector_actions": [], "requires": [],
        "entities": [
            {
                "entity_key": "testimonial", "name": "Testimonial", "plural_name": "Testimonials",
                "schema": {"fields": [
                    {"key": "quote", "name": "Quote", "type": "text", "required": True},
                    {"key": "author_name", "name": "Author name", "type": "text", "required": True},
                    {"key": "author_role", "name": "Author role", "type": "text"},
                    {"key": "author_organization", "name": "Author organization", "type": "text"},
                    {"key": "avatar_file_id", "name": "Avatar", "type": "text"},
                    {"key": "rating", "name": "Rating", "type": "number"},
                    {"key": "source_url", "name": "Source URL", "type": "text"},
                    {"key": "featured", "name": "Featured", "type": "boolean"},
                ]},
            },
            {
                "entity_key": "faq_item", "name": "FAQ Item", "plural_name": "FAQ Items",
                "schema": {"fields": [
                    {"key": "question", "name": "Question", "type": "text", "required": True},
                    {"key": "answer", "name": "Answer", "type": "text", "required": True},
                    {"key": "category", "name": "Category", "type": "text"},
                    {"key": "sort_order", "name": "Sort order", "type": "number"},
                ]},
            },
            {
                "entity_key": "service_item", "name": "Service", "plural_name": "Services",
                "schema": {"fields": [
                    {"key": "name", "name": "Name", "type": "text", "required": True},
                    {"key": "description", "name": "Description", "type": "text"},
                    {"key": "icon_file_id", "name": "Icon or image", "type": "text"},
                    {"key": "price_hint", "name": "Price hint", "type": "text"},
                    {"key": "category", "name": "Category", "type": "text"},
                    {"key": "sort_order", "name": "Sort order", "type": "number"},
                ]},
            },
            {
                "entity_key": "team_member", "name": "Team Member", "plural_name": "Team Members",
                "schema": {"fields": [
                    {"key": "name", "name": "Name", "type": "text", "required": True},
                    {"key": "role", "name": "Role", "type": "text", "required": True},
                    {"key": "bio", "name": "Bio", "type": "text"},
                    {"key": "photo_file_id", "name": "Photo", "type": "text"},
                    {"key": "email", "name": "Email", "type": "text"},
                    {"key": "social_links", "name": "Social links", "type": "json"},
                    {"key": "sort_order", "name": "Sort order", "type": "number"},
                ]},
            },
            {
                "entity_key": "business_location", "name": "Location", "plural_name": "Locations",
                "schema": {"fields": [
                    {"key": "name", "name": "Name", "type": "text", "required": True},
                    {"key": "address", "name": "Address", "type": "text", "required": True},
                    {"key": "phone", "name": "Phone", "type": "text"},
                    {"key": "email", "name": "Email", "type": "text"},
                    {"key": "hours", "name": "Hours", "type": "text"},
                    {"key": "map_url", "name": "Map URL", "type": "text"},
                    {"key": "is_primary", "name": "Primary location", "type": "boolean"},
                ]},
            },
            {
                "entity_key": "pricing_tier", "name": "Pricing Tier", "plural_name": "Pricing Tiers",
                "schema": {"fields": [
                    {"key": "name", "name": "Name", "type": "text", "required": True},
                    {"key": "price", "name": "Price", "type": "text", "required": True},
                    {"key": "billing_period", "name": "Billing period", "type": "text"},
                    {"key": "description", "name": "Description", "type": "text"},
                    {"key": "features", "name": "Features", "type": "json"},
                    {"key": "is_featured", "name": "Featured", "type": "boolean"},
                    {"key": "cta_label", "name": "CTA label", "type": "text"},
                    {"key": "sort_order", "name": "Sort order", "type": "number"},
                ]},
            },
            {
                "entity_key": "gallery_item", "name": "Gallery Item", "plural_name": "Gallery Items",
                "schema": {"fields": [
                    {"key": "title", "name": "Title", "type": "text"},
                    {"key": "image_file_id", "name": "Image", "type": "text", "required": True},
                    {"key": "caption", "name": "Caption", "type": "text"},
                    {"key": "category", "name": "Category", "type": "text"},
                    {"key": "sort_order", "name": "Sort order", "type": "number"},
                ]},
            },
            {
                "entity_key": "event_item", "name": "Event", "plural_name": "Events",
                "schema": {"fields": [
                    {"key": "name", "name": "Name", "type": "text", "required": True},
                    {"key": "description", "name": "Description", "type": "text"},
                    {"key": "starts_at", "name": "Starts at", "type": "date", "required": True},
                    {"key": "ends_at", "name": "Ends at", "type": "date"},
                    {"key": "location", "name": "Location", "type": "text"},
                    {"key": "image_file_id", "name": "Image", "type": "text"},
                    {"key": "registration_url", "name": "Registration URL", "type": "text"},
                ]},
            },
        ],
    }, implementation_version="1.0.0")


def upgrade() -> None:
    op.get_bind().execute(insert(feature_packs).values(
        id=uuid4(), slug="content-presets", name="Content Presets", version="1.0.0",
        description=(
            "Canonical staff-authored content entities (testimonials, FAQs, "
            "services, team members, locations, pricing tiers, gallery items, "
            "events) for pairing with the content.collection App Block, so "
            "these common marketing-site structures don't need a bespoke "
            "schema invented per generated project."
        ),
        tags=["content", "marketing", "website"], status="active",
        composition=content_presets_composition(),
        input_schema={"type": "object", "properties": {}},
        access_policy={"minimum_plan": "free"},
    ).on_conflict_do_nothing(index_elements=["slug"]))


def downgrade() -> None:
    pass
