"""Seed the classifieds feature pack: classified_listing (owner_managed).

Craigslist/OLX-style listings -- a signed-in user posts something for sale, wanted, a
service, or free, publicly browsable, editable only by its own poster. Same
owner_managed (not owner_singleton) shape as social-feed's business_post: one user
posts many listings. Deliberately standalone (requires_packs: []) rather than
depending on business-directory -- a classifieds seller is just a signed-in user, not
necessarily a "business" (the business_profile framing would feel wrong for a casual
garage-sale-style listing).

"Message the seller" is an OPTIONAL integration with the messaging pack (now
generalized to not require business_profile either, see migration 118) -- codegen
starts a fresh conversation when a buyer clicks "message seller", there is no stored
link between a listing and its conversations; a listing works fine standalone if
messaging isn't installed (seller_contact_name is there for that case).

Revision ID: 119
Revises: 118
"""
from datetime import datetime, timezone
from uuid import uuid4

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

revision = "119"
down_revision = "118"
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),
)

_CLASSIFIED_LISTING_ENTITY = {
    "entity_key": "classified_listing",
    "name": "Classified Listing",
    "plural_name": "Classified Listings",
    "owner_managed": True,
    "schema": {
        "fields": [
            {"key": "title", "name": "Title", "type": "text", "required": True, "indexed": True},
            {"key": "description", "name": "Description", "type": "text", "required": True},
            {
                "key": "listing_type", "name": "Listing Type", "type": "select", "required": True, "indexed": True,
                "options": ["for_sale", "wanted", "service", "free"],
            },
            {"key": "category", "name": "Category", "type": "text", "indexed": True},
            {"key": "price", "name": "Price", "type": "number"},
            {"key": "currency", "name": "Currency", "type": "text"},
            {
                "key": "condition", "name": "Condition", "type": "select", "indexed": True,
                "options": ["new", "used", "for_parts"],
            },
            {
                "key": "tags", "name": "Tags", "type": "multi_select",
                "options": [
                    "negotiable", "urgent", "like_new", "warranty_included",
                    "delivery_available", "pickup_only", "bulk_discount", "rare",
                    "vintage", "local_only",
                ],
            },
            {"key": "location_country", "name": "Country", "type": "text", "indexed": True},
            {"key": "location_city", "name": "City", "type": "text", "indexed": True},
            {"key": "images", "name": "Images", "type": "json"},
            {
                "key": "status", "name": "Status", "type": "select", "required": True, "indexed": True,
                "options": ["active", "sold", "expired", "removed"],
            },
            {"key": "seller_contact_name", "name": "Seller Contact Name", "type": "text"},
            {
                "key": "seller_ref", "name": "Seller Reference", "type": "text", "indexed": True,
                "description": "Optional link to this project's own identity concept (e.g. a business_profile id), same convention as messaging's participant_ref/sender_ref. Display-only -- ownership is owner_user_id.",
            },
        ],
    },
}

_COMPOSITION = {
    "function_slugs": [],
    "entities": [_CLASSIFIED_LISTING_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": "classifieds",
                "name": "Classifieds",
                "description": (
                    "Craigslist/OLX-style listings -- users post items for sale, "
                    "wanted, services, or free, publicly browsable, editable only "
                    "by their own poster. Pairs well with the messaging pack for "
                    "buyer-seller inquiries, but works standalone."
                ),
                "version": "1.0.0",
                "tags": ["classifieds", "marketplace", "listings"],
                "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 = 'classifieds'")
