"""business-directory v1.1.0: business_profile needs owner_singleton explicitly.

Cloud's owner_managed create check originally rejected a second owned record for ANY
owner_managed entity unconditionally. That was corrected (see the companion P2HSCloud
commit adding a separate owner_singleton flag, needed so connections-networking's
business_connection -- an owner_managed entity that legitimately needs many rows per
owner -- doesn't get wrongly capped at one). business_profile was relying on the old
unconditional behavior for its real invariant ("one profile per user"), so it needs
owner_singleton: true added explicitly to keep that guarantee.

Revision ID: 113
Revises: 112
"""
from datetime import datetime, timezone

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

revision = "113"
down_revision = "112"
branch_labels = None
depends_on = None

feature_packs = sa.table(
    "feature_packs",
    sa.column("slug", sa.String),
    sa.column("version", sa.String),
    sa.column("composition", postgresql.JSONB),
    sa.column("updated_at", sa.DateTime(timezone=True)),
)

_BUSINESS_PROFILE_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"],
    },
]

_BUSINESS_PROFILE_ENTITY_V1_1 = {
    "entity_key": "business_profile",
    "name": "Business Profile",
    "plural_name": "Business Profiles",
    "owner_managed": True,
    "owner_singleton": True,
    "schema": {"fields": _BUSINESS_PROFILE_FIELDS},
}

_BUSINESS_PROFILE_ENTITY_V1_0 = {
    "entity_key": "business_profile",
    "name": "Business Profile",
    "plural_name": "Business Profiles",
    "owner_managed": True,
    "schema": {"fields": _BUSINESS_PROFILE_FIELDS},
}

_COMPOSITION_V1_1 = {
    "function_slugs": [],
    "entities": [_BUSINESS_PROFILE_ENTITY_V1_1],
    "connector_actions": [],
    "requires": [],
}

_COMPOSITION_V1_0 = {
    "function_slugs": [],
    "entities": [_BUSINESS_PROFILE_ENTITY_V1_0],
    "connector_actions": [],
    "requires": [],
}


def upgrade() -> None:
    op.execute(
        sa.update(feature_packs)
        .where(feature_packs.c.slug == "business-directory")
        .values(version="1.1.0", composition=_COMPOSITION_V1_1, updated_at=datetime.now(timezone.utc))
    )


def downgrade() -> None:
    op.execute(
        sa.update(feature_packs)
        .where(feature_packs.c.slug == "business-directory")
        .values(version="1.0.0", composition=_COMPOSITION_V1_0, updated_at=datetime.now(timezone.utc))
    )
