"""Seed the business-directory feature pack: one owner_managed entity, business_profile.

Each signed-up business manages its own public profile, like a normal social-app user
profile -- publicly readable by anyone, but only the owning app_user can create/edit/
delete it. Neither existing entity mode fit this (non-RLS is open write too; rls_enabled
also hides reads from everyone but the owner), so this is the first pack to use the new
`owner_managed` entity flag (see the companion P2HSCloud commit) instead of `rls_enabled`.

Revision ID: 112
Revises: 111
"""
from datetime import datetime, timezone
from uuid import uuid4

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

revision = "112"
down_revision = "111"
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_PROFILE_ENTITY = {
    "entity_key": "business_profile",
    "name": "Business Profile",
    "plural_name": "Business Profiles",
    # Public read, owner-only write — see apps/pc-gateway/src/routes/records.ts on Cloud.
    "owner_managed": True,
    "schema": {
        "fields": [
            {"key": "business_name", "name": "Business Name", "type": "text", "required": True, "indexed": True},
            {
                "key": "business_type", "name": "Business Type", "type": "select", "indexed": True,
                "options": [
                    "startup", "school_education", "retail", "manufacturing", "restaurant_food",
                    "technology", "healthcare", "finance", "real_estate", "services",
                    "freelancing", "other",
                ],
            },
            {"key": "industry", "name": "Industry", "type": "text", "indexed": True},
            {"key": "description", "name": "Description", "type": "text"},
            {"key": "website", "name": "Website", "type": "text"},
            {"key": "logo_url", "name": "Logo URL", "type": "text"},
            {"key": "location_country", "name": "Country", "type": "text", "indexed": True},
            {"key": "location_city", "name": "City", "type": "text", "indexed": True},
            {
                "key": "needs", "name": "Needs", "type": "multi_select",
                "options": [
                    "customers", "suppliers", "investors", "partners", "employees",
                    "technology", "marketing", "funding", "distribution", "infrastructure", "other",
                ],
            },
            {
                "key": "goals", "name": "Goals", "type": "multi_select",
                "options": [
                    "increase_sales", "find_customers", "find_suppliers", "expand_markets",
                    "raise_funding", "build_partnerships", "improve_operations",
                    "launch_product", "start_business",
                ],
            },
            {
                "key": "verification_status", "name": "Verification Status", "type": "select", "indexed": True,
                "options": ["unverified", "pending", "verified"],
            },
        ],
    },
}

_COMPOSITION = {
    "function_slugs": [],
    "entities": [_BUSINESS_PROFILE_ENTITY],
    "connector_actions": [],
    "requires": [],
}

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


def upgrade() -> None:
    now = datetime.now(timezone.utc)
    op.bulk_insert(
        feature_packs,
        [
            {
                "id": uuid4(),
                "slug": "business-directory",
                "name": "Business Directory",
                "description": (
                    "A publicly browsable directory of business profiles, each owned and "
                    "editable only by the app-user who created it — like a normal user "
                    "profile, not staff-managed catalog data."
                ),
                "version": "1.0.0",
                "tags": ["directory", "profiles", "networking"],
                "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 = 'business-directory'")
